Skip to content

Commit

Permalink
First endpoint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Dec 7, 2024
1 parent 530ea49 commit 3fe8ad1
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [
'resources' => [
'Endpoints' => ['url' => 'api/endpoints'],
'Sources' => ['url' => 'api/sources'],
'Mappings' => ['url' => 'api/mappings'],
'Jobs' => ['url' => 'api/jobs'],
Expand Down
179 changes: 179 additions & 0 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

namespace OCA\OpenConnector\Controller;

use OCA\OpenConnector\Service\ObjectService;
use OCA\OpenConnector\Service\SearchService;
use OCA\OpenConnector\Db\Endpoint;
use OCA\OpenConnector\Db\EndpointMapper;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use OCA\OpenConnector\Service\EndpointService;

/**
* Controller for managing endpoints in the OpenConnector app
*/
class EndpointController extends Controller
{
/**
* Constructor for the EndpointController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param EndpointMapper $endpointMapper Mapper for endpoint database operations
* @param EndpointService $endpointService Service for endpoint business logic
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private EndpointMapper $endpointMapper,
private EndpointService $endpointService,
)
{
parent::__construct($appName, $request);
}

/**
* Returns the template of the main app's page
*
* This method renders the main page of the application, adding any necessary data to the template.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse The rendered template response
*/
public function page(): TemplateResponse
{
return new TemplateResponse(
'openconnector',
'index',
[]
);
}

/**
* Retrieves a list of all endpoints
*
* This method returns a JSON response containing an array of all endpoints in the system.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the list of endpoints
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->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([]);
}
}

0 comments on commit 3fe8ad1

Please sign in to comment.