203 lines
7.2 KiB
PHP
203 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXM\ChronoManager\Controllers;
|
|
|
|
use KTXC\Http\Response\JsonResponse;
|
|
use KTXC\SessionIdentity;
|
|
use KTXC\SessionTenant;
|
|
use KTXF\Controller\ControllerAbstract;
|
|
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
use InvalidArgumentException;
|
|
use KTXF\Resource\Selector\SourceSelector;
|
|
use KTXM\ChronoManager\Manager;
|
|
|
|
class EntityController extends ControllerAbstract {
|
|
|
|
public function __construct(
|
|
private readonly SessionTenant $tenantIdentity,
|
|
private readonly SessionIdentity $userIdentity,
|
|
private Manager $chronoManager,
|
|
) {}
|
|
|
|
/**
|
|
* List entities for a specific user
|
|
*
|
|
* @param SourceSelector|null $sources entity sources
|
|
* @param array|null $filter entity filter
|
|
* @param array|null $sort entity sort
|
|
* @param array|null $range entity range
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/list', name: 'chronomanager.entity.list', methods: ['POST'])]
|
|
|
|
public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?array $range = null, ?string $uid = null): JsonResponse {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve entities
|
|
$responseData = $this->chronoManager->entityList($tenantId, $userId, $sources, $filter, $sort, $range);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Delta of entity changes since last request
|
|
*
|
|
* @param SourceSelector $sources entity sources
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/delta', name: 'chronomanager.entity.delta', methods: ['POST'])]
|
|
|
|
public function delta(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve entity delta
|
|
$responseData = $this->chronoManager->entityDelta($tenantId, $userId, $sources);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Confirm if specific entities are available for a specific user
|
|
*
|
|
* @param SourceSelector $sources entity sources
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/extant', name: 'chronomanager.entity.extant', methods: ['POST'])]
|
|
|
|
public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve entity status
|
|
$responseData = $this->chronoManager->entityExtant($tenantId, $userId, $sources);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Fetch specific entities from a specific collection
|
|
*
|
|
* @param string|null $uid user identifier
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $collection collection identifier
|
|
* @param array<string|int> $identifiers entity identifiers
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/fetch', name: 'chronomanager.entity.fetch', methods: ['POST'])]
|
|
|
|
public function fetch(string $provider, string $service, string|int $collection, array $identifiers, ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve entities
|
|
$responseData = $this->chronoManager->entityFetch($tenantId, $userId, $provider, $service, $collection, $identifiers);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new entity in a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $collection collection identifier
|
|
* @param array $data entity to create
|
|
* @param array $options additional options
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/create', name: 'chronomanager.entity.create', methods: ['POST'])]
|
|
|
|
public function create(string $provider, string $service, string|int $collection, array $data, array $options = [], ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// create entity
|
|
$responseData = $this->chronoManager->entityCreate($tenantId, $userId, $provider, $service, $collection, $data, $options);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modify an existing entity in a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $collection collection identifier
|
|
* @param string|int $identifier entity identifier
|
|
* @param array $entity entity with modifications
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/modify', name: 'chronomanager.entity.modify', methods: ['POST'])]
|
|
|
|
public function modify(string $provider, string $service, string|int $collection, string|int $identifier, array $data, ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// modify entity
|
|
$responseData = $this->chronoManager->entityModify($tenantId, $userId, $provider, $service, $collection, $identifier, $data);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete an entity from a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $collection collection identifier
|
|
* @param string|int $identifier entity identifier
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/entity/destroy', name: 'chronomanager.entity.destroy', methods: ['POST'])]
|
|
public function destroy(string $provider, string $service, string|int $collection, string|int $identifier, ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// destroy entity
|
|
$success = $this->chronoManager->entityDestroy($tenantId, $userId, $provider, $service, $collection, $identifier);
|
|
|
|
return new JsonResponse(['success' => $success], $success ? JsonResponse::HTTP_OK : JsonResponse::HTTP_NOT_FOUND);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
}
|