Skip to content

Commit

Permalink
Implement missing endpoints and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Sep 14, 2023
1 parent 8b0a2d8 commit 4509a80
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 44 deletions.
59 changes: 43 additions & 16 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@
"Played"
],
"summary": "Get played movies of user",
"description": "Get all played movies and their watch dates.",
"parameters": [
{
"name": "username",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
}
}
}
}
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
}
}
}
}
Expand Down Expand Up @@ -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",
Expand All @@ -799,6 +819,13 @@
"properties": {
"movaryId": {
"$ref": "#/components/schemas/id"
},
"watchDates": {
"type": "array",
"items": {
"$ref": "#/components/schemas/dateNullable"
},
"required": false
}
}
}
Expand Down
75 changes: 53 additions & 22 deletions src/HttpController/Api/PlayedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,51 @@ 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();
}

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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions tests/rest/api/history.http
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]

####

Expand All @@ -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"}]

####

Expand All @@ -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"}]

####
41 changes: 38 additions & 3 deletions tests/rest/api/played.http
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]

####

Expand All @@ -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"
}
]
}
]

####

Expand All @@ -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": []
}
]

####

0 comments on commit 4509a80

Please sign in to comment.