Skip to content

Commit

Permalink
Merge pull request #77 from leepeuker/hotfix-date-format
Browse files Browse the repository at this point in the history
Fix issue with wrong date format when adding/removing watch dates
  • Loading branch information
leepeuker authored Jul 28, 2022
2 parents 4d68bdd + 176dc48 commit 0a139b5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
6 changes: 5 additions & 1 deletion public/js/logMovie.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function logMovie () {
let tmdbId = getTmdbId()
let watchDate = getWatchDate()
let movieTitle = document.getElementById('watchDateModalTitle').innerHTML
let dateFormatPhp = document.getElementById('dateFormatPhp').value

if (validateWatchDate(watchDate) === false) {
return
Expand All @@ -156,7 +157,10 @@ function logMovie () {
method: 'post', headers: {
'Content-type': 'application/json',
}, body: JSON.stringify({
'tmdbId': tmdbId, 'watchDate': watchDate, 'personalRating': rating,
'tmdbId': tmdbId,
'watchDate': watchDate,
'dateFormat': dateFormatPhp,
'personalRating': rating,
})
})
.then(function (response) {
Expand Down
4 changes: 3 additions & 1 deletion public/js/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function deleteWatchDate (e) {
return
}

const dateFormatPhp = document.getElementById('dateFormatPhp').value
const watchDate = document.getElementById(e.id + '-watch-date')

const apiUrl = '/movie/' + watchDate.getAttribute('movie-id') + '/history'
Expand All @@ -16,7 +17,8 @@ function deleteWatchDate (e) {
url: apiUrl,
type: 'DELETE',
data: JSON.stringify({
'date': watchDate.getAttribute('date')
'date': watchDate.getAttribute('date'),
'dateFormat': dateFormatPhp
}),
success: function (data, textStatus, xhr) {
window.location.reload()
Expand Down
7 changes: 3 additions & 4 deletions src/HttpController/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Movary\Application\User\Service\Authentication;
use Movary\Util\Json;
use Movary\ValueObject\Date;
use Movary\ValueObject\DateTime;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Movary\ValueObject\Http\StatusCode;
Expand All @@ -28,7 +27,7 @@ public function __construct(
private readonly Movie\Api $movieApi,
private readonly SyncMovie $tmdbMovieSyncService,
private readonly Authentication $authenticationService,
private readonly User\Api $userApi
private readonly User\Api $userApi,
) {
}

Expand All @@ -43,7 +42,7 @@ public function deleteHistoryEntry(Request $request) : Response
$requestBody = Json::decode($request->getBody());

$movieId = (int)$request->getRouteParameters()['id'];
$date = Date::createFromString($requestBody['date']);
$date = Date::createFromStringAndFormat($requestBody['date'], $requestBody['dateFormat']);
$count = $requestBody['count'] ?? 1;

$this->movieApi->deleteHistoryByIdAndDate($movieId, $userId, $date, $count);
Expand All @@ -65,7 +64,7 @@ public function logMovie(Request $request) : Response
throw new \RuntimeException('Missing parameters');
}

$watchDate = Date::createFromDateTime(DateTime::createFromString($requestData['watchDate']));
$watchDate = Date::createFromStringAndFormat($requestData['watchDate'], $requestData['dateFormat']);
$tmdbId = (int)$requestData['tmdbId'];
$personalRating = $requestData['personalRating'] === 0 ? null : PersonalRating::create((int)$requestData['personalRating']);

Expand Down
11 changes: 11 additions & 0 deletions src/ValueObject/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public static function createFromString(string $dateString) : self
return new self ((new \DateTime($dateString))->format(self::FORMAT));
}

public static function createFromStringAndFormat(string $dateString, string $dateFormat) : self
{
$dateTime = \DateTime::createFromFormat($dateFormat, $dateString);

if ($dateTime === false) {
throw new \RuntimeException(sprintf('Could not create datetime of string "%s" with format "%s".', $dateString, $dateFormat));
}

return new self ($dateTime->format(self::FORMAT));
}

public function __toString() : string
{
return $this->date;
Expand Down
1 change: 1 addition & 0 deletions templates/page/logMovie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}
<input value="{{ dateFormatJavascript }}" id="dateFormatJavascript" hidden>
<input value="{{ dateFormatPhp }}" id="dateFormatPhp" hidden>

<div id="alerts" style="margin-top: 1rem">
</div>
Expand Down
1 change: 1 addition & 0 deletions templates/page/movie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<p style="margin-block-end:0">Watched at:</p>
<ul>
<input value="{{ dateFormatPhp }}" id="dateFormatPhp" hidden>
{% for watchDate in watchDates|reverse %}
<li class="fw-light">{{ watchDate.watched_at|date(dateFormatPhp) }} {% if watchDate.plays > 1 %}({{ watchDate.plays }}x){% endif %}
{% if loggedIn == true %}
Expand Down
11 changes: 10 additions & 1 deletion tests/rest/movie.http
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ Accept: */*
Cache-Control: no-cache
Content-Type: application/application/json

{"watchDate" : "","tmdbId" : 110, "personalRating" : 3}
{"watchDate" : "22.07.22","tmdbId" : 9323, "personalRating" : 6, "dateFormat" : "d.m.y"}

####

DELETE http://127.0.0.1/movie/26/history
Accept: */*
Cache-Control: no-cache
Content-Type: application/application/json

{"date" : "23.07.22", "dateFormat" : "d.m.y"}

0 comments on commit 0a139b5

Please sign in to comment.