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

Add admin tool to handle duplicate artist names #480

Merged
merged 2 commits into from
Mar 6, 2025

Conversation

colagrosso
Copy link
Collaborator

Sorry for the delay.

Here's a video showing how it works:

simplescreenrecorder-2025-03-05_18.14.13.mp4

It works like we described in #419

Copy link
Member

@acabal acabal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Mike, thanks! I made some comments inline. In general I don't think we need to calculate the number of artworks changed, it's a DB call and that info isn't especially relevant.

lib/Artist.php Outdated
@@ -151,6 +187,28 @@ public static function GetByAlternateUrlName(?string $urlName): Artist{
', [$urlName], Artist::class)[0] ?? throw new Exceptions\ArtistNotFoundException();
}

public function AddAlternateName(string $name): void{
Db::Query('
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can throw a duplicate key exception which we should have logic for - in case the user is accidentally adding an alias that already exists.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added logic to catch the duplicate key exception and show a useful error message to the user.

lib/Artist.php Outdated
SELECT *
from Artists
where Name = ?
', [$name], Artist::class)[0] ?? throw new Exceptions\ArtistNotFoundException('Name: ' . $name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should override the default error message for this exception

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lib/Artist.php Outdated
SELECT *
from Artists
where UrlName = ?
', [$urlName], Artist::class)[0] ?? throw new Exceptions\ArtistNotFoundException('URL name: ' . $urlName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. URL name is meaningless to anyone but us who are familiar with the system internals.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lib/Artist.php Outdated
public function Delete(): void{
$artworkCount = count(Artwork::GetAllByArtist($this->UrlName, Enums\ArtworkFilterType::Admin, null /* submitterUserId */));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not count like this because we're potentially instantiating a huge amount of objects just to get a count. Instead do a raw Db::QueryBool('select exists (select ...))') which will just return true on the first record match and then stop

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


class ArtistHasArtworkException extends AppException{
/** @var string $message */
protected $message = 'Artist has artwork assoicated with it. Can’t delete.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error, also let's make this a complete sentence

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another whack at rewriting the error message.


$exception = HttpInput::SessionObject('exception', Exceptions\AppException::class);

$artist = Artist::GetByUrlName(HttpInput::Str(GET, 'artist-url-name') ?? '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need ?? here because passing null will still throw an exception, but will also avoid a DB call

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

$exception = HttpInput::SessionObject('exception', Exceptions\AppException::class);

$artist = Artist::GetByUrlName(HttpInput::Str(GET, 'artist-url-name') ?? '');
$artworkCount = count(Artwork::GetAllByArtist($artist->UrlName, Enums\ArtworkFilterType::Admin, null /* submitterUserId */));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to calculate $artworkCount, it's a DB call and the number is not especially relevant to anything

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<span>Reassign artwork by <?= Formatter::EscapeHtml($artist->Name) ?> to this artist. Total artworks to reassign: <?= $artworkCount ?></span>
<datalist id="artist-names-except-this-artist">
<? foreach(Artist::GetAll() as $a){ ?>
<? if($a->ArtistId == $artist->ArtistId){ continue; } ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of continue, test if $a->ArtistId != $artist->ArtistId so it's a more natural brace structure instead of a short circuit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


<? if($isAdminView){ ?>
<h2>Admin</h2>
<p><a href="<?= $artist->DeleteUrl ?>">Delete artist and reassign artwork</a></p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before ?>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if($httpMethod == Enums\HttpMethod::Delete){
$artistToDelete = Artist::GetByUrlName(HttpInput::Str(GET, 'artist-url-name') ?? '');
$exceptionRedirectUrl = $artistToDelete->DeleteUrl;
$artworkReassignedCount = count(Artwork::GetAllByArtist($artistToDelete->UrlName, Enums\ArtworkFilterType::Admin, null /* submitterUserId */));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary to calculate

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@colagrosso
Copy link
Collaborator Author

Thanks for the quick review. I pushed the fixes as a separate commit so that they are easier to review. You can squash and merge them when we're done.

@acabal acabal merged commit a915097 into standardebooks:master Mar 6, 2025
@acabal
Copy link
Member

acabal commented Mar 6, 2025

Great work, thanks!

@colagrosso colagrosso deleted the cover_artist_merge branch March 7, 2025 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants