mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
Merge pull request #287 from jikan-me/fix+update/seasons
Fixes + Updates Seasons API
This commit is contained in:
commit
2d93956925
@ -4,15 +4,26 @@ namespace App\Http\Controllers\V4DB;
|
||||
|
||||
use App\Anime;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\QueryBuilder\AnimeSearchQueryBuilder;
|
||||
use App\Http\Resources\V4\AnimeCollection;
|
||||
use App\Http\Resources\V4\ResultsResource;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jikan\Helper\Constants;
|
||||
use Jikan\Model\Common\DateRange;
|
||||
use Jikan\Request\SeasonList\SeasonListRequest;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SeasonController extends Controller
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private const VALID_SEASONS = [
|
||||
'Summer',
|
||||
'Spring',
|
||||
@ -40,7 +51,14 @@ class SeasonController extends Controller
|
||||
* @OA\Schema(type="string")
|
||||
* ),
|
||||
*
|
||||
* @OA\Parameter(ref="#/components/parameters/page"),
|
||||
* @OA\Parameter(
|
||||
* name="filter",
|
||||
* description="Entry types",
|
||||
* in="query",
|
||||
* @OA\Schema(type="string",enum={"tv","movie","ova","special","ona","music"})
|
||||
* ),
|
||||
*
|
||||
* @OA\Parameter(ref="#/components/parameters/page"),
|
||||
*
|
||||
* @OA\Response(
|
||||
* response="200",
|
||||
@ -81,6 +99,7 @@ class SeasonController extends Controller
|
||||
$maxResultsPerPage = env('MAX_RESULTS_PER_PAGE', 30);
|
||||
$page = $request->get('page') ?? 1;
|
||||
$limit = $request->get('limit') ?? $maxResultsPerPage;
|
||||
$type = $request->get('filter');
|
||||
|
||||
if (!empty($limit)) {
|
||||
$limit = (int) $limit;
|
||||
@ -113,14 +132,24 @@ class SeasonController extends Controller
|
||||
list($season, $year) = $this->getSeasonStr();
|
||||
}
|
||||
|
||||
$range = $this->getSeasonRange($year, $season);
|
||||
|
||||
$results = Anime::query()
|
||||
->where('premiered', "{$season} $year")
|
||||
->whereBetween('aired.from', [$range['from'], $range['to']]);
|
||||
|
||||
if (array_key_exists(strtolower($type), AnimeSearchQueryBuilder::MAP_TYPES)) {
|
||||
$results = $results
|
||||
->where('type', AnimeSearchQueryBuilder::MAP_TYPES[$type]);
|
||||
}
|
||||
|
||||
$results = $results
|
||||
->orderBy('members', 'desc')
|
||||
->paginate(
|
||||
$limit,
|
||||
['*'],
|
||||
null,
|
||||
$page);
|
||||
$page
|
||||
);
|
||||
|
||||
$response = (new AnimeCollection(
|
||||
$results
|
||||
@ -212,7 +241,14 @@ class SeasonController extends Controller
|
||||
* operationId="getSeasonUpcoming",
|
||||
* tags={"seasons"},
|
||||
*
|
||||
* @OA\Parameter(ref="#/components/parameters/page"),
|
||||
* @OA\Parameter(
|
||||
* name="filter",
|
||||
* description="Entry types",
|
||||
* in="query",
|
||||
* @OA\Schema(type="string",enum={"tv","movie","ova","special","ona","music"})
|
||||
* ),
|
||||
*
|
||||
* @OA\Parameter(ref="#/components/parameters/page"),
|
||||
*
|
||||
* @OA\Response(
|
||||
* response="200",
|
||||
@ -233,6 +269,8 @@ class SeasonController extends Controller
|
||||
$maxResultsPerPage = env('MAX_RESULTS_PER_PAGE', 30);
|
||||
$page = $request->get('page') ?? 1;
|
||||
$limit = $request->get('limit') ?? $maxResultsPerPage;
|
||||
$type = $request->get('filter');
|
||||
|
||||
|
||||
if (!empty($limit)) {
|
||||
$limit = (int) $limit;
|
||||
@ -246,20 +284,17 @@ class SeasonController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// $nextYear = (new \DateTime(null, new \DateTimeZone('Asia/Tokyo')))
|
||||
// ->modify('+1 year')
|
||||
// ->format('Y');
|
||||
|
||||
$results = Anime::query()
|
||||
->where('status', 'Not yet aired')
|
||||
// ->where('premiered', 'like', "%{$nextYear}%")
|
||||
->orderBy('members', 'desc');
|
||||
->where('status', 'Not yet aired');
|
||||
|
||||
if (array_key_exists(strtolower($type), AnimeSearchQueryBuilder::MAP_TYPES)) {
|
||||
$results = $results
|
||||
->where('type', AnimeSearchQueryBuilder::MAP_TYPES[$type]);
|
||||
}
|
||||
|
||||
$season = 'Later';
|
||||
|
||||
|
||||
$results = $results
|
||||
->orderBy('members', 'desc')
|
||||
->paginate(
|
||||
$limit,
|
||||
['*'],
|
||||
@ -302,4 +337,25 @@ class SeasonController extends Controller
|
||||
default: throw new Exception('Could not generate seasonal string');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @param string $season
|
||||
* @return string[]
|
||||
*/
|
||||
private function getSeasonRange(int $year, string $season) : array
|
||||
{
|
||||
[$monthStart, $monthEnd] = match ($season) {
|
||||
'Winter' => [1, 3],
|
||||
'Spring' => [4, 6],
|
||||
'Summer' => [7, 9],
|
||||
'Fall' => [10, 12],
|
||||
default => throw new BadRequestException('Invalid season supplied'),
|
||||
};
|
||||
|
||||
return [
|
||||
'from' => Carbon::createFromDate($year, $monthStart, 1)->toAtomString(),
|
||||
'to' => Carbon::createFromDate($year, $monthEnd, 1)->modify('last day of this month')->toAtomString()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
645
composer.lock
generated
645
composer.lock
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user