Skip to content
Snippets Groups Projects
Unverified Commit 5dfc56e9 authored by Joas Schilling's avatar Joas Schilling Committed by Julius Härtl
Browse files

Allow to create collections

parent 136d2c39
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment