Skip to content

Commit

Permalink
First controller setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Oct 22, 2024
1 parent 5db3364 commit 7a78fc0
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 326 deletions.
83 changes: 38 additions & 45 deletions lib/Controller/BerichtenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,20 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCA\ZaakAfhandelApp\Service\ObjectService;
use OCP\IRequest;

class BerichtenController extends Controller
{
public function __construct(
$appName,
IRequest $request,
private readonly IAppConfig $config
private readonly ObjectService $objectService,
)
{
parent::__construct($appName, $request);
}

/**
* This returns the template of the main app's page
* It adds some data to the template (app version)
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
//Application::APP_ID,
'zaakafhandelapp',
'index',
[]
);
}


/**
* Return (and serach) all objects
Expand All @@ -51,11 +32,14 @@ public function page(): TemplateResponse
*/
public function index(CallService $callService): JSONResponse
{
// Latere zorg
$query= $this->request->getParams();
// Retrieve all request parameters
$requestParams = $this->request->getParams();

$results = $callService->index(source: 'klanten', endpoint: 'taken');
return new JSONResponse($results);
// Fetch catalog objects based on filters and order
$data = $this->objectService->getResultArrayForRequest('berichten', $requestParams);

// Return JSON response
return new JSONResponse($data);
}

/**
Expand All @@ -68,11 +52,11 @@ public function index(CallService $callService): JSONResponse
*/
public function show(string $id, CallService $callService): JSONResponse
{
// Latere zorg
$query= $this->request->getParams();
// Fetch the catalog object by its ID
$object = $this->objectService->getObject('berichten', $id);

$results = $callService->show(source: 'klanten', endpoint: 'taken', id: $id);
return new JSONResponse($results);
// Return the catalog as a JSON response
return new JSONResponse($object);
}


Expand All @@ -86,18 +70,17 @@ public function show(string $id, CallService $callService): JSONResponse
*/
public function create(CallService $callService): JSONResponse
{
// get post from requests
$body = $this->request->getParams();
$body['id'] = $this->get_guid(); //@todo remove this hotfic (or actually horror fix)
$results = $callService->create(source: 'klanten', endpoint: 'taken', data: $body);
return new JSONResponse($results);
}
// Get all parameters from the request
$data = $this->request->getParams();

function get_guid() {
$data = PHP_MAJOR_VERSION < 7 ? openssl_random_pseudo_bytes(16) : random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // Set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // Set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
// Remove the 'id' field if it exists, as we're creating a new object
unset($data['id']);

// Save the new catalog object
$object = $this->objectService->saveObject('berichten', $data);

// Return the created object as a JSON response
return new JSONResponse($object);
}

/**
Expand All @@ -110,9 +93,17 @@ function get_guid() {
*/
public function update(string $id, CallService $callService): JSONResponse
{
$body = $this->request->getParams();
$results = $callService->update(source: 'klanten', endpoint: 'taken', data: $body, id: $id);
return new JSONResponse($results);
// Get all parameters from the request
$data = $this->request->getParams();

// Remove the 'id' field if it exists, as we're creating a new object
unset($data['id']);

// Save the new catalog object
$object = $this->objectService->saveObject('berichten', $data);

// Return the created object as a JSON response
return new JSONResponse($object);
}

/**
Expand All @@ -125,8 +116,10 @@ public function update(string $id, CallService $callService): JSONResponse
*/
public function destroy(string $id, CallService $callService): JSONResponse
{
$callService->destroy(source: 'klanten', endpoint: 'taken', id: $id);
// Delete the catalog object
$result = $this->objectService->deleteObject('berichten', $id);

return new JsonResponse([]);
// Return the result as a JSON response
return new JSONResponse(['success' => $result], $result === true ? '200' : '404');
}
}
78 changes: 39 additions & 39 deletions lib/Controller/KlantenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCA\ZaakAfhandelApp\Service\CallService;
use OCP\IAppConfig;
use OCA\ZaakAfhandelApp\Service\ObjectService;
use OCP\IRequest;

class KlantenController extends Controller
Expand All @@ -17,31 +16,12 @@ public function __construct
(
$appName,
IRequest $request,
private readonly IAppConfig $config
private readonly ObjectService $objectService,
)
{
parent::__construct($appName, $request);
}

/**
* This returns the template of the main app's page
* It adds some data to the template (app version)
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
//Application::APP_ID,
'zaakafhandelapp',
'index',
[]
);
}


/**
* Return (and serach) all objects
Expand All @@ -53,11 +33,14 @@ public function page(): TemplateResponse
*/
public function index(CallService $klantenService): JSONResponse
{
// Latere zorg
$query= $this->request->getParams();
// Retrieve all request parameters
$requestParams = $this->request->getParams();

// Fetch catalog objects based on filters and order
$data = $this->objectService->getResultArrayForRequest('klanten', $requestParams);

$results = $klantenService->index(source: 'klanten', endpoint: 'klanten');
return new JSONResponse($results);
// Return JSON response
return new JSONResponse($data);
}

/**
Expand All @@ -70,11 +53,11 @@ public function index(CallService $klantenService): JSONResponse
*/
public function show(string $id, CallService $klantenService): JSONResponse
{
// Latere zorg
$query= $this->request->getParams();
// Fetch the catalog object by its ID
$object = $this->objectService->getObject('klanten', $id);

$results = $klantenService->show(source: 'klanten', endpoint: 'klanten', id: $id);
return new JSONResponse($results);
// Return the catalog as a JSON response
return new JSONResponse($object);
}


Expand All @@ -88,10 +71,17 @@ public function show(string $id, CallService $klantenService): JSONResponse
*/
public function create(CallService $callService): JSONResponse
{
// get post from requests
$body = $this->request->getParams();
$results = $callService->create(source: 'klanten', endpoint: 'klanten', data: $body);
return new JSONResponse($results);
// Get all parameters from the request
$data = $this->request->getParams();

// Remove the 'id' field if it exists, as we're creating a new object
unset($data['id']);

// Save the new catalog object
$object = $this->objectService->saveObject('klanten', $data);

// Return the created object as a JSON response
return new JSONResponse($object);
}

/**
Expand All @@ -104,9 +94,17 @@ public function create(CallService $callService): JSONResponse
*/
public function update(string $id, CallService $callService): JSONResponse
{
$body = $this->request->getParams();
$results = $callService->update(source: 'klanten', endpoint: 'klanten', data: $body, id: $id);
return new JSONResponse($results);
// Get all parameters from the request
$data = $this->request->getParams();

// Remove the 'id' field if it exists, as we're creating a new object
unset($data['id']);

// Save the new catalog object
$object = $this->objectService->saveObject('klanten', $data);

// Return the created object as a JSON response
return new JSONResponse($object);
}

/**
Expand All @@ -119,8 +117,10 @@ public function update(string $id, CallService $callService): JSONResponse
*/
public function destroy(string $id, CallService $callService): JSONResponse
{
$callService->destroy(source: 'klanten', endpoint: 'klanten', id: $id);
// Delete the catalog object
$result = $this->objectService->deleteObject('klanten', $id);

return new JsonResponse([]);
// Return the result as a JSON response
return new JSONResponse(['success' => $result], $result === true ? '200' : '404');
}
}
Loading

0 comments on commit 7a78fc0

Please sign in to comment.