From 1384d02c2ab47a597e0f2f1b3f034d3bcbfe1b58 Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 29 Dec 2021 01:22:08 +0500 Subject: [PATCH] add `/club/{id}/relations` --- app/Http/Controllers/V4DB/ClubController.php | 85 +++++++++ .../Resources/V4/ClubRelationsResource.php | 69 +++++++ app/Http/Resources/V4/ClubResource.php | 93 ++-------- composer.lock | 8 +- routes/web.v4.php | 4 + storage/api-docs/api-docs.json | 171 ++++++++++-------- 6 files changed, 275 insertions(+), 155 deletions(-) create mode 100644 app/Http/Resources/V4/ClubRelationsResource.php diff --git a/app/Http/Controllers/V4DB/ClubController.php b/app/Http/Controllers/V4DB/ClubController.php index 5b289c5..34d178d 100644 --- a/app/Http/Controllers/V4DB/ClubController.php +++ b/app/Http/Controllers/V4DB/ClubController.php @@ -275,4 +275,89 @@ class ClubController extends Controller $request ); } + + /** + * @OA\Get( + * path="/clubs/{id}/relations", + * operationId="getClubRelations", + * tags={"clubs"}, + * + * @OA\Parameter( + * name="id", + * in="path", + * required=true, + * @OA\Schema(type="integer") + * ), + * + * @OA\Response( + * response="200", + * description="Returns Club Relations", + * @OA\JsonContent( + * ref="#/components/schemas/club relations" + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ) + */ + public function relations(Request $request, int $id) + { + $results = Club::query() + ->where('mal_id', $id) + ->get(); + + if ( + $results->isEmpty() + || $this->isExpired($request, $results) + ) { + $response = Club::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()) { + Club::query() + ->insert($response); + } + + if ($this->isExpired($request, $results)) { + Club::query() + ->where('mal_id', $id) + ->update($response); + } + + $results = Club::query() + ->where('mal_id', $id) + ->get(); + } + + + if ($results->isEmpty()) { + return HttpResponse::notFound($request); + } + + $response = (new \App\Http\Resources\V4\ClubRelationsResource( + $results->first() + ))->response(); + + return $this->prepareResponse( + $response, + $results, + $request + ); + } } diff --git a/app/Http/Resources/V4/ClubRelationsResource.php b/app/Http/Resources/V4/ClubRelationsResource.php new file mode 100644 index 0000000..a1a76ea --- /dev/null +++ b/app/Http/Resources/V4/ClubRelationsResource.php @@ -0,0 +1,69 @@ + $this['anime'], + 'manga' => $this['manga'], + 'characters' => $this['characters'], + ]; + } +} \ No newline at end of file diff --git a/app/Http/Resources/V4/ClubResource.php b/app/Http/Resources/V4/ClubResource.php index 02bb076..2cb3267 100644 --- a/app/Http/Resources/V4/ClubResource.php +++ b/app/Http/Resources/V4/ClubResource.php @@ -26,40 +26,30 @@ class ClubResource extends JsonResource * description="MyAnimeList ID" * ), * @OA\Property( + * property="name", + * type="string", + * description="Club name" + * ), + * @OA\Property( * property="url", * type="string", - * description="MyAnimeList URL" + * description="Club URL" * ), * @OA\Property( * property="images", * type="object", - * description="Images", - * @OA\Property( - * property="jpg", - * type="object", - * description="Available images in JPG", - * @OA\Property( - * property="image_url", - * type="string", - * description="Image URL JPG (225x335)", - * ), - * ), + * ref="#/components/schemas/common images", * ), * @OA\Property( - * property="members_count", + * property="members", * type="integer", * description="Number of club members" * ), * @OA\Property( - * property="pictures_count", - * type="integer", - * description="Number of club pictures" - * ), - * @OA\Property( * property="category", * type="string", * description="Club Category", - * enum={"Actors & Artists", "Anime", "Characters", "Cities & Neighborhoods", "Companies", "Conventions", "Games", "Japan", "Manga", "Music", "Others", "Schools"} + * enum={"actors & artists", "anime", "characters", "cities & neighborhoods", "companies", "conventions", "games", "japan", "manga", "music", "others", "schools"} * ), * @OA\Property( * property="created", @@ -67,60 +57,11 @@ class ClubResource extends JsonResource * description="Date Created ISO8601" * ), * @OA\Property( - * property="type", + * property="access", * type="string", - * description="Type", + * description="Club access", * enum={"public", "private", "secret"} * ), - * @OA\Property( - * property="staff", - * type="array", - * description="Staff members", - * @OA\Items( - * type="object", - * description="Staff member", - * @OA\Property( - * property="url", - * type="string", - * description="MyAnimeList URL" - * ), - * @OA\Property( - * property="username", - * type="string", - * description="MyAnimeList Username" - * ), - * ), - * ), - * @OA\Property( - * property="anime_relations", - * type="array", - * description="Anime Relations", - * @OA\Items( - * type="object", - * description="Resource", - * ref="#/components/schemas/mal_url" - * ), - * ), - * @OA\Property( - * property="manga_relations", - * type="array", - * description="Manga Relations", - * @OA\Items( - * type="object", - * description="Resource", - * ref="#/components/schemas/mal_url" - * ), - * ), - * @OA\Property( - * property="character_relations", - * type="array", - * description="Character Relations", - * @OA\Items( - * type="object", - * description="Resource", - * ref="#/components/schemas/mal_url" - * ), - * ), * ), * ), */ @@ -129,16 +70,12 @@ class ClubResource extends JsonResource return [ 'mal_id' => $this->mal_id, 'url' => $this->url, - 'images' => $this->image, - 'title' => $this->title, - 'members_count' => $this->members_count, - 'pictures_count' => $this->pictures_count, + 'images' => $this->images, + 'name' => $this->name, + 'members' => $this->members, 'category' => $this->category, 'created' => $this->created, - 'type' => $this->type, - 'anime_relations' => $this->anime_relations, - 'manga_relations' => $this->manga_relations, - 'character_relations' => $this->character_relations, + 'access' => $this->access, ]; } } \ No newline at end of file diff --git a/composer.lock b/composer.lock index c5109f2..5fb44a3 100755 --- a/composer.lock +++ b/composer.lock @@ -2932,12 +2932,12 @@ "source": { "type": "git", "url": "https://github.com/jikan-me/jikan.git", - "reference": "da951a70678e173abc0bd29351ca814131f553b1" + "reference": "70ea41605c5437c6f4b6205e75099b26f78dc77d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/jikan-me/jikan/zipball/da951a70678e173abc0bd29351ca814131f553b1", - "reference": "da951a70678e173abc0bd29351ca814131f553b1", + "url": "https://api.github.com/repos/jikan-me/jikan/zipball/70ea41605c5437c6f4b6205e75099b26f78dc77d", + "reference": "70ea41605c5437c6f4b6205e75099b26f78dc77d", "shasum": "" }, "require": { @@ -2986,7 +2986,7 @@ "type": "patreon" } ], - "time": "2021-12-27T20:23:43+00:00" + "time": "2021-12-28T19:59:27+00:00" }, { "name": "jms/metadata", diff --git a/routes/web.v4.php b/routes/web.v4.php index f307a2e..e4375f5 100755 --- a/routes/web.v4.php +++ b/routes/web.v4.php @@ -398,6 +398,10 @@ $router->group( $router->get('/{id:[0-9]+}/staff', [ 'uses' => 'ClubController@staff' ]); + + $router->get('/{id:[0-9]+}/relations', [ + 'uses' => 'ClubController@relations' + ]); } ); diff --git a/storage/api-docs/api-docs.json b/storage/api-docs/api-docs.json index 28bdbbe..5f01ffe 100644 --- a/storage/api-docs/api-docs.json +++ b/storage/api-docs/api-docs.json @@ -812,6 +812,39 @@ } } }, + "/clubs/{id}/relations": { + "get": { + "tags": [ + "clubs" + ], + "operationId": "getClubRelations", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + } + ], + "responses": { + "200": { + "description": "Returns Club Relations", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/club relations" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/genres/anime": { "get": { "tags": [ @@ -4261,6 +4294,50 @@ } ] }, + "club relations": { + "description": "Club Relations", + "properties": { + "data": { + "properties": { + "anime": { + "type": "array", + "items": { + "properties": { + "": { + "$ref": "#/components/schemas/mal_url" + } + }, + "type": "object" + } + }, + "manga": { + "type": "array", + "items": { + "properties": { + "": { + "$ref": "#/components/schemas/mal_url" + } + }, + "type": "object" + } + }, + "characters": { + "type": "array", + "items": { + "properties": { + "": { + "$ref": "#/components/schemas/mal_url" + } + }, + "type": "object" + } + } + }, + "type": "object" + } + }, + "type": "object" + }, "club": { "description": "Club Resource", "properties": { @@ -4270,103 +4347,51 @@ "description": "MyAnimeList ID", "type": "integer" }, + "name": { + "description": "Club name", + "type": "string" + }, "url": { - "description": "MyAnimeList URL", + "description": "Club URL", "type": "string" }, "images": { - "description": "Images", - "properties": { - "jpg": { - "description": "Available images in JPG", - "properties": { - "image_url": { - "description": "Image URL JPG (225x335)", - "type": "string" - } - }, - "type": "object" - } - }, - "type": "object" + "$ref": "#/components/schemas/common images" }, - "members_count": { + "members": { "description": "Number of club members", "type": "integer" }, - "pictures_count": { - "description": "Number of club pictures", - "type": "integer" - }, "category": { "description": "Club Category", "type": "string", "enum": [ - "Actors & Artists", - "Anime", - "Characters", - "Cities & Neighborhoods", - "Companies", - "Conventions", - "Games", - "Japan", - "Manga", - "Music", - "Others", - "Schools" + "actors & artists", + "anime", + "characters", + "cities & neighborhoods", + "companies", + "conventions", + "games", + "japan", + "manga", + "music", + "others", + "schools" ] }, "created": { "description": "Date Created ISO8601", "type": "string" }, - "type": { - "description": "Type", + "access": { + "description": "Club access", "type": "string", "enum": [ "public", "private", "secret" ] - }, - "staff": { - "description": "Staff members", - "type": "array", - "items": { - "description": "Staff member", - "properties": { - "url": { - "description": "MyAnimeList URL", - "type": "string" - }, - "username": { - "description": "MyAnimeList Username", - "type": "string" - } - }, - "type": "object" - } - }, - "anime_relations": { - "description": "Anime Relations", - "type": "array", - "items": { - "$ref": "#/components/schemas/mal_url" - } - }, - "manga_relations": { - "description": "Manga Relations", - "type": "array", - "items": { - "$ref": "#/components/schemas/mal_url" - } - }, - "character_relations": { - "description": "Character Relations", - "type": "array", - "items": { - "$ref": "#/components/schemas/mal_url" - } } }, "type": "object"