mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
add v3 Person backwards compatibility
This commit is contained in:
parent
6a0655ff2d
commit
93b7e57b8d
@ -2,29 +2,113 @@
|
||||
|
||||
namespace App\Http\Controllers\V3;
|
||||
|
||||
use App\Http\HttpHelper;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Person;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jikan\Exception\BadResponseException;
|
||||
use Jikan\Request\Manga\MangaPicturesRequest;
|
||||
use Jikan\Request\Person\PersonRequest;
|
||||
use Jikan\Request\Person\PersonPicturesRequest;
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
|
||||
class PersonController extends Controller
|
||||
{
|
||||
public function main(int $id)
|
||||
public function main(Request $request, int $id)
|
||||
{
|
||||
if ($id < 1) { // MAL INCONSISTENCY: doesn't return 404, it returns an error message with HTTP 200 instead
|
||||
throw new BadResponseException(null, 404);
|
||||
$results = Person::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
|
||||
$isExpired = $this->isExpired($request, $results);
|
||||
$expiryTtl = $this->getExpiryTtl($request, $results);
|
||||
$cached = true;
|
||||
|
||||
if (
|
||||
$results->isEmpty()
|
||||
|| $isExpired
|
||||
) {
|
||||
$response = Person::scrape($id);
|
||||
|
||||
if (HttpHelper::hasError($response)) {
|
||||
return HttpResponse::notFound($request);
|
||||
}
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
$meta = [
|
||||
'createdAt' => new UTCDateTime(),
|
||||
'modifiedAt' => new UTCDateTime(),
|
||||
'request_hash' => $this->fingerprint
|
||||
];
|
||||
}
|
||||
$meta['modifiedAt'] = new UTCDateTime();
|
||||
|
||||
$response = $meta + $response;
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
Person::query()
|
||||
->insert($response);
|
||||
}
|
||||
|
||||
if ($this->isExpired($request, $results)) {
|
||||
Person::query()
|
||||
->where('request_hash', $this->fingerprint)
|
||||
->update($response);
|
||||
}
|
||||
|
||||
$results = Person::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
}
|
||||
|
||||
$person = $this->jikan->getPerson(new PersonRequest($id));
|
||||
return response($this->serializer->serialize($person, 'json'));
|
||||
if ($results->isEmpty()) {
|
||||
return HttpResponse::notFound($request);
|
||||
}
|
||||
|
||||
$response = (new \App\Http\Resources\V3\PersonResource(
|
||||
$results->first()
|
||||
))->toArray($request);
|
||||
|
||||
return $this->prepareResponse(
|
||||
response(
|
||||
$this->bcMeta($response, $this->fingerprint, $cached, $expiryTtl)
|
||||
),
|
||||
$results,
|
||||
$request
|
||||
);
|
||||
}
|
||||
|
||||
public function pictures(int $id)
|
||||
public function pictures(Request $request, int $id)
|
||||
{
|
||||
if ($id < 1) { // MAL INCONSISTENCY: doesn't return 404, it returns an error message with HTTP 200 instead
|
||||
throw new BadResponseException(null, 404);
|
||||
$results = DB::table($this->getRouteTable($request))
|
||||
->where('request_hash', $this->fingerprint)
|
||||
->get();
|
||||
|
||||
$isExpired = $this->isExpired($request, $results);
|
||||
$expiryTtl = $this->getExpiryTtl($request, $results);
|
||||
$cached = true;
|
||||
|
||||
if (
|
||||
$results->isEmpty()
|
||||
|| $isExpired
|
||||
) {
|
||||
$person = ['pictures' => $this->jikan->getPersonPictures(new PersonPicturesRequest($id))];
|
||||
$response = \json_decode($this->serializer->serialize($person, 'json'), true);
|
||||
|
||||
$results = $this->updateCache($request, $results, $response);
|
||||
}
|
||||
|
||||
$person = ['pictures' => $this->jikan->getPersonPictures(new PersonPicturesRequest($id))];
|
||||
return response($this->serializer->serialize($person, 'json'));
|
||||
$response = (new \App\Http\Resources\V3\PicturesResource(
|
||||
$results->first()
|
||||
))->toArray($request);
|
||||
|
||||
return $this->prepareResponse(
|
||||
response(
|
||||
$this->bcMeta($response, $this->fingerprint, $cached, $expiryTtl)
|
||||
),
|
||||
$results,
|
||||
$request
|
||||
);
|
||||
}
|
||||
}
|
||||
|
34
app/Http/Resources/V3/PersonResource.php
Normal file
34
app/Http/Resources/V3/PersonResource.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\V3;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class PersonResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'mal_id' => $this->mal_id,
|
||||
'url' => $this->url,
|
||||
'website_url' => $this->website_url,
|
||||
'image_url' => $this->images['jpg']['image_url'],
|
||||
'name' => $this->name,
|
||||
'given_name' => $this->given_name,
|
||||
'family_name' => $this->family_name,
|
||||
'alternate_names' => $this->alternate_names,
|
||||
'birthday' => $this->birthday,
|
||||
'member_favorites' => $this->favorites,
|
||||
'about' => $this->about,
|
||||
'voice_acting_roles' => $this->voice_acting_roles,
|
||||
'anime_staff_positions' => $this->anime_staff_positions,
|
||||
'published_manga' => $this->published_manga,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user