Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug removing existing person data when adding new movie #568

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Domain/Movie/MovieApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public function updateCast(int $movieId, TmdbCast $tmdbCast) : void
$this->castApi->deleteByMovieId($movieId);

foreach ($tmdbCast as $position => $castMember) {
$person = $this->personApi->createOrUpdatePersonByTmdbId(
$person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData(
$castMember->getPerson()->getTmdbId(),
$castMember->getPerson()->getName(),
$castMember->getPerson()->getGender(),
Expand All @@ -522,7 +522,7 @@ public function updateCrew(int $movieId, TmdbCrew $tmdbCrew) : void
$this->crewApi->deleteByMovieId($movieId);

foreach ($tmdbCrew as $position => $crewMember) {
$person = $this->personApi->createOrUpdatePersonByTmdbId(
$person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData(
$crewMember->getPerson()->getTmdbId(),
$crewMember->getPerson()->getName(),
$crewMember->getPerson()->getGender(),
Expand Down
29 changes: 14 additions & 15 deletions src/Domain/Person/PersonApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function create(
);
}

public function createOrUpdatePersonByTmdbId(
public function createOrUpdatePersonWithTmdbCreditsData(
int $tmdbId,
string $name,
Gender $gender,
Expand All @@ -61,13 +61,12 @@ public function createOrUpdatePersonByTmdbId(
}

if ($person->getName() !== $name ||
$person->getGender() !== $gender ||
$person->getGender()->isEqual($gender) === false ||
$person->getKnownForDepartment() !== $knownForDepartment ||
$person->getTmdbPosterPath() !== $posterPath
) {
$this->update(
$this->repository->updateWithTmdbCreditsData(
$person->getId(),
$tmdbId,
$name,
$gender,
$knownForDepartment,
Expand Down Expand Up @@ -103,24 +102,19 @@ public function findByTmdbId(int $tmdbId) : ?PersonEntity
return $this->repository->findByTmdbId($tmdbId);
}

public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void
{
$this->repository->updateHideInTopLists($userId, $personId, $isHidden);
}

public function update(
int $id,
int $tmdbId,
string $name,
Gender $gender,
?string $knownForDepartment,
?string $tmdbPosterPath,
?string $biography = null,
?Date $birthDate = null,
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
?string $biography,
?Date $birthDate,
?Date $deathDate,
?string $placeOfBirth,
?DateTime $updatedAtTmdb,
?string $imdbId,
) : PersonEntity {
return $this->repository->update(
$id,
Expand All @@ -137,4 +131,9 @@ public function update(
$imdbId,
);
}

public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void
{
$this->repository->updateHideInTopLists($userId, $personId, $isHidden);
}
}
37 changes: 31 additions & 6 deletions src/Domain/Person/PersonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public function update(
Gender $gender,
?string $knownForDepartment,
?string $tmdbPosterPath,
?string $biography = null,
?Date $birthDate = null,
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
?string $biography,
?Date $birthDate,
?Date $deathDate,
?string $placeOfBirth,
?DateTime $updatedAtTmdb,
?string $imdbId,
) : PersonEntity {
$payload = [
'name' => $name,
Expand Down Expand Up @@ -165,6 +165,31 @@ public function updateHideInTopLists(int $userId, int $personId, bool $isHidden)
);
}

public function updateWithTmdbCreditsData(
int $id,
string $name,
Gender $gender,
?string $knownForDepartment,
?string $tmdbPosterPath,
) : PersonEntity {
$updatedAt = (string)DateTime::create();

$payload = [
'name' => $name,
'gender' => $gender->asInt(),
'known_for_department' => $knownForDepartment,
'tmdb_poster_path' => $tmdbPosterPath,
'updated_at' => $updatedAt,
'updated_at_tmdb' => $updatedAt,
];

$this->dbConnection->update(
'person', $payload, ['id' => $id],
);

return $this->fetchById($id);
}

private function fetchById(int $id) : PersonEntity
{
$data = $this->dbConnection->fetchAssociative('SELECT * FROM `person` WHERE id = ?', [$id]);
Expand Down
3 changes: 1 addition & 2 deletions src/Service/Tmdb/PersonChangesCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class PersonChangesCalculator
{

// phpcs:ignore Generic.Metrics.CyclomaticComplexity
public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPerson) : array
{
Expand Down Expand Up @@ -62,7 +61,7 @@ public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPer
];
}

if ($person->getGender()->asInt() !== $tmdbPerson->getGender()->asInt()) {
if ($person->getGender()->isEqual($tmdbPerson->getGender()) === false) {
$changes['gender'] = [
'old' => $person->getGender()->getText(),
'new' => $tmdbPerson->getGender()->getText(),
Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/Gender.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public function getText() : string

return self::GENDER_TEXT[$this->asInt()];
}

public function isEqual(Gender $gender) : bool
{
return $this->gender === $gender->gender;
}
}