From 3b62ffb0f9cecd656823505dde0a9e754a76f820 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Aug 2022 16:13:32 +0200 Subject: better error for duplicate Signed-off-by: Louis Chemineau --- lib/Album/AlbumMapper.php | 20 +++++++++++++------- lib/Exception/AlreadyInAlbumException.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 lib/Exception/AlreadyInAlbumException.php (limited to 'lib') 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 @@ + + * + * @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 . + * + */ + +namespace OCA\Photos\Exception; + +class AlreadyInAlbumException extends \Exception { + +} -- cgit v1.2.3