refactor(manager): replace internal CMS HTTP layer with direct CmsTrackService calls
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
@page "/cms/tracks/{Id:int}"
|
||||
@using System.Net.Http.Headers
|
||||
@using System.Net.Http.Json
|
||||
@using DeepDrftManager.Services
|
||||
@attribute [HierarchicalRoleAuthorize([SystemRoleConstants.Admin])]
|
||||
@inject ITrackService TrackService
|
||||
@inject IHttpClientFactory HttpClientFactory
|
||||
@inject IAuthSession AuthSession
|
||||
@inject ICmsTrackService CmsTrackService
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
@inject NavigationManager Nav
|
||||
@@ -126,27 +124,32 @@
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
var http = HttpClientFactory.CreateClient("DeepDrft.API");
|
||||
await AttachBearerAsync(http);
|
||||
|
||||
var payload = new
|
||||
// Re-fetch under the current scope so we mutate the DB-authoritative entity, not
|
||||
// the copy loaded at OnInitialized. Metadata-only update — EntryKey is immutable.
|
||||
var lookup = await TrackService.GetById(Id);
|
||||
if (!lookup.Success || lookup.Value is null)
|
||||
{
|
||||
TrackName = _form.TrackName,
|
||||
Artist = _form.Artist,
|
||||
Album = string.IsNullOrWhiteSpace(_form.Album) ? null : _form.Album,
|
||||
Genre = string.IsNullOrWhiteSpace(_form.Genre) ? null : _form.Genre,
|
||||
ReleaseDate = _form.ReleaseDate is { } d ? DateOnly.FromDateTime(d) : (DateOnly?)null
|
||||
};
|
||||
Snackbar.Add("Save failed — track could not be loaded.", Severity.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await http.PutAsJsonAsync($"api/cms/track/{Id}", payload);
|
||||
if (response.IsSuccessStatusCode)
|
||||
var track = lookup.Value;
|
||||
track.TrackName = _form.TrackName;
|
||||
track.Artist = _form.Artist;
|
||||
track.Album = string.IsNullOrWhiteSpace(_form.Album) ? null : _form.Album;
|
||||
track.Genre = string.IsNullOrWhiteSpace(_form.Genre) ? null : _form.Genre;
|
||||
track.ReleaseDate = _form.ReleaseDate is { } d ? DateOnly.FromDateTime(d) : null;
|
||||
|
||||
var updated = await TrackService.Update(track);
|
||||
if (updated.Success)
|
||||
{
|
||||
Snackbar.Add("Track updated.", Severity.Success);
|
||||
await LoadAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"Save failed: {(int)response.StatusCode} {response.ReasonPhrase}", Severity.Error);
|
||||
var error = updated.Messages.FirstOrDefault()?.Message ?? "Unknown error";
|
||||
Snackbar.Add($"Save failed: {error}", Severity.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -160,7 +163,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE api/cms/track/{Id} is handled by CmsDeleteController (T3 branch).
|
||||
private async Task ConfirmDelete()
|
||||
{
|
||||
if (_track is null) return;
|
||||
@@ -176,18 +178,16 @@
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
var http = HttpClientFactory.CreateClient("DeepDrft.API");
|
||||
await AttachBearerAsync(http);
|
||||
|
||||
var response = await http.DeleteAsync($"api/cms/track/{Id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
var result = await CmsTrackService.DeleteTrackAsync(Id);
|
||||
if (result.Success)
|
||||
{
|
||||
Snackbar.Add("Track deleted.", Severity.Success);
|
||||
Nav.NavigateTo("/cms/tracks");
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"Delete failed: {(int)response.StatusCode} {response.ReasonPhrase}", Severity.Error);
|
||||
var error = result.Messages.FirstOrDefault()?.Message ?? "Unknown error";
|
||||
Snackbar.Add($"Delete failed: {error}", Severity.Error);
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
@@ -199,15 +199,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AttachBearerAsync(HttpClient http)
|
||||
{
|
||||
var token = await AuthSession.GetValidTokenAsync();
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TrackEditForm
|
||||
{
|
||||
public string TrackName { get; set; } = string.Empty;
|
||||
|
||||
Reference in New Issue
Block a user