Skip to content

Commit

Permalink
Implement position in general watch date logic
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Nov 12, 2023
1 parent 36c4d35 commit 1bc1e41
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 47 deletions.
18 changes: 18 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
},
"comment": {
"$ref": "#/components/schemas/commentOptional"
},
"position": {
"$ref": "#/components/schemas/positionOptional"
}
}
}
Expand Down Expand Up @@ -211,6 +214,9 @@
},
"comment": {
"$ref": "#/components/schemas/comment"
},
"position": {
"$ref": "#/components/schemas/position"
}
}
}
Expand Down Expand Up @@ -1199,6 +1205,18 @@
"default": null,
"required": false
},
"position": {
"type": "number",
"example": 1,
"nullable": false
},
"positionOptional": {
"type": "number",
"example": 1,
"nullable": true,
"default": 1,
"required": false
},
"date": {
"type": "string",
"example": "2023-07-02"
Expand Down
3 changes: 3 additions & 0 deletions public/js/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function loadWatchDateModal(watchDateListElement) {
document.getElementById('editWatchDateModalInput').value = watchDateListElement.dataset.watchDate;
document.getElementById('editWatchDateModalPlaysInput').value = watchDateListElement.dataset.plays;
document.getElementById('editWatchDateModalCommentInput').value = watchDateListElement.dataset.comment;
document.getElementById('editWatchDateModalPositionInput').value = watchDateListElement.dataset.position;

document.getElementById('originalWatchDate').value = watchDateListElement.dataset.watchDate;
document.getElementById('originalWatchDatePlays').value = watchDateListElement.dataset.plays;
Expand All @@ -94,6 +95,7 @@ function editWatchDate() {

const newWatchDate = document.getElementById('editWatchDateModalInput').value;
const newWatchDatePlays = document.getElementById('editWatchDateModalPlaysInput').value;
const newPositionPlays = document.getElementById('editWatchDateModalPositionInput').value;
const comment = document.getElementById('editWatchDateModalCommentInput').value;

const apiUrl = '/users/' + getRouteUsername() + '/movies/' + getMovieId() + '/history'
Expand All @@ -106,6 +108,7 @@ function editWatchDate() {
'originalWatchDate': originalWatchDate,
'plays': newWatchDatePlays,
'comment': comment,
'position': newPositionPlays,
'dateFormat': document.getElementById('dateFormatPhp').value
}),
success: function (data, textStatus, xhr) {
Expand Down
68 changes: 34 additions & 34 deletions src/Domain/Movie/History/MovieHistoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function __construct(
) {
}

public function create(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?string $comment = null) : void
public function create(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?int $position = null, ?string $comment = null) : void
{
$this->repository->create($movieId, $userId, $watchedAt, $plays, $comment);
$this->repository->create($movieId, $userId, $watchedAt, $plays, $comment, $position ?? 1);

if ($this->userApi->fetchUser($userId)->hasJellyfinSyncEnabled() === false) {
return;
Expand Down Expand Up @@ -241,6 +241,36 @@ public function fetchMovieIdsWithWatchDatesByUserId(int $userId) : array
return $this->movieRepository->fetchMovieIdsWithWatchDatesByUserId($userId);
}

public function fetchPlayedMoviesPaginated(
int $userId,
int $limit,
int $page,
?string $searchTerm = null,
string $sortBy = 'title',
?SortOrder $sortOrder = null,
?Year $releaseYear = null,
?string $language = null,
?string $genre = null,
) : array {
if ($sortOrder === null) {
$sortOrder = SortOrder::createAsc();
}

$movies = $this->movieRepository->fetchUniqueWatchedMoviesPaginated(
$userId,
$limit,
$page,
$searchTerm,
$sortBy,
$sortOrder,
$releaseYear,
$language,
$genre,
);

return $this->urlGenerator->replacePosterPathWithImageSrcUrl($movies);
}

public function fetchTmdbIdsToLastWatchDatesMap(int $userId, array $tmdbIds) : array
{
$map = [];
Expand Down Expand Up @@ -386,36 +416,6 @@ public function fetchUniqueWatchedMoviesPaginated(
return $this->urlGenerator->replacePosterPathWithImageSrcUrl($movies);
}

public function fetchPlayedMoviesPaginated(
int $userId,
int $limit,
int $page,
?string $searchTerm = null,
string $sortBy = 'title',
?SortOrder $sortOrder = null,
?Year $releaseYear = null,
?string $language = null,
?string $genre = null,
) : array {
if ($sortOrder === null) {
$sortOrder = SortOrder::createAsc();
}

$movies = $this->movieRepository->fetchUniqueWatchedMoviesPaginated(
$userId,
$limit,
$page,
$searchTerm,
$sortBy,
$sortOrder,
$releaseYear,
$language,
$genre,
);

return $this->urlGenerator->replacePosterPathWithImageSrcUrl($movies);
}

public function fetchWatchDatesForMovieIds(int $userId, array $movieIds) : array
{
$watchDates = [];
Expand All @@ -440,9 +440,9 @@ public function findHistoryEntryForMovieByUserOnDate(int $movieId, int $userId,
return $this->movieRepository->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedAt);
}

public function update(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?string $comment = null) : void
public function update(int $movieId, int $userId, ?Date $watchedAt, int $plays, int $position, ?string $comment = null) : void
{
$this->repository->update($movieId, $userId, $watchedAt, $plays, $comment);
$this->repository->update($movieId, $userId, $watchedAt, $plays, $position, $comment);
}

public function updateHistoryComment(int $movieId, int $userId, ?Date $watchAt, ?string $comment) : void
Expand Down
7 changes: 7 additions & 0 deletions src/Domain/Movie/History/MovieHistoryEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ private function __construct(
private readonly int $movieId,
private readonly ?Date $watchedAt,
private readonly int $plays,
private readonly int $position,
private readonly ?string $comment,
) {
}
Expand All @@ -20,6 +21,7 @@ public static function createFromArray(array $data) : self
(int)$data['movie_id'],
$data['watched_at'] == null ? null : Date::createFromString($data['watched_at']),
$data['plays'],
$data['position'],
$data['comment'],
);
}
Expand All @@ -39,6 +41,11 @@ public function getPlays() : int
return $this->plays;
}

public function getPosition() : int
{
return $this->position;
}

public function getWatchedAt() : ?Date
{
return $this->watchedAt;
Expand Down
13 changes: 8 additions & 5 deletions src/Domain/Movie/History/MovieHistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ public function __construct(private readonly Connection $dbConnection)
{
}

public function create(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?string $comment) : void
public function create(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?string $comment, int $position) : void
{
$this->dbConnection->executeStatement(
'INSERT INTO movie_user_watch_dates (movie_id, user_id, watched_at, plays, `comment`) VALUES (?, ?, ?, ?, ?)',
'INSERT INTO movie_user_watch_dates (movie_id, user_id, watched_at, plays, `comment`, `position`) VALUES (?, ?, ?, ?, ?, ?)',
[
$movieId,
$userId,
$watchedAt !== null ? (string)$watchedAt : null,
(string)$plays,
$comment,
$position,
],
);
}
Expand Down Expand Up @@ -64,14 +65,15 @@ public function deleteHistoryByIdAndDate(int $movieId, int $userId, ?Date $watch
);
}

public function update(int $movieId, int $userId, ?Date $watchedAt, int $plays, ?string $comment) : void
public function update(int $movieId, int $userId, ?Date $watchedAt, int $plays, int $position, ?string $comment) : void
{
if ($watchedAt === null) {
$this->dbConnection->executeStatement(
'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ? WHERE movie_id = ? AND user_id = ? AND watched_at IS NULL',
'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ?, `position` = ? WHERE movie_id = ? AND user_id = ? AND watched_at IS NULL',
[
$comment,
$plays,
$position,
$movieId,
$userId,
],
Expand All @@ -81,10 +83,11 @@ public function update(int $movieId, int $userId, ?Date $watchedAt, int $plays,
}

$this->dbConnection->executeStatement(
'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ? WHERE movie_id = ? AND user_id = ? AND watched_at = ?',
'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ? , `position` = ? WHERE movie_id = ? AND user_id = ? AND watched_at = ?',
[
$comment,
$plays,
$position,
$movieId,
$userId,
(string)$watchedAt,
Expand Down
10 changes: 7 additions & 3 deletions src/Domain/Movie/MovieApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
) {
}

public function addPlaysForMovieOnDate(int $movieId, int $userId, ?Date $watchedDate, int $playsToAdd = 1, ?string $comment = null) : void
public function addPlaysForMovieOnDate(int $movieId, int $userId, ?Date $watchedDate, int $playsToAdd = 1, ?int $position = null, ?string $comment = null) : void
{
$historyEntry = $this->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedDate);

Expand All @@ -59,6 +59,7 @@ public function addPlaysForMovieOnDate(int $movieId, int $userId, ?Date $watched
$userId,
$watchedDate,
$playsToAdd,
$position,
$comment,
);

Expand All @@ -70,6 +71,7 @@ public function addPlaysForMovieOnDate(int $movieId, int $userId, ?Date $watched
$userId,
$watchedDate,
$historyEntry->getPlays() + $playsToAdd,
$position ?? $historyEntry->getPosition(),
$comment ?? $historyEntry->getComment(),
);
}
Expand Down Expand Up @@ -405,7 +407,7 @@ public function findUserRating(int $movieId, int $userId) : ?PersonalRating
return $this->repository->findUserRating($movieId, $userId);
}

public function replaceHistoryForMovieByDate(int $movieId, int $userId, ?Date $watchedAt, int $playsPerDate, ?string $comment = null) : void
public function replaceHistoryForMovieByDate(int $movieId, int $userId, ?Date $watchedAt, int $playsPerDate, ?int $position = null, ?string $comment = null) : void
{
$existingHistoryEntry = $this->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedAt);

Expand All @@ -417,6 +419,7 @@ public function replaceHistoryForMovieByDate(int $movieId, int $userId, ?Date $w
$userId,
$watchedAt,
$playsPerDate,
$position,
$comment,
);

Expand All @@ -432,7 +435,8 @@ public function replaceHistoryForMovieByDate(int $movieId, int $userId, ?Date $w
$userId,
$watchedAt,
$playsPerDate,
$comment,
$position ?? $existingHistoryEntry->getPosition(),
$comment ?? $existingHistoryEntry->getComment(),
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/HttpController/Api/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function addToHistory(Request $request) : Response
$userId,
Date::createFromString($historyAddition['watchedAt']),
$historyAddition['plays'] ?? 1,
$historyAddition['position'] ?? 1,
$historyAddition['comment'] ?? null,
);
}
Expand Down Expand Up @@ -102,6 +103,7 @@ public function updateHistory(Request $request) : Response
$userId,
Date::createFromString($historyAddition['watchedAt']),
$historyAddition['plays'],
$historyAddition['position'],
$historyAddition['comment'],
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/HttpController/Web/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ public function createHistoryEntry(Request $request) : Response
$newWatchDate = empty($requestBody['newWatchDate']) === false ? Date::createFromStringAndFormat($requestBody['newWatchDate'], $dateFormat) : null;
$originalWatchDate = empty($requestBody['originalWatchDate']) === false ? Date::createFromStringAndFormat($requestBody['originalWatchDate'], $dateFormat) : null;

$plays = (int)$requestBody['plays'];
$plays = empty($requestBody['plays']) === true ? 1 : (int)$requestBody['plays'];
$comment = empty($requestBody['comment']) === true ? null : (string)$requestBody['comment'];
$position = empty($requestBody['position']) === true ? 1 : (int)$requestBody['position'];

if ($originalWatchDate == $newWatchDate) {
$this->movieApi->replaceHistoryForMovieByDate($movieId, $userId, $newWatchDate, $plays, $comment);
$this->movieApi->replaceHistoryForMovieByDate($movieId, $userId, $newWatchDate, $plays, $position, $comment);

return Response::create(StatusCode::createNoContent());
}

$this->movieApi->addPlaysForMovieOnDate($movieId, $userId, $newWatchDate, $plays);
$this->movieApi->addPlaysForMovieOnDate($movieId, $userId, $newWatchDate, $plays, $position);
$this->movieApi->deleteHistoryByIdAndDate($movieId, $userId, $originalWatchDate);

if ($comment !== null) {
Expand Down
6 changes: 5 additions & 1 deletion templates/component/modal-edit-watch-date.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
</div>
<div class="input-group" style="margin-top: 1rem;margin-bottom: 1rem">
<i class="input-group-text">Plays</i>
<input type="number" class="datepicker_input form-control" placeholder="" aria-label="Plays" id="editWatchDateModalPlaysInput">
<input type="number" class="form-control" aria-label="Plays" id="editWatchDateModalPlaysInput" min="1" required>
</div>
<div class="input-group" style="margin-top: 1rem;margin-bottom: 1rem">
<i class="input-group-text">Position</i>
<input type="number" class="form-control" aria-label="Position" id="editWatchDateModalPositionInput" min="1" required>
</div>
<div id="alertMovieModalDiv"></div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion templates/page/movie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
{% if currentUserName == routeUsername %}onclick="loadWatchDateModal(this)"{% endif %}
data-watch-date="{{ watchDate.watched_at is null ? '' : watchDate.watched_at|date(dateFormatPhp) }}"
data-plays="{{ watchDate.plays }}"
data-comment="{{ watchDate.comment }}">
data-comment="{{ watchDate.comment }}"
data-position="{{ watchDate.position }}">
{{ watchDate.watched_at is null ? 'Unkown date' : watchDate.watched_at|date(dateFormatPhp) }} {% if watchDate.plays > 1 %}({{ watchDate.plays }}x){% endif %}
{% if watchDate.comment != '' %}<i class="bi bi-chat-square-text" style="position: absolute; right:.7rem; top:25%;">{% endif %}</i>
</li>
Expand Down

0 comments on commit 1bc1e41

Please sign in to comment.