179 lines
6.3 KiB
PHP
179 lines
6.3 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\Chrono\Collection\ICollectionBase;
|
|
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
use InvalidArgumentException;
|
|
use KTXF\Resource\Selector\SourceSelector;
|
|
use KTXM\ChronoManager\Manager;
|
|
|
|
class CollectionController extends ControllerAbstract {
|
|
|
|
public function __construct(
|
|
private readonly SessionTenant $tenantIdentity,
|
|
private readonly SessionIdentity $userIdentity,
|
|
private Manager $chronoManager,
|
|
) {}
|
|
|
|
/**
|
|
* Retrieve all collections matching criteria
|
|
*
|
|
* @param SourceSelector|null $sources collection sources
|
|
* @param array|null $filter collection filter options
|
|
* @param array|null $sort collection sorting options
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/list', name: 'chronomanager.collection.list', methods: ['POST'])]
|
|
|
|
public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?string $uid = null): JsonResponse {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve collections
|
|
$responseData = $this->chronoManager->collectionList($tenantId, $userId, $sources, $filter, $sort);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Confirm if specific collections are available for a specific user
|
|
*
|
|
* @param SourceSelector $sources collection sources
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/extant', name: 'chronomanager.collection.extant', methods: ['POST'])]
|
|
|
|
public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve collection status
|
|
$responseData = $this->chronoManager->collectionExtant($tenantId, $userId, $sources);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Fetch a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $identifier collection identifier
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/fetch', name: 'chronomanager.collection.fetch', methods: ['POST'])]
|
|
|
|
public function fetch(string $provider, string $service, string|int $identifier, ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// retrieve collection
|
|
$responseData = $this->chronoManager->collectionFetch($tenantId, $userId, $provider, $service, $identifier);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param ICollectionBase|array $data collection to create
|
|
* @param array $options additional options
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/create', name: 'chronomanager.collection.create', methods: ['POST'])]
|
|
|
|
public function create(string $provider, string $service, ICollectionBase|array $data, array $options = [], ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// create collection
|
|
$responseData = $this->chronoManager->collectionCreate($tenantId, $userId, $provider, $service, $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 collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $identifier collection identifier
|
|
* @param ICollectionBase|array $data collection data
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/modify', name: 'chronomanager.collection.modify', methods: ['POST'])]
|
|
|
|
public function modify(string $provider, string $service, string|int $identifier, ICollectionBase|array $data, ?string $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// modify collection
|
|
$responseData = $this->chronoManager->collectionModify($tenantId, $userId, $provider, $service, $identifier, $data);
|
|
|
|
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
} catch (InvalidArgumentException $e) {
|
|
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a collection
|
|
*
|
|
* @param string $provider provider identifier
|
|
* @param string $service service identifier
|
|
* @param string|int $identifier collection identifier
|
|
* @param string|null $uid user identifier
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
#[AuthenticatedRoute('/collection/destroy', name: 'chronomanager.collection.destroy', methods: ['POST'])]
|
|
|
|
public function destroy(string $provider, string $service, string|int $identifier, string|null $uid = null): JsonResponse {
|
|
try {
|
|
// authorize request
|
|
$tenantId = $this->tenantIdentity->identifier();
|
|
$userId = $this->userIdentity->identifier();
|
|
// destroy collection
|
|
$success = $this->chronoManager->collectionDestroy($tenantId, $userId, $provider, $service, $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);
|
|
}
|
|
}
|
|
|
|
}
|