mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
add Character endpoints (/anime, /manga, /seiyuu)
This commit is contained in:
parent
cf2dd515cc
commit
6205f35cb8
@ -6,7 +6,13 @@ use App\Anime;
|
||||
use App\Character;
|
||||
use App\Http\HttpHelper;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\Resources\V4\CharacterAnimeCollection;
|
||||
use App\Http\Resources\V4\CharacterMangaCollection;
|
||||
use App\Http\Resources\V4\CharacterMangaResource;
|
||||
use App\Http\Resources\V4\CharacterSeiyuuCollection;
|
||||
use App\Http\Resources\V4\PersonMangaCollection;
|
||||
use App\Http\Resources\V4\PicturesResource;
|
||||
use App\Person;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jikan\Request\Anime\AnimePicturesRequest;
|
||||
@ -101,6 +107,219 @@ class CharacterController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/characters/{id}/anime",
|
||||
* operationId="getCharacterAnime",
|
||||
* tags={"characters"},
|
||||
*
|
||||
* @OA\Response(
|
||||
* response="200",
|
||||
* description="Returns characters's anime",
|
||||
* @OA\JsonContent()
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="400",
|
||||
* description="Error: Bad request. When required parameters were not supplied.",
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function anime(Request $request, int $id)
|
||||
{
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
|
||||
if (
|
||||
$results->isEmpty()
|
||||
|| $this->isExpired($request, $results)
|
||||
) {
|
||||
$response = Character::scrape($id);
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
$meta = [
|
||||
'createdAt' => new UTCDateTime(),
|
||||
'modifiedAt' => new UTCDateTime(),
|
||||
'request_hash' => $this->fingerprint
|
||||
];
|
||||
}
|
||||
$meta['modifiedAt'] = new UTCDateTime();
|
||||
|
||||
$response = $meta + $response;
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
Character::query()
|
||||
->insert($response);
|
||||
}
|
||||
|
||||
if ($this->isExpired($request, $results)) {
|
||||
Character::query()
|
||||
->where('mal_id', $id)
|
||||
->update($response);
|
||||
}
|
||||
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
}
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
return HttpResponse::notFound($request);
|
||||
}
|
||||
|
||||
$response = (new CharacterAnimeCollection(
|
||||
$results->first()['animeography']
|
||||
))->response();
|
||||
|
||||
return $this->prepareResponse(
|
||||
$response,
|
||||
$results,
|
||||
$request
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/characters/{id}/manga",
|
||||
* operationId="getCharacterManga",
|
||||
* tags={"characters"},
|
||||
*
|
||||
* @OA\Response(
|
||||
* response="200",
|
||||
* description="Returns characters's manga",
|
||||
* @OA\JsonContent()
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="400",
|
||||
* description="Error: Bad request. When required parameters were not supplied.",
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function manga(Request $request, int $id)
|
||||
{
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
|
||||
if (
|
||||
$results->isEmpty()
|
||||
|| $this->isExpired($request, $results)
|
||||
) {
|
||||
$response = Character::scrape($id);
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
$meta = [
|
||||
'createdAt' => new UTCDateTime(),
|
||||
'modifiedAt' => new UTCDateTime(),
|
||||
'request_hash' => $this->fingerprint
|
||||
];
|
||||
}
|
||||
$meta['modifiedAt'] = new UTCDateTime();
|
||||
|
||||
$response = $meta + $response;
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
Character::query()
|
||||
->insert($response);
|
||||
}
|
||||
|
||||
if ($this->isExpired($request, $results)) {
|
||||
Character::query()
|
||||
->where('mal_id', $id)
|
||||
->update($response);
|
||||
}
|
||||
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
}
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
return HttpResponse::notFound($request);
|
||||
}
|
||||
|
||||
$response = (new CharacterMangaCollection(
|
||||
$results->first()['mangaography']
|
||||
))->response();
|
||||
|
||||
return $this->prepareResponse(
|
||||
$response,
|
||||
$results,
|
||||
$request
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/characters/{id}/seiyuu",
|
||||
* operationId="getCharacterSeiyuu",
|
||||
* tags={"characters"},
|
||||
*
|
||||
* @OA\Response(
|
||||
* response="200",
|
||||
* description="Returns characters's Seiyuu (voice actors)",
|
||||
* @OA\JsonContent()
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="400",
|
||||
* description="Error: Bad request. When required parameters were not supplied.",
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function voices(Request $request, int $id)
|
||||
{
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
|
||||
if (
|
||||
$results->isEmpty()
|
||||
|| $this->isExpired($request, $results)
|
||||
) {
|
||||
$response = Character::scrape($id);
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
$meta = [
|
||||
'createdAt' => new UTCDateTime(),
|
||||
'modifiedAt' => new UTCDateTime(),
|
||||
'request_hash' => $this->fingerprint
|
||||
];
|
||||
}
|
||||
$meta['modifiedAt'] = new UTCDateTime();
|
||||
|
||||
$response = $meta + $response;
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
Character::query()
|
||||
->insert($response);
|
||||
}
|
||||
|
||||
if ($this->isExpired($request, $results)) {
|
||||
Character::query()
|
||||
->where('mal_id', $id)
|
||||
->update($response);
|
||||
}
|
||||
|
||||
$results = Character::query()
|
||||
->where('mal_id', $id)
|
||||
->get();
|
||||
}
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
return HttpResponse::notFound($request);
|
||||
}
|
||||
|
||||
$response = (new CharacterSeiyuuCollection(
|
||||
$results->first()['voice_actors']
|
||||
))->response();
|
||||
|
||||
return $this->prepareResponse(
|
||||
$response,
|
||||
$results,
|
||||
$request
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/characters/{id}/pictures",
|
||||
|
32
app/Http/Resources/V4/CharacterAnimeCollection.php
Normal file
32
app/Http/Resources/V4/CharacterAnimeCollection.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class CharacterAnimeCollection extends ResourceCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* The resource that this resource collects.
|
||||
|
||||
*/
|
||||
public $collects = 'App\Http\Resources\V4\CharacterAnimeResource';
|
||||
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection,
|
||||
'links' => [
|
||||
'self' => 'link-value',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
28
app/Http/Resources/V4/CharacterAnimeResource.php
Normal file
28
app/Http/Resources/V4/CharacterAnimeResource.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CharacterAnimeResource extends JsonResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'role' => $this['role'],
|
||||
'anime' => [
|
||||
'mal_id' => $this['anime']['mal_id'],
|
||||
'url' => $this['anime']['url'],
|
||||
'images' => $this['anime']['images'],
|
||||
'title' => $this['anime']['title']
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
32
app/Http/Resources/V4/CharacterMangaCollection.php
Normal file
32
app/Http/Resources/V4/CharacterMangaCollection.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class CharacterMangaCollection extends ResourceCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* The resource that this resource collects.
|
||||
|
||||
*/
|
||||
public $collects = 'App\Http\Resources\V4\CharacterMangaResource';
|
||||
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection,
|
||||
'links' => [
|
||||
'self' => 'link-value',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
28
app/Http/Resources/V4/CharacterMangaResource.php
Normal file
28
app/Http/Resources/V4/CharacterMangaResource.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CharacterMangaResource extends JsonResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'role' => $this['role'],
|
||||
'manga' => [
|
||||
'mal_id' => $this['manga']['mal_id'],
|
||||
'url' => $this['manga']['url'],
|
||||
'images' => $this['manga']['images'],
|
||||
'title' => $this['manga']['title']
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -158,10 +158,7 @@ class CharacterResource extends JsonResource
|
||||
'name' => $this->name,
|
||||
'nicknames' => $this->nicknames,
|
||||
'favorites' => $this->favorites,
|
||||
'about' => $this->about,
|
||||
'animeography' => $this->animeography,
|
||||
'mangaography' => $this->mangaography,
|
||||
'voice_actors' => $this->voice_actors,
|
||||
'about' => $this->about
|
||||
];
|
||||
}
|
||||
}
|
32
app/Http/Resources/V4/CharacterSeiyuuCollection.php
Normal file
32
app/Http/Resources/V4/CharacterSeiyuuCollection.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class CharacterSeiyuuCollection extends ResourceCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* The resource that this resource collects.
|
||||
|
||||
*/
|
||||
public $collects = 'App\Http\Resources\V4\CharacterSeiyuuResource';
|
||||
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection,
|
||||
'links' => [
|
||||
'self' => 'link-value',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
28
app/Http/Resources/V4/CharacterSeiyuuResource.php
Normal file
28
app/Http/Resources/V4/CharacterSeiyuuResource.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\V4;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CharacterSeiyuuResource extends JsonResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'language' => $this['language'],
|
||||
'person' => [
|
||||
'mal_id' => $this['person']['mal_id'],
|
||||
'url' => $this['person']['url'],
|
||||
'images' => $this['person']['images'],
|
||||
'name' => $this['person']['name']
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
"flipbox/lumen-generator": "^6",
|
||||
"illuminate/redis": "^7",
|
||||
"jenssegers/mongodb": "^4.0",
|
||||
"jikan-me/jikan": "v3.0.0-alpha.21",
|
||||
"jikan-me/jikan": "v3.0.0-alpha.23",
|
||||
"jms/serializer": "^1.13",
|
||||
"laravel/lumen-framework": "^7.0",
|
||||
"league/flysystem": "^1.0",
|
||||
|
17
composer.lock
generated
17
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "31b20ce2b4d0743c4dd7b564d06f33b8",
|
||||
"content-hash": "4c26689674266ffd85bf1e7375030997",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@ -2264,16 +2264,16 @@
|
||||
},
|
||||
{
|
||||
"name": "jikan-me/jikan",
|
||||
"version": "v3.0.0-alpha.21",
|
||||
"version": "v3.0.0-alpha.23",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/jikan-me/jikan.git",
|
||||
"reference": "732dcbfddfca790b1b64c8c3d064842682f318bb"
|
||||
"reference": "4c4204489525d715b0a37db588affbf4a39751fa"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/jikan-me/jikan/zipball/732dcbfddfca790b1b64c8c3d064842682f318bb",
|
||||
"reference": "732dcbfddfca790b1b64c8c3d064842682f318bb",
|
||||
"url": "https://api.github.com/repos/jikan-me/jikan/zipball/4c4204489525d715b0a37db588affbf4a39751fa",
|
||||
"reference": "4c4204489525d715b0a37db588affbf4a39751fa",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -2311,7 +2311,7 @@
|
||||
}
|
||||
],
|
||||
"description": "Jikan is an unofficial MyAnimeList API",
|
||||
"time": "2020-08-01T07:05:45+00:00"
|
||||
"time": "2020-09-08T17:02:59+00:00"
|
||||
},
|
||||
{
|
||||
"name": "jms/metadata",
|
||||
@ -3202,12 +3202,12 @@
|
||||
"version": "v1.1.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/nrk/predis.git",
|
||||
"url": "https://github.com/predis/predis.git",
|
||||
"reference": "f0210e38881631afeafb56ab43405a92cafd9fd1"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1",
|
||||
"url": "https://api.github.com/repos/predis/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1",
|
||||
"reference": "f0210e38881631afeafb56ab43405a92cafd9fd1",
|
||||
"shasum": ""
|
||||
},
|
||||
@ -6424,6 +6424,7 @@
|
||||
"keywords": [
|
||||
"tokenizer"
|
||||
],
|
||||
"abandoned": true,
|
||||
"time": "2019-09-17T06:23:10+00:00"
|
||||
},
|
||||
{
|
||||
|
@ -147,6 +147,18 @@ $router->group(
|
||||
'uses' => 'CharacterController@main'
|
||||
]);
|
||||
|
||||
$router->get('/anime', [
|
||||
'uses' => 'CharacterController@anime'
|
||||
]);
|
||||
|
||||
$router->get('/seiyuu', [
|
||||
'uses' => 'CharacterController@voices'
|
||||
]);
|
||||
|
||||
$router->get('/manga', [
|
||||
'uses' => 'CharacterController@manga'
|
||||
]);
|
||||
|
||||
$router->get('/pictures', [
|
||||
'uses' => 'CharacterController@pictures'
|
||||
]);
|
||||
@ -169,7 +181,7 @@ $router->group(
|
||||
'uses' => 'PersonController@anime'
|
||||
]);
|
||||
|
||||
$router->get('/voices', [
|
||||
$router->get('/seiyuu', [
|
||||
'uses' => 'PersonController@voices'
|
||||
]);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user