2021-12-14 16:22:03 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
|
|
use App\Exceptions\NexusException;
|
2025-09-14 04:42:37 +07:00
|
|
|
use App\Models\Bookmark;
|
2021-12-14 16:22:03 +08:00
|
|
|
use App\Models\Torrent;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
|
|
class BookmarkRepository extends BaseRepository
|
|
|
|
|
{
|
|
|
|
|
public function add(User $user, $torrentId)
|
|
|
|
|
{
|
2025-09-14 04:42:37 +07:00
|
|
|
$torrent = Torrent::query()->find($torrentId);
|
|
|
|
|
if (!$torrent) {
|
|
|
|
|
throw new NexusException(nexus_trans('bookmark.torrent_not_exists', ['torrent_id' => $torrentId]));
|
|
|
|
|
}
|
2021-12-14 16:22:03 +08:00
|
|
|
$torrent->checkIsNormal();
|
|
|
|
|
$exists = $user->bookmarks()->where('torrentid', $torrentId)->exists();
|
|
|
|
|
if ($exists) {
|
2025-09-14 04:42:37 +07:00
|
|
|
throw new NexusException(nexus_trans('bookmark.torrent_already_bookmarked', ['torrent_id' => $torrentId]));
|
2021-12-14 16:22:03 +08:00
|
|
|
}
|
|
|
|
|
$result = $user->bookmarks()->create(['torrentid' => $torrentId]);
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function remove(User $user, $torrentId)
|
|
|
|
|
{
|
2025-09-14 04:42:37 +07:00
|
|
|
/**
|
|
|
|
|
* @var Bookmark $record
|
|
|
|
|
*/
|
|
|
|
|
$record = $user->bookmarks()->where('torrentid', $torrentId)->first();
|
|
|
|
|
if (!$record) {
|
|
|
|
|
throw new NexusException(nexus_trans('bookmark.torrent_has_not_been_bookmarked', ['torrent_id' => $torrentId]));
|
2021-12-14 16:22:03 +08:00
|
|
|
}
|
2025-09-14 04:42:37 +07:00
|
|
|
do_log("going to remove bookmark of torrent: $torrentId");
|
|
|
|
|
$record->delete();
|
|
|
|
|
return true;
|
2021-12-14 16:22:03 +08:00
|
|
|
}
|
|
|
|
|
}
|