Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
better error for duplicate
  • Loading branch information
icewind1991 committed Aug 8, 2022
commit 2d56d5321370326c541d992dec9f3a827f625af7
20 changes: 13 additions & 7 deletions lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

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;
Expand Down Expand Up @@ -138,13 +140,17 @@ public function getForUserWithFiles(string $userId): array {
}

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 {
Expand Down
28 changes: 28 additions & 0 deletions lib/Exception/AlreadyInAlbumException.php
Original file line number Diff line number Diff line change
@@ -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 {

}