diff --git a/appinfo/routes.php b/appinfo/routes.php index a07197c..5531ecd 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -2,6 +2,7 @@ return [ 'resources' => [ + 'Endpoints' => ['url' => 'api/endpoints'], 'Sources' => ['url' => 'api/sources'], 'Mappings' => ['url' => 'api/mappings'], 'Jobs' => ['url' => 'api/jobs'], diff --git a/lib/Controller/EndpointController.php b/lib/Controller/EndpointController.php new file mode 100644 index 0000000..124eeba --- /dev/null +++ b/lib/Controller/EndpointController.php @@ -0,0 +1,179 @@ +request->getParams(); + $fieldsToSearch = ['name', 'description', 'url']; + + $searchParams = $searchService->createMySQLSearchParams(filters: $filters); + $searchConditions = $searchService->createMySQLSearchConditions(filters: $filters, fieldsToSearch: $fieldsToSearch); + $filters = $searchService->unsetSpecialQueryParams(filters: $filters); + + return new JSONResponse(['results' => $this->endpointMapper->findAll(limit: null, offset: null, filters: $filters, searchConditions: $searchConditions, searchParams: $searchParams)]); + } + + /** + * Retrieves a single endpoint by its ID + * + * This method returns a JSON response containing the details of a specific endpoint. + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $id The ID of the endpoint to retrieve + * @return JSONResponse A JSON response containing the endpoint details + */ + public function show(string $id): JSONResponse + { + try { + return new JSONResponse($this->endpointMapper->find(id: (int) $id)); + } catch (DoesNotExistException $exception) { + return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404); + } + } + + /** + * Creates a new endpoint + * + * This method creates a new endpoint based on POST data. + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse A JSON response containing the created endpoint + */ + public function create(): JSONResponse + { + $data = $this->request->getParams(); + + foreach ($data as $key => $value) { + if (str_starts_with($key, '_')) { + unset($data[$key]); + } + } + + if (isset($data['id'])) { + unset($data['id']); + } + + // Create the endpoint + $endpoint = $this->endpointMapper->createFromArray(object: $data); + + return new JSONResponse($endpoint); + } + + /** + * Updates an existing endpoint + * + * This method updates an existing endpoint based on its ID. + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $id The ID of the endpoint to update + * @return JSONResponse A JSON response containing the updated endpoint details + */ + public function update(int $id): JSONResponse + { + $data = $this->request->getParams(); + + foreach ($data as $key => $value) { + if (str_starts_with($key, '_')) { + unset($data[$key]); + } + } + if (isset($data['id'])) { + unset($data['id']); + } + + // Update the endpoint + $endpoint = $this->endpointMapper->updateFromArray(id: (int) $id, object: $data); + + return new JSONResponse($endpoint); + } + + /** + * Deletes an endpoint + * + * This method deletes an endpoint based on its ID. + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $id The ID of the endpoint to delete + * @return JSONResponse An empty JSON response + */ + public function destroy(int $id): JSONResponse + { + $this->endpointMapper->delete($this->endpointMapper->find((int) $id)); + + return new JSONResponse([]); + } +}