Skip to content

Commit b8a32bd

Browse files
committed
implement deleting artist aliases
1 parent b550974 commit b8a32bd

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

EMQ/Client/Components/EditArtistComponent.razor

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@using EMQ.Shared.MusicBrainz.Business
1010
@using EMQ.Shared.VNDB.Business
1111
@inject HttpClient _client
12+
@inject IJSRuntime _jsRuntime
1213

1314
<div style="border: 1px black solid; padding: 10px; max-width: 1400px;">
1415
@if (IsNew)
@@ -28,7 +29,7 @@
2829
<summary>Guidelines for editing artists</summary>
2930
<ul>
3031
<li>Follow VNDB's guidelines on name order and romanization.</li>
31-
<li>Deleting aliases is currently not supported.</li>
32+
<li>Ask a mod if you want to delete an existing alias.</li>
3233
</ul>
3334
<br/>
3435
</details>
@@ -67,6 +68,12 @@
6768
int indexCopy = index;
6869
<div style="border: 1px black solid; padding: 10px;">
6970
<span style="display: inline-block; cursor: pointer; margin-right: 4px;" @onclick="@(async () => await RemoveAlias(indexCopy))">🗑</span>
71+
@if (AuthStuff.HasPermission(ClientState.Session, PermissionKind.ReviewEdit))
72+
{
73+
<button type="button" class="btn btn-danger" @onclick="@(async () => { await Onclick_DeleteAAFromDb(Artist.Id, alias.ArtistAliasId); })">
74+
Delete artist alias from the database
75+
</button>
76+
}
7077
<br/>
7178

7279
<label style="padding: 4px;">
@@ -628,4 +635,25 @@
628635
Artist = artist;
629636
}
630637

638+
private async Task Onclick_DeleteAAFromDb(int aId, int aaId)
639+
{
640+
bool confirmed = await _jsRuntime.InvokeAsync<bool>("confirm",
641+
"Songs linked to this alias will be transferred over to the primary alias. This action is IRREVERSIBLE. Are you sure you want to delete this alias?");
642+
if (!confirmed)
643+
{
644+
return;
645+
}
646+
647+
var req = new SongArtist() { Id = aId, Titles = new List<Title>() { new() { ArtistAliasId = aaId } } };
648+
HttpResponseMessage res = await _client.PostAsJsonAsync("Mod/DeleteArtistAlias", req);
649+
if (res.IsSuccessStatusCode)
650+
{
651+
Artist.Titles.RemoveAll(x => x.ArtistAliasId == aaId);
652+
}
653+
else
654+
{
655+
await _jsRuntime.InvokeVoidAsync("alert",
656+
$"Error: {res.StatusCode:D} {res.StatusCode} {await res.Content.ReadAsStringAsync()}");
657+
}
658+
}
631659
}

EMQ/Server/Controllers/ModController.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,24 @@ public async Task<ActionResult> EditUser(ResGetPublicUserInfo req)
297297
await transactionAuth.CommitAsync();
298298
return Ok();
299299
}
300+
301+
[CustomAuthorize(PermissionKind.ReviewEdit)]
302+
[HttpPost]
303+
[Route("DeleteArtistAlias")]
304+
public async Task<ActionResult> DeleteArtistAlias(SongArtist req)
305+
{
306+
if (ServerState.IsServerReadOnly)
307+
{
308+
return Unauthorized();
309+
}
310+
311+
var session = AuthStuff.GetSession(HttpContext.Items);
312+
if (session is null)
313+
{
314+
return Unauthorized();
315+
}
316+
317+
bool success = await DbManager.DeleteArtistAlias(req.Id, req.Titles.Single().ArtistAliasId);
318+
return success ? Ok() : StatusCode(500);
319+
}
300320
}

EMQ/Server/Db/DbManager.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ await connection.InsertAsync(
16101610
}
16111611

16121612
// todo delete aliases that exist in the db but not in the argument -- might be easier to use UpsertList
1613+
// actually don't because we will have erroneous deletes when editing
16131614
aaIds.Add(aaId);
16141615
}
16151616

@@ -1643,6 +1644,30 @@ await connection.InsertAsync(
16431644
return (aId, aaIds);
16441645
}
16451646

1647+
public static async Task<bool> DeleteArtistAlias(int aId, int aaId)
1648+
{
1649+
await using var connection = new NpgsqlConnection(ConnectionHelper.GetConnectionString());
1650+
await connection.OpenAsync();
1651+
await using var transaction = await connection.BeginTransactionAsync();
1652+
1653+
int newAaId = await connection.ExecuteScalarAsync<int>(
1654+
"select id from artist_alias where artist_id = @aId AND is_main_name",
1655+
new { aId }, transaction);
1656+
if (newAaId > 0)
1657+
{
1658+
await connection.ExecuteAsync(
1659+
"UPDATE artist_music am SET artist_alias_id = @newAaId where artist_alias_id = @aaId",
1660+
new { aaId, newAaId }, transaction);
1661+
if (await connection.DeleteAsync(new ArtistAlias() { id = aaId }, transaction))
1662+
{
1663+
await transaction.CommitAsync();
1664+
return true;
1665+
}
1666+
}
1667+
1668+
return false;
1669+
}
1670+
16461671
// todo take QuizSettings as a required param instead of all this crap
16471672
public static async Task<List<Song>> GetRandomSongs(int numSongs, bool duplicates,
16481673
List<string>? validSources = null, QuizFilters? filters = null, bool printSql = false,

Tests/EntryPoints.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,4 +2696,23 @@ public async Task InsertCALFromMusicBrainz()
26962696
await Task.Delay(TimeSpan.FromSeconds(10));
26972697
}
26982698
}
2699+
2700+
[Test, Explicit]
2701+
public async Task ListArtistAliasesThatDifferOnlyInIsMainAliasFlag()
2702+
{
2703+
await using var connection = new NpgsqlConnection(ConnectionHelper.GetConnectionString());
2704+
var aas = (await connection.GetListAsync<ArtistAlias>()).ToArray();
2705+
foreach (ArtistAlias aa in aas)
2706+
{
2707+
var dup = aas.Where(x =>
2708+
x.artist_id == aa.artist_id &&
2709+
x.latin_alias == aa.latin_alias &&
2710+
x.non_latin_alias == aa.non_latin_alias).ToList();
2711+
if (dup.Count > 1)
2712+
{
2713+
Console.WriteLine(JsonSerializer.Serialize(dup, Utils.JsoIndented));
2714+
Console.WriteLine("-------------------------------------------------------");
2715+
}
2716+
}
2717+
}
26992718
}

0 commit comments

Comments
 (0)