Files
UNIT3D/app/Http/Controllers/API/TorrentRequestController.php
2025-08-30 17:30:48 -05:00

81 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Http\Resources\TorrentRequestResource;
use App\Models\TorrentRequest;
use Illuminate\Http\Request;
class RequestController extends Controller
{
/**
* Request search filter.
*/
public function filter(Request $request): \Illuminate\Http\JsonResponse
{
$query = TorrentRequest::query()
->with(['category', 'type', 'resolution', 'user', 'bounties', 'claim.user'])
->withSum('bounties as bounty', 'seedbonus');
$query->when($request->filled('name'), function ($query) use ($request) {
$searchTerm = str_replace(' ', '%', $request->input('name'));
return $query->where('name', 'LIKE', '%'.$searchTerm.'%');
})
->when($request->filled('category_id'), fn ($query) => $query->whereIntegerInRaw('category_id', (array) $request->input('category_id')))
->when($request->filled('type_id'), fn ($query) => $query->whereIntegerInRaw('type_id', (array) $request->input('type_id')))
->when($request->filled('resolution_id'), fn ($query) => $query->whereIntegerInRaw('resolution_id', (array) $request->input('resolution_id')))
->when($request->filled('tmdb'), function ($query) use ($request) {
$tmdb = $request->integer('tmdb');
return $query->where(fn ($query) => $query->where('tmdb_movie_id', '=', $tmdb)
->orWhere('tmdb_tv_id', '=', $tmdb));
})
->when($request->filled('imdb'), fn ($query) => $query->where('imdb', '=', $request->integer('imdb')))
->when($request->filled('tvdb'), fn ($query) => $query->where('tvdb', '=', $request->integer('tvdb')))
->when($request->filled('mal'), fn ($query) => $query->where('mal', '=', $request->integer('mal')))
->when($request->filled('filled'), fn ($query) => $request->boolean('filled')
? $query->whereNotNull('filled_by')
: $query->whereNull('filled_by'))
->when($request->filled('claimed'), fn ($query) => $request->boolean('claimed')
? $query->whereNotNull('claim')
: $query->whereNull('claim'));
$perPage = min($request->integer('perPage', 25), 100);
$page = max($request->integer('page', 1), 1);
$requests = $query->paginate(
perPage: $perPage,
page: $page
);
return TorrentRequestResource::collection($requests)->response();
}
/**
* View a single request.
*/
public function show(int $id): \Illuminate\Http\JsonResponse
{
$request = TorrentRequest::with(['category', 'type', 'resolution', 'user', 'bounties', 'claim.user'])
->withSum('bounties as bounty', 'seedbonus')
->findOrFail($id);
return (new TorrentRequestResource($request))->response();
}
}