Allow to create collections
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
136d2c39ac
commit
5dfc56e925
5 changed files with 54 additions and 1 deletions
|
@ -160,8 +160,40 @@ class CollaborationResourcesController extends OCSController {
|
|||
return new DataResponse(array_map([$this, 'prepareCollection'], $resource->getCollections()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param string $baseResourceType
|
||||
* @param string $baseResourceId
|
||||
* @param string $resourceType
|
||||
* @param string $resourceId
|
||||
* @return DataResponse
|
||||
*/
|
||||
public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $resourceType, string $resourceId): DataResponse {
|
||||
try {
|
||||
$baseResource = $this->manager->getResource($baseResourceType, $baseResourceId);
|
||||
$resource = $this->manager->getResource($resourceType, $resourceId);
|
||||
} catch (CollectionException $e) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (!$baseResource->canAccess($this->userSession->getUser()) ||
|
||||
!$resource->canAccess($this->userSession->getUser())) {
|
||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
$collection = $this->manager->newCollection();
|
||||
$collection->addResource($baseResource);
|
||||
$collection->addResource($resource);
|
||||
|
||||
return new DataResponse($this->prepareCollection($collection));
|
||||
}
|
||||
|
||||
protected function prepareCollection(ICollection $collection): array {
|
||||
return array_map([$this, 'prepareResources'], $collection->getResources());
|
||||
return [
|
||||
'id' => $collection->getId(),
|
||||
'resources' => array_map([$this, 'prepareResources'], $collection->getResources()),
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareResources(IResource $resource): array {
|
||||
|
|
|
@ -95,6 +95,7 @@ $application->registerRoutes($this, [
|
|||
['root' => '/collaboration', 'name' => 'CollaborationResources#addResource', 'url' => '/resources/collections/{collectionId}', 'verb' => 'POST'],
|
||||
['root' => '/collaboration', 'name' => 'CollaborationResources#removeResource', 'url' => '/resources/collections/{collectionId}', 'verb' => 'DELETE'],
|
||||
['root' => '/collaboration', 'name' => 'CollaborationResources#getCollectionsByResource', 'url' => '/resources/{resourceType}/{resourceId}', 'verb' => 'GET'],
|
||||
['root' => '/collaboration', 'name' => 'CollaborationResources#createCollectionOnResource', 'url' => '/resources/{baseResourceType}/{baseResourceId}', 'verb' => 'POST'],
|
||||
],
|
||||
]);
|
||||
|
||||
|
|
|
@ -49,6 +49,14 @@ class Manager implements IManager {
|
|||
return new Collection($this, $this->connection, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ICollection
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function newCollection(): ICollection {
|
||||
return new Collection($this, $this->connection, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $id
|
||||
|
|
|
@ -29,6 +29,12 @@ use OCP\IUser;
|
|||
*/
|
||||
interface ICollection {
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getId(): int;
|
||||
|
||||
/**
|
||||
* @return IResource[]
|
||||
* @since 15.0.0
|
||||
|
|
|
@ -34,6 +34,12 @@ interface IManager extends IProvider {
|
|||
*/
|
||||
public function getCollection(int $id): ICollection;
|
||||
|
||||
/**
|
||||
* @return ICollection
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function newCollection(): ICollection;
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $id
|
||||
|
|
Loading…
Reference in a new issue