mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 04:20:49 +08:00
API bookmark
This commit is contained in:
@@ -7,4 +7,6 @@ enum RoutePermissionEnum: string {
|
||||
case TORRENT_VIEW = 'torrent:view';
|
||||
case TORRENT_UPLOAD = 'torrent:upload';
|
||||
case USER_VIEW = "user:view";
|
||||
case BOOKMARK_STORE = "bookmark:store";
|
||||
case BOOKMARK_DELETE = "bookmark:delete";
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\BookmarkResource;
|
||||
use App\Http\Resources\TorrentResource;
|
||||
use App\Models\Torrent;
|
||||
use App\Repositories\BookmarkRepository;
|
||||
@@ -25,7 +26,7 @@ class BookmarkController extends Controller
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
@@ -33,7 +34,8 @@ class BookmarkController extends Controller
|
||||
'torrent_id' => 'required|integer',
|
||||
]);
|
||||
$result = $this->repository->add(Auth::user(), $request->torrent_id);
|
||||
return $this->success($result->toArray(), nexus_trans('bookmark.actions.store_success'));
|
||||
$resource = new BookmarkResource($result);
|
||||
return $this->success($resource, nexus_trans('bookmark.actions.store_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,12 +64,15 @@ class BookmarkController extends Controller
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$result = $this->repository->remove(Auth::user(), $id);
|
||||
return $this->success($result, nexus_trans('bookmark.actions.delete_success'));
|
||||
$request->validate([
|
||||
'torrent_id' => 'required|integer',
|
||||
]);
|
||||
$result = $this->repository->remove(Auth::user(), $request->torrent_id);
|
||||
return $this->success(true, nexus_trans('bookmark.actions.delete_success'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,7 @@ class Controller extends BaseController
|
||||
protected ?array $extraSettingNames = null;
|
||||
|
||||
/**
|
||||
* 返回成功信息,这里是旧方法,大部分情况下 $data 已经是 JsonResource
|
||||
* 但很多地方有使用,历史原因保留不动,尽量使用 successJsonResource
|
||||
* 返回成功信息
|
||||
*
|
||||
* @param $data
|
||||
* @param $msg
|
||||
@@ -44,6 +43,7 @@ class Controller extends BaseController
|
||||
/**
|
||||
* 返回成功信息,对于不是 JsonResource 的数据,进行包装。返回的数据在 data.data 中
|
||||
*
|
||||
* @deprecated 没有必要,已经在 api() 中添加 data 包裹,使用 success() 即可
|
||||
* @param $data
|
||||
* @param $msg
|
||||
* @return array
|
||||
|
||||
@@ -37,7 +37,7 @@ class TokenController extends Controller
|
||||
$newAccessToken = $user->createToken($request->name, $request->permissions);
|
||||
$tokenText = $newAccessToken->plainTextToken;
|
||||
$msg = nexus_trans("token.create_success_tip", ['token' => $tokenText]);
|
||||
return $this->successJsonResource(['token' => $tokenText], $msg);
|
||||
return $this->success(['token' => $tokenText], $msg);
|
||||
} catch (\Exception $exception) {
|
||||
return $this->fail(false, $exception->getMessage());
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ class Bookmark extends NexusModel
|
||||
|
||||
protected $fillable = ['userid', 'torrentid'];
|
||||
|
||||
const FILTER_IGNORE = '0';
|
||||
const FILTER_INCLUDE = '1';
|
||||
const FILTER_EXCLUDE = '2';
|
||||
|
||||
public function torrent()
|
||||
{
|
||||
return $this->belongsTo(Torrent::class, 'torrentid');
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Models\Bookmark;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
|
||||
@@ -10,11 +11,14 @@ class BookmarkRepository extends BaseRepository
|
||||
{
|
||||
public function add(User $user, $torrentId)
|
||||
{
|
||||
$torrent = Torrent::query()->findOrFail($torrentId);
|
||||
$torrent = Torrent::query()->find($torrentId);
|
||||
if (!$torrent) {
|
||||
throw new NexusException(nexus_trans('bookmark.torrent_not_exists', ['torrent_id' => $torrentId]));
|
||||
}
|
||||
$torrent->checkIsNormal();
|
||||
$exists = $user->bookmarks()->where('torrentid', $torrentId)->exists();
|
||||
if ($exists) {
|
||||
throw new NexusException("torrent: $torrentId already bookmarked.");
|
||||
throw new NexusException(nexus_trans('bookmark.torrent_already_bookmarked', ['torrent_id' => $torrentId]));
|
||||
}
|
||||
$result = $user->bookmarks()->create(['torrentid' => $torrentId]);
|
||||
return $result;
|
||||
@@ -22,12 +26,15 @@ class BookmarkRepository extends BaseRepository
|
||||
|
||||
public function remove(User $user, $torrentId)
|
||||
{
|
||||
$torrent = Torrent::query()->findOrFail($torrentId);
|
||||
$exists = $user->bookmarks()->where('torrentid', $torrentId)->exists();
|
||||
if (!$exists) {
|
||||
throw new NexusException("torrent: $torrentId has not been bookmarked.");
|
||||
/**
|
||||
* @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]));
|
||||
}
|
||||
$result = $user->bookmarks()->where('torrentid', $torrentId)->delete();
|
||||
return $result;
|
||||
do_log("going to remove bookmark of torrent: $torrentId");
|
||||
$record->delete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ class TokenRepository extends BaseRepository
|
||||
RoutePermissionEnum::TORRENT_VIEW->value,
|
||||
RoutePermissionEnum::TORRENT_UPLOAD->value,
|
||||
RoutePermissionEnum::USER_VIEW->value,
|
||||
RoutePermissionEnum::BOOKMARK_STORE->value,
|
||||
RoutePermissionEnum::BOOKMARK_DELETE->value,
|
||||
];
|
||||
|
||||
public static function listUserTokenPermissions(bool $format = true): array
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Exceptions\InsufficientPermissionException;
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Http\Resources\TorrentResource;
|
||||
use App\Models\AudioCodec;
|
||||
use App\Models\Bookmark;
|
||||
use App\Models\Category;
|
||||
use App\Models\Claim;
|
||||
use App\Models\Codec;
|
||||
@@ -36,6 +37,7 @@ use Carbon\Carbon;
|
||||
use Elasticsearch\Endpoints\Search;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -96,7 +98,8 @@ class TorrentRepository extends BaseRepository
|
||||
];
|
||||
$allowFilters = [
|
||||
'title', 'category', 'source', 'medium', 'codec', 'audiocodec', 'standard', 'processing', 'team',
|
||||
'owner', 'visible', 'added', 'size', 'sp_state', 'leechers', 'seeders', 'times_completed'
|
||||
'owner', 'visible', 'added', 'size', 'sp_state', 'leechers', 'seeders', 'times_completed',
|
||||
'bookmark'
|
||||
];
|
||||
$allowSorts = ['id', 'comments', 'size', 'seeders', 'leechers', 'times_completed'];
|
||||
$apiQueryBuilder = ApiQueryBuilder::for(TorrentResource::NAME, $query, $request)
|
||||
@@ -114,6 +117,18 @@ class TorrentRepository extends BaseRepository
|
||||
});
|
||||
}
|
||||
})
|
||||
->registerCustomFilter('bookmark', function (Builder $query, Request $request) use ($user) {
|
||||
$filterBookmark = $request->input(ApiQueryBuilder::PARAM_NAME_FILTER.".bookmark");
|
||||
if ($filterBookmark === Bookmark::FILTER_INCLUDE) {
|
||||
$query->whereHas("bookmarks", function (Builder $query) use ($user) {
|
||||
$query->where("userid", $user->id);
|
||||
});
|
||||
} elseif ($filterBookmark === Bookmark::FILTER_EXCLUDE) {
|
||||
$query->whereDoesntHave("bookmarks", function (Builder $query) use ($user) {
|
||||
$query->where("userid", $user->id);
|
||||
});
|
||||
}
|
||||
})
|
||||
;
|
||||
$query = $apiQueryBuilder->build();
|
||||
if (!$apiQueryBuilder->hasSort()) {
|
||||
|
||||
Reference in New Issue
Block a user