Skip to content

Commit

Permalink
Merge pull request #185 from leepeuker/fix-cast-issue-on-missing-char…
Browse files Browse the repository at this point in the history
…acter-name

Make cast character names nullable to prevent errors when name missing
  • Loading branch information
leepeuker authored Dec 25, 2022
2 parents 38c5eb0 + 25b692f commit 17ca8e3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class SetCastCharacterNameColumnToNullable extends AbstractMigration
{
public function down() : void
{
$this->execute('ALTER TABLE movie_cast MODIFY character_name TEXT NOT NULL');
}

public function up() : void
{
$this->execute('ALTER TABLE movie_cast MODIFY character_name TEXT DEFAULT NULL');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class SetCastCharacterNameColumnToNullable extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_movie_cast` (
`person_id` INTEGER NOT NULL,
`movie_id` INTEGER NOT NULL,
`character_name` TEXT NOT NULL,
`position` INTEGER DEFAULT NULL,
FOREIGN KEY (`person_id`) REFERENCES person (`id`) ON DELETE CASCADE,
FOREIGN KEY (`movie_id`) REFERENCES movie (`id`) ON DELETE CASCADE,
UNIQUE (`movie_id`, `position`)
)
SQL,
);
$this->execute('INSERT INTO `tmp_movie_cast` (person_id, movie_id, character_name, position) SELECT * FROM movie_cast');
$this->execute('DROP TABLE `movie_cast`');
$this->execute('ALTER TABLE `tmp_movie_cast` RENAME TO `movie_cast`');
}

public function up() : void
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_movie_cast` (
`person_id` INTEGER NOT NULL,
`movie_id` INTEGER NOT NULL,
`character_name` TEXT DEFAULT NULL,
`position` INTEGER DEFAULT NULL,
FOREIGN KEY (`person_id`) REFERENCES person (`id`) ON DELETE CASCADE,
FOREIGN KEY (`movie_id`) REFERENCES movie (`id`) ON DELETE CASCADE,
UNIQUE (`movie_id`, `position`)
)
SQL,
);
$this->execute('INSERT INTO `tmp_movie_cast` (person_id, movie_id, character_name, position) SELECT * FROM movie_cast');
$this->execute('DROP TABLE `movie_cast`');
$this->execute('ALTER TABLE `tmp_movie_cast` RENAME TO `movie_cast`');
}
}
18 changes: 2 additions & 16 deletions src/Api/Tmdb/Dto/TmdbCastMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,24 @@
class TmdbCastMember
{
private function __construct(
private readonly int $castId,
private readonly TmdbCreditsPerson $person,
private readonly string $character,
private readonly int $order,
private readonly ?string $character,
) {
}

public static function createFromArray(array $data) : self
{
return new self(
$data['cast_id'],
TmdbCreditsPerson::createFromArray($data),
$data['character'],
$data['order'],
);
}

public function getCastId() : int
{
return $this->castId;
}

public function getCharacter() : string
public function getCharacter() : ?string
{
return $this->character;
}

public function getOrder() : int
{
return $this->order;
}

public function getPerson() : TmdbCreditsPerson
{
return $this->person;
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Movie/Cast/CastApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(
) {
}

public function create(int $movieId, int $personId, string $character, int $position) : void
public function create(int $movieId, int $personId, ?string $character, int $position) : void
{
$this->repository->create($movieId, $personId, $character, $position);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Movie/Cast/CastRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(private readonly Connection $dbConnection)
{
}

public function create(int $movieId, int $personId, string $character, int $position) : void
public function create(int $movieId, int $personId, ?string $character, int $position) : void
{
$this->dbConnection->insert(
'movie_cast',
Expand Down

0 comments on commit 17ca8e3

Please sign in to comment.