From 4509a80b022e5d3d10c4469c147762af4b88b35f Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Thu, 14 Sep 2023 19:22:39 +0200 Subject: [PATCH] Implement missing endpoints and add docs --- docs/openapi.json | 59 +++++++++++----- src/HttpController/Api/PlayedController.php | 75 +++++++++++++++------ tests/rest/api/history.http | 6 +- tests/rest/api/played.http | 41 ++++++++++- 4 files changed, 137 insertions(+), 44 deletions(-) diff --git a/docs/openapi.json b/docs/openapi.json index ba843924..d1dbd1ff 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -513,6 +513,7 @@ "Played" ], "summary": "Get played movies of user", + "description": "Get all played movies and their watch dates.", "parameters": [ { "name": "username", @@ -660,6 +661,7 @@ "Played" ], "summary": "Add movie plays to user", + "description": "Create or update the provided watch dates for the specified movies.", "parameters": [ { "name": "username", @@ -682,14 +684,22 @@ "movaryId": { "$ref": "#/components/schemas/id" }, - "watchedAt": { - "$ref": "#/components/schemas/dateNullable" - }, - "plays": { - "$ref": "#/components/schemas/playsOptional" - }, - "comment": { - "$ref": "#/components/schemas/commentOptional" + "watchDates": { + "type": "array", + "items": { + "type": "object", + "properties": { + "watchedAt": { + "$ref": "#/components/schemas/dateNullable" + }, + "plays": { + "$ref": "#/components/schemas/playsOptional" + }, + "comment": { + "$ref": "#/components/schemas/commentOptional" + } + } + } } } } @@ -719,6 +729,7 @@ "Played" ], "summary": "Replace movie plays for user", + "description": "Create or replace the provided watch dates for the specified movies.", "parameters": [ { "name": "username", @@ -741,14 +752,22 @@ "movaryId": { "$ref": "#/components/schemas/id" }, - "watchedAt": { - "$ref": "#/components/schemas/dateNullable" - }, - "plays": { - "$ref": "#/components/schemas/plays" - }, - "comment": { - "$ref": "#/components/schemas/comment" + "watchDates": { + "type": "array", + "items": { + "type": "object", + "properties": { + "watchedAt": { + "$ref": "#/components/schemas/dateNullable" + }, + "plays": { + "$ref": "#/components/schemas/plays" + }, + "comment": { + "$ref": "#/components/schemas/comment" + } + } + } } } } @@ -778,6 +797,7 @@ "Played" ], "summary": "Delete movie plays from user", + "description": "Delete all watch dates of specified movies if no specific watch dates are provided.", "parameters": [ { "name": "username", @@ -799,6 +819,13 @@ "properties": { "movaryId": { "$ref": "#/components/schemas/id" + }, + "watchDates": { + "type": "array", + "items": { + "$ref": "#/components/schemas/dateNullable" + }, + "required": false } } } diff --git a/src/HttpController/Api/PlayedController.php b/src/HttpController/Api/PlayedController.php index 54ef2b5a..1744a7f5 100644 --- a/src/HttpController/Api/PlayedController.php +++ b/src/HttpController/Api/PlayedController.php @@ -26,9 +26,22 @@ public function __construct( public function addToPlayed(Request $request) : Response { $userId = $this->requestMapper->mapUsernameFromRoute($request)->getId(); - $watchlistAdditions = Json::decode($request->getBody()); - - // TODO + $playedAdditions = Json::decode($request->getBody()); + + foreach ($playedAdditions as $playAddition) { + $movieId = (int)$playAddition['movaryId']; + $watchDates = $playAddition['watchDates'] ?? []; + + foreach ($watchDates as $watchDate) { + $this->movieApi->addPlaysForMovieOnDate( + $movieId, + $userId, + $watchDate['watchedAt'] !== null ? Date::createFromString($watchDate['watchedAt']) : null, + $watchDate['plays'] ?? 1, + $watchDate['comment'] ?? null, + ); + } + } return Response::createNoContent(); } @@ -36,13 +49,28 @@ public function addToPlayed(Request $request) : Response public function deleteFromPlayed(Request $request) : Response { $userId = $this->requestMapper->mapUsernameFromRoute($request)->getId(); - $historyAdditions = Json::decode($request->getBody()); - - foreach ($historyAdditions as $historyAddition) { - $this->movieApi->deleteHistoryById( - (int)$historyAddition['movaryId'], - $userId, - ); + $playedDeletions = Json::decode($request->getBody()); + + foreach ($playedDeletions as $playedDeletion) { + $movieId = (int)$playedDeletion['movaryId']; + $watchDates = $playedDeletion['watchDates'] ?? []; + + if (count($watchDates) === 0) { + $this->movieApi->deleteHistoryById( + $movieId, + $userId, + ); + + continue; + } + + foreach ($watchDates as $date) { + $this->movieApi->deleteHistoryByIdAndDate( + $movieId, + $userId, + empty($date) === true ? null : Date::createFromString($date), + ); + } } return Response::createNoContent(); @@ -92,18 +120,21 @@ public function getPlayed(Request $request) : Response public function updatePlayed(Request $request) : Response { $userId = $this->requestMapper->mapUsernameFromRoute($request)->getId(); - $historyAdditions = Json::decode($request->getBody()); - - // TODO - - foreach ($historyAdditions as $historyAddition) { - $this->movieApi->replaceHistoryForMovieByDate( - (int)$historyAddition['movaryId'], - $userId, - isset($historyAddition['watchedAt']) === true ? Date::createFromString($historyAddition['watchedAt']) : null, - $historyAddition['plays'], - $historyAddition['comment'], - ); + $playedUpdates = Json::decode($request->getBody()); + + foreach ($playedUpdates as $playedUpdate) { + $movieId = (int)$playedUpdate['movaryId']; + $watchDates = $playedUpdate['watchDates'] ?? []; + + foreach ($watchDates as $watchDate) { + $this->movieApi->replaceHistoryForMovieByDate( + $movieId, + $userId, + $watchDate['watchedAt'] !== null ? Date::createFromString($watchDate['watchedAt']) : null, + $watchDate['plays'], + $watchDate['comment'], + ); + } } return Response::createNoContent(); diff --git a/tests/rest/api/history.http b/tests/rest/api/history.http index 1c7ea76b..3253c379 100644 --- a/tests/rest/api/history.http +++ b/tests/rest/api/history.http @@ -12,7 +12,7 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -[{"movieId" : 1, "watchedAt" : "2011-05-06", "plays" : 1, "comment" : "comment"}] +[{"movaryId" : 1, "watchedAt" : "2011-05-06", "plays" : 1, "comment" : "comment"}] #### @@ -22,7 +22,7 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -[{"movieId" : 1, "watchedAt" : "2011-05-06"}] +[{"movaryId" : 1, "watchedAt" : "2011-05-06"}] #### @@ -32,6 +32,6 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -[{"movieId" : 1, "watchedAt" : "2011-05-06"}] +[{"movaryId" : 1, "watchedAt" : "2011-05-06"}] #### diff --git a/tests/rest/api/played.http b/tests/rest/api/played.http index 642e3ad5..9177594a 100644 --- a/tests/rest/api/played.http +++ b/tests/rest/api/played.http @@ -12,7 +12,23 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -//[{"movieId" : 1, "watchedAt" : "2011-05-06", "plays" : 1, "comment" : "comment"}] +[ + { + "movaryId": 1, + "watchDates": [ + { + "watchedAt": null, + "plays": 2, + "comment": "Test comment" + }, + { + "watchedAt": "2024-05-06", + "plays": 2, + "comment": "Test comment" + } + ] + } +] #### @@ -22,7 +38,21 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -//[{"movieId" : 1, "watchedAt" : "2011-05-06"}] +[ + { + "movaryId": 1, + "watchDates": [ + { + "watchedAt": null + }, + { + "watchedAt": "2024-05-06", + "plays": 2, + "comment": "Test comment" + } + ] + } +] #### @@ -32,6 +62,11 @@ Cache-Control: no-cache Content-Type: application/json X-Auth-Token: {{xAuthToken}} -//[{"movieId" : 1, "watchedAt" : "2011-05-06"}] +[ + { + "movaryId": 1, + "watchDates": [] + } +] ####