Skip to content

Commit

Permalink
fix: only save on title update once
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Sep 16, 2024
1 parent 187c45d commit 2dcb7ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 13 additions & 5 deletions app/Helpers/database/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,22 @@ function modifyGameTitle(string $username, int $gameId, string $value): bool
}

$originalTitle = $game->title;

$game->title = $value;
$game->save();

// Also, if necessary, update the game's sort title.
(new WriteGameSortTitleFromGameTitleAction())->execute($game, $originalTitle);
$newSortTitle = (new WriteGameSortTitleFromGameTitleAction())->execute(
$game,
$originalTitle,
shouldSaveGame: false,
);

addArticleComment('Server', ArticleType::GameModification, $gameId, "{$username} changed the game name");
if ($newSortTitle !== null) {
$game->sort_title = $newSortTitle;
}

if ($game->isDirty()) {
$game->save();
addArticleComment('Server', ArticleType::GameModification, $gameId, "{$username} changed the game name");
}

return true;
}
Expand Down
12 changes: 9 additions & 3 deletions app/Platform/Actions/WriteGameSortTitleFromGameTitleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

class WriteGameSortTitleFromGameTitleAction
{
public function execute(Game $game, string $originalTitle, bool $shouldRespectCustomSortTitle = true): ?string
{
public function execute(
Game $game,
string $originalTitle,
bool $shouldRespectCustomSortTitle = true,
bool $shouldSaveGame = true,
): ?string {
$computeAction = new ComputeGameSortTitleAction();

// Compute the original sort title based on the original game title.
Expand All @@ -25,7 +29,9 @@ public function execute(Game $game, string $originalTitle, bool $shouldRespectCu

// Otherwise, compute and save the new sort title.
$game->sort_title = $computeAction->execute($game->title);
$game->save();
if ($shouldSaveGame) {
$game->save();
}

return $game->sort_title;
}
Expand Down

0 comments on commit 2dcb7ec

Please sign in to comment.