|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * NOTICE OF LICENSE. |
| 7 | + * |
| 8 | + * UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0 |
| 9 | + * The details is bundled with this project in the file LICENSE.txt. |
| 10 | + * |
| 11 | + * @project UNIT3D Community Edition |
| 12 | + * |
| 13 | + * @author HDVinnie <hdinnovations@protonmail.com> |
| 14 | + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace App\Http\Controllers\API; |
| 18 | + |
| 19 | +use App\Http\Controllers\Controller; |
| 20 | +use App\Http\Resources\TorrentRequestResource; |
| 21 | +use App\Models\TorrentRequest; |
| 22 | +use Illuminate\Http\Request; |
| 23 | + |
| 24 | +class RequestController extends Controller |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Request search filter. |
| 28 | + */ |
| 29 | + public function filter(Request $request): \Illuminate\Http\JsonResponse |
| 30 | + { |
| 31 | + $query = TorrentRequest::query() |
| 32 | + ->with(['user', 'claim.user', 'filler']) |
| 33 | + ->withSum('bounties', 'seedbonus') |
| 34 | + ->when($request->filled('name'), fn ($query) => $query->where('name', 'LIKE', '%'.str_replace(' ', '%', $request->input('name')).'%')) |
| 35 | + ->when($request->filled('category_id'), fn ($query) => $query->whereIntegerInRaw('category_id', (array) $request->input('category_id'))) |
| 36 | + ->when($request->filled('type_id'), fn ($query) => $query->whereIntegerInRaw('type_id', (array) $request->input('type_id'))) |
| 37 | + ->when($request->filled('resolution_id'), fn ($query) => $query->whereIntegerInRaw('resolution_id', (array) $request->input('resolution_id'))) |
| 38 | + ->when($request->filled('tmdb'), fn ($query) => $query->whereAny(['tmdb_movie_id', 'tmdb_tv_id'], '=', $request->integer('tmdb'))) |
| 39 | + ->when($request->filled('imdb'), fn ($query) => $query->where('imdb', '=', $request->integer('imdb'))) |
| 40 | + ->when($request->filled('tvdb'), fn ($query) => $query->where('tvdb', '=', $request->integer('tvdb'))) |
| 41 | + ->when($request->filled('mal'), fn ($query) => $query->where('mal', '=', $request->integer('mal'))) |
| 42 | + ->when($request->filled('filled'), fn ($query) => $request->boolean('filled') |
| 43 | + ? $query->whereNotNull('filled_by') |
| 44 | + : $query->whereNull('filled_by')) |
| 45 | + ->when($request->filled('claimed'), fn ($query) => $request->boolean('claimed') |
| 46 | + ? $query->whereNotNull('claim') |
| 47 | + : $query->whereNull('claim')); |
| 48 | + |
| 49 | + $perPage = min($request->integer('perPage', 25), 100); |
| 50 | + $page = max($request->integer('page', 1), 1); |
| 51 | + $requests = $query->paginate( |
| 52 | + perPage: $perPage, |
| 53 | + page: $page |
| 54 | + ); |
| 55 | + |
| 56 | + return TorrentRequestResource::collection($requests)->response(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * View a single request. |
| 61 | + */ |
| 62 | + public function show(int $id): \Illuminate\Http\JsonResponse |
| 63 | + { |
| 64 | + $request = TorrentRequest::with(['user', 'claim.user', 'filler']) |
| 65 | + ->withSum('bounties', 'seedbonus') |
| 66 | + ->findOrFail($id); |
| 67 | + |
| 68 | + return new TorrentRequestResource($request)->response(); |
| 69 | + } |
| 70 | +} |
0 commit comments