diff options
author | Robin Appelman <robin@icewind.nl> | 2022-08-08 17:13:32 +0300 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2022-08-22 21:03:52 +0300 |
commit | 3b62ffb0f9cecd656823505dde0a9e754a76f820 (patch) | |
tree | 983c2405f6320e4fc463ac24dd8fe19ab82a7cd3 /lib | |
parent | 9c68a0fbfe8878419038e47aaebb8283cdab14ab (diff) |
better error for duplicate
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Album/AlbumMapper.php | 20 | ||||
-rw-r--r-- | lib/Exception/AlreadyInAlbumException.php | 28 |
2 files changed, 41 insertions, 7 deletions
diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index 6d119322..1084b550 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace OCA\Photos\Album; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; +use OCA\Photos\Exception\AlreadyInAlbumException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\IMimeTypeLoader; use OCP\IDBConnection; @@ -138,13 +140,17 @@ class AlbumMapper { } public function addFile(int $albumId, int $fileId): void { - $query = $this->connection->getQueryBuilder(); - $query->insert("photos_albums_files") - ->values([ - "album_id" => $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT), - "file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT) - ]); - $query->executeStatement(); + try { + $query = $this->connection->getQueryBuilder(); + $query->insert("photos_albums_files") + ->values([ + "album_id" => $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT), + "file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT) + ]); + $query->executeStatement(); + } catch (UniqueConstraintViolationException $e) { + throw new AlreadyInAlbumException("File already in album", 0, $e); + } } public function removeFile(int $albumId, int $fileId): void { diff --git a/lib/Exception/AlreadyInAlbumException.php b/lib/Exception/AlreadyInAlbumException.php new file mode 100644 index 00000000..9c7d7fe3 --- /dev/null +++ b/lib/Exception/AlreadyInAlbumException.php @@ -0,0 +1,28 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Photos\Exception; + +class AlreadyInAlbumException extends \Exception { + +} |