From 9487ddc9100d3c5674afea7691ea69933941adf2 Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Sat, 2 Nov 2024 20:14:52 +0100 Subject: [PATCH] improved organization folder service: added function to find by id, return model entity after creation, added function to remove --- lib/Errors/OrganizationFolderNotFound.php | 9 ++++++ lib/Errors/OrganizationNotFound.php | 2 +- lib/Service/OrganizationFolderService.php | 39 ++++++++++++++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 lib/Errors/OrganizationFolderNotFound.php diff --git a/lib/Errors/OrganizationFolderNotFound.php b/lib/Errors/OrganizationFolderNotFound.php new file mode 100644 index 0000000..2fcdb0e --- /dev/null +++ b/lib/Errors/OrganizationFolderNotFound.php @@ -0,0 +1,9 @@ + $id]); + } +} \ No newline at end of file diff --git a/lib/Errors/OrganizationNotFound.php b/lib/Errors/OrganizationNotFound.php index 5d1d376..48cdb68 100644 --- a/lib/Errors/OrganizationNotFound.php +++ b/lib/Errors/OrganizationNotFound.php @@ -4,6 +4,6 @@ namespace OCA\OrganizationFolders\Errors; class OrganizationNotFound extends NotFoundException { public function __construct($provider, $id) { - parent::__construct(OCA\OrganizationFolders\Model\Organization::class, ["provider" => $provider, "id" => $id]); + parent::__construct(\OCA\OrganizationFolders\Model\Organization::class, ["provider" => $provider, "id" => $id]); } } \ No newline at end of file diff --git a/lib/Service/OrganizationFolderService.php b/lib/Service/OrganizationFolderService.php index 865cf5b..200d436 100644 --- a/lib/Service/OrganizationFolderService.php +++ b/lib/Service/OrganizationFolderService.php @@ -10,6 +10,7 @@ use OCP\IDBConnection; use OCA\GroupFolders\Folder\FolderManager; use OCA\GroupfolderTags\Service\TagService; +use OCA\OrganizationFolders\Errors\OrganizationFolderNotFound; use OCA\OrganizationFolders\Model\OrganizationFolder; use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager; @@ -44,8 +45,26 @@ class OrganizationFolderService { return $result; } - public function create(string $name, int $quota, ?string $organizationProvider = null,?int $organizationId = null) { - $this->atomic(function () use ($name, $quota) { + public function find(int $id): OrganizationFolder { + $groupfolder = $this->tagService->findGroupfolderWithTags($id,[ + ["key" => "organization_folder"], + ], ["organization_provider", "organization_id"]); + + if(is_null($groupfolder)) { + throw new OrganizationFolderNotFound($id); + } + + return new OrganizationFolder( + id: $groupfolder["id"], + name: $groupfolder["mount_point"], + quota: $groupfolder["quota"], + organizationProvider: $groupfolder["organization_provider"], + organizationId: $groupfolder["organization_id"], + ); + } + + public function create(string $name, int $quota, ?string $organizationProvider = null,?int $organizationId = null): OrganizationFolder { + return $this->atomic(function () use ($name, $quota, $organizationProvider, $organizationId) { $groupfolderId = $this->folderManager->createFolder($name); $this->folderManager->setFolderQuota($groupfolderId, $quota); $this->folderManager->setFolderACL($groupfolderId, true); @@ -58,9 +77,16 @@ class OrganizationFolderService { $this->tagService->update($groupfolderId, "organization_provider", $organizationProvider); $this->tagService->update($groupfolderId, "organization_id", $organization->getId()); } - - // TODO: return Model object + $organizationFolder = new OrganizationFolder( + id: $groupfolderId, + name: $name, + quota: $quota, + organizationProvider: $organizationProvider, + organizationId: $organizationId, + ); + + return $organizationFolder; }, $this->db); } @@ -68,4 +94,9 @@ class OrganizationFolderService { } + public function remove($id): void { + $organizationFolder = $this->find($id); + $this->folderManager->removeFolder($organizationFolder->getId()); + } + }