]> source.dussan.org Git - nextcloud-server.git/commitdiff
Implement search and rename in backend
authorJulius Härtl <jus@bitgrid.net>
Tue, 29 Jan 2019 11:23:14 +0000 (12:23 +0100)
committerJulius Härtl <jus@bitgrid.net>
Fri, 1 Mar 2019 19:56:18 +0000 (20:56 +0100)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
core/Controller/CollaborationResourcesController.php
core/routes.php
lib/private/Collaboration/Resources/Manager.php
lib/public/Collaboration/Resources/IManager.php

index a15904a4c2ac4d09dd1702ef0f76b1bd4ce7a4ad..3c9937aade244c625199621a20870d884dd03fe2 100644 (file)
@@ -84,6 +84,22 @@ class CollaborationResourcesController extends OCSController {
                return new DataResponse($this->prepareCollection($collection));
        }
 
+       /**
+        * @NoAdminRequired
+        *
+        * @param int $collectionId
+        * @return DataResponse
+        */
+       public function searchCollections(string $filter): DataResponse {
+               try {
+                       $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
+               } catch (CollectionException $e) {
+                       return new DataResponse([], Http::STATUS_NOT_FOUND);
+               }
+
+               return new DataResponse(array_map([$this, 'prepareCollection'], $collections));
+       }
+
        /**
         * @NoAdminRequired
         *
@@ -189,6 +205,31 @@ class CollaborationResourcesController extends OCSController {
                return new DataResponse($this->prepareCollection($collection));
        }
 
+       /**
+        * @NoAdminRequired
+        *
+        * @param int $collectionId
+        * @param string $collectionName
+        * @return DataResponse
+        */
+       public function renameCollection(int $collectionId, string $collectionName): DataResponse {
+               try {
+                       $collection = $this->manager->getCollection($collectionId);
+                       if (!$collection->canAccess($this->userSession->getUser())) {
+                               throw new CollectionException('Not found');
+                       }
+               } catch (CollectionException $exception) {
+                       return new DataResponse([], Http::STATUS_NOT_FOUND);
+               }
+
+               try {
+                       $collection = $this->manager->renameCollection($collectionId, $collectionName);
+               } catch (CollectionException $e) {
+                       return new DataResponse([], Http::STATUS_NOT_FOUND);
+               }
+               return new DataResponse($this->prepareCollection($collection));
+       }
+
        protected function prepareCollection(ICollection $collection): array {
                return [
                        'id' => $collection->getId(),
@@ -206,7 +247,8 @@ class CollaborationResourcesController extends OCSController {
                        'type' => $resource->getType(),
                        'id' => $resource->getId(),
                        'name' => $resource->getName(),
-                       'iconClass' => $resource->getIconClass()
+                       'iconClass' => $resource->getIconClass(),
+                       'link' => $resource->getLink(),
                ];
        }
 }
index 079f99d265f20852c3a2f27832d618bf6caa53c5..974d644000a3e05f65a133e4e42be0a44ecc5775 100644 (file)
@@ -91,8 +91,11 @@ $application->registerRoutes($this, [
                ['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'],
                ['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'],
 
+               ['root' => '/collaboration', 'name' => 'CollaborationResources#searchCollections', 'url' => '/resources/collections/search/{filter}', 'verb' => 'GET'],
                ['root' => '/collaboration', 'name' => 'CollaborationResources#listCollection', 'url' => '/resources/collections/{collectionId}', 'verb' => 'GET'],
+               ['root' => '/collaboration', 'name' => 'CollaborationResources#renameCollection', 'url' => '/resources/collections/{collectionId}', 'verb' => 'PUT'],
                ['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'],
index c11382c2f1d6f7d9d9f77c69424f9fb2638e4be9..9f083b3c676f0a86928f3a0018b38699520871f6 100644 (file)
@@ -67,6 +67,35 @@ class Manager implements IManager {
                return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']);
        }
 
+       /**
+        * @param int $id
+        * @return ICollection
+        * @throws CollectionException when the collection could not be found
+        * @since 15.0.0
+        */
+       public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array {
+               $query = $this->connection->getQueryBuilder();
+               $query->select('*')
+                       ->from('collres_collections')
+                       ->where($query->expr()->iLike('name', $query->createNamedParameter($filter, IQueryBuilder::PARAM_STR)))
+                       ->setMaxResults($limit)
+                       ->setFirstResult($start);
+               $result = $query->execute();
+               $collections = [];
+               /** TODO: this is a huge performance bottleneck */
+               while ($row = $result->fetch()) {
+                       $collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name']);
+                       if ($collection->canAccess($user)) {
+                               $collections[] = $collection;
+                       }
+               }
+               $result->closeCursor();
+
+               // TODO: call with increased first result if no matches found
+
+               return $collections;
+       }
+
        /**
         * @param string $name
         * @return ICollection
@@ -199,4 +228,19 @@ class Manager implements IManager {
 
                return '';
        }
+
+       /**
+        * @param string $name
+        * @return ICollection
+        * @since 15.0.0
+        */
+       public function renameCollection(int $id, string $name): ICollection {
+               $query = $this->connection->getQueryBuilder();
+               $query->update('collres_collections')
+                       ->set('name', $query->createNamedParameter($name))
+                       ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
+               $query->execute();
+
+               return new Collection($this, $this->connection, $id, $name);
+       }
 }
index eab101ff8e735476581f915e9d46db6b91b06278..cac13812feea849360c25fcd3638b2009a9df2f8 100644 (file)
@@ -42,6 +42,14 @@ interface IManager extends IProvider {
         */
        public function newCollection(string $name): ICollection;
 
+
+       /**
+        * @param string $name
+        * @return ICollection
+        * @since 15.0.0
+        */
+       public function renameCollection(int $id, string $name): ICollection;
+
        /**
         * @param string $type
         * @param string $id