Add CMS track delete: DeepDrftContent DELETE endpoint, DeepDrftWeb SQL-first orchestration, DeepDrftCms confirmation dialog (W3-T3)
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using DeepDrftWeb.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DeepDrftWeb.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// CMS delete endpoint. Owned by W3-T3 — separate controller from upload/edit to
|
||||
/// avoid merge contention with parallel CMS tracks.
|
||||
///
|
||||
/// Delete order (CMS-PLAN W1.5): SQL first, then vault. If the SQL row is gone we
|
||||
/// return success to the user even when the subsequent vault delete fails — SQL is
|
||||
/// the source of truth for "exists from the user's view". A vault failure is logged
|
||||
/// as an orphan for maintenance to reap (see PLAN.md §4.3 dead-letter).
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/cms/track")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class CmsDeleteController : ControllerBase
|
||||
{
|
||||
private readonly ITrackService _trackService;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly ILogger<CmsDeleteController> _logger;
|
||||
|
||||
public CmsDeleteController(
|
||||
ITrackService trackService,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<CmsDeleteController> logger)
|
||||
{
|
||||
_trackService = trackService;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpDelete("{id:long}")]
|
||||
public async Task<ActionResult> DeleteTrack(long id)
|
||||
{
|
||||
// 1. Resolve the EntryKey before we delete the SQL row — afterwards the join is gone.
|
||||
var lookup = await _trackService.GetById(id);
|
||||
if (!lookup.Success)
|
||||
{
|
||||
_logger.LogError("CMS delete: lookup failed for track {TrackId}: {Error}", id, lookup.Messages.FirstOrDefault()?.Message);
|
||||
return StatusCode(500, "Failed to load track");
|
||||
}
|
||||
|
||||
var track = lookup.Value;
|
||||
if (track == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var entryKey = track.EntryKey;
|
||||
|
||||
// 2. SQL delete. On failure, do NOT touch the vault — nothing to clean up.
|
||||
var sqlDelete = await _trackService.Delete(id);
|
||||
if (!sqlDelete.Success)
|
||||
{
|
||||
_logger.LogError("CMS delete: SQL delete failed for track {TrackId}: {Error}", id, sqlDelete.Messages.FirstOrDefault()?.Message);
|
||||
return StatusCode(500, "Failed to delete track");
|
||||
}
|
||||
|
||||
// 3. Vault delete. Failure is logged as an orphan but does not fail the request:
|
||||
// SQL is the source of truth for the user's view; the orphan is a maintenance concern.
|
||||
var client = _httpClientFactory.CreateClient(Startup.ContentCmsHttpClientName);
|
||||
try
|
||||
{
|
||||
var response = await client.DeleteAsync($"api/track/{Uri.EscapeDataString(entryKey)}");
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Vault delete failed after SQL delete. {TrackId} {EntryKey} {Reason} {StatusCode}",
|
||||
id, entryKey, "vault delete failed after SQL delete", (int)response.StatusCode);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
ex,
|
||||
"Vault delete threw after SQL delete. {TrackId} {EntryKey} {Reason}",
|
||||
id, entryKey, "vault delete failed after SQL delete");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,12 @@ builder.Services.AddMudServices();
|
||||
|
||||
builder.Services.AddCmsServices();
|
||||
|
||||
// CMS → DeepDrftContent calls require the DeepDrftContent ApiKey. Loaded from a
|
||||
// gitignored environment file, same shape as DeepDrftContent/environment/apikey.json.
|
||||
// Optional so the file's absence in non-CMS dev does not fail boot; missing key is
|
||||
// surfaced when Startup.ConfigureDomainServices binds the CMS HttpClient.
|
||||
builder.Configuration.AddJsonFile("environment/apikey.json", optional: true, reloadOnChange: true);
|
||||
|
||||
var baseUrl = builder.GetKestrelUrl();
|
||||
var contentApiUrl = builder.Configuration["ApiUrls:ContentApi"] ?? throw new Exception("Content API URL is not configured");
|
||||
|
||||
|
||||
+21
-1
@@ -7,6 +7,13 @@ namespace DeepDrftWeb;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// Named HttpClient used by CMS controllers to call DeepDrftContent's ApiKey-protected endpoints.
|
||||
/// Distinct from the public WASM-facing "DeepDrft.Content" client so the API key never reaches
|
||||
/// the browser. Configured server-side only.
|
||||
/// </summary>
|
||||
public const string ContentCmsHttpClientName = "DeepDrft.Content.Cms";
|
||||
|
||||
public static void ConfigureDomainServices(WebApplicationBuilder builder)
|
||||
{
|
||||
// Add Entity Framework services
|
||||
@@ -18,11 +25,24 @@ public static class Startup
|
||||
builder.Services
|
||||
.AddHttpContextAccessor()
|
||||
.AddScoped<DarkModeService>();
|
||||
|
||||
|
||||
// Add Track services
|
||||
builder.Services
|
||||
.AddScoped<TrackRepository>()
|
||||
.AddScoped<ITrackService, TrackService>();
|
||||
|
||||
// CMS → DeepDrftContent client. The API key is required up front (no lazy resolution)
|
||||
// so a misconfiguration surfaces at startup instead of on the first delete attempt.
|
||||
var contentApiUrl = builder.Configuration["ApiUrls:ContentApi"]
|
||||
?? throw new InvalidOperationException("ApiUrls:ContentApi is required");
|
||||
var contentApiKey = builder.Configuration["DeepDrftContent:ApiKey"]
|
||||
?? throw new InvalidOperationException("DeepDrftContent:ApiKey is required (see environment/apikey.json)");
|
||||
|
||||
builder.Services.AddHttpClient(ContentCmsHttpClientName, client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(contentApiUrl);
|
||||
client.DefaultRequestHeaders.Add("ApiKey", contentApiKey);
|
||||
});
|
||||
}
|
||||
|
||||
public static string GetKestrelUrl(this WebApplicationBuilder builder)
|
||||
|
||||
Reference in New Issue
Block a user