feat(sites): suppression de site en cascade — agents et caméras (#177)
This commit is contained in:
1 parent
e6e8690588
commit
eef8c95d98
7 files changed
+177
-4
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Sites.Commands.DeleteSite;
|
||||
|
||||
@@ -8,15 +9,40 @@ public class DeleteSiteHandler : IRequestHandler<DeleteSiteCommand, ServiceResul
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
public DeleteSiteHandler(IApplicationDbContext context) => _context = context;
|
||||
|
||||
public async Task<ServiceResult> Handle(DeleteSiteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var site = await _context.Sites.FindAsync([request.Id], cancellationToken).ConfigureAwait(false);
|
||||
if (site is null)
|
||||
return ServiceResult.NotFound("Site not found");
|
||||
|
||||
// ── Cascade soft-delete: agents + cameras in the same transaction ──
|
||||
var cameras = await _context.Cameras
|
||||
.Where(c => c.SiteId == request.Id && !c.IsDeleted)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var agents = await _context.Agents
|
||||
.Where(a => a.SiteId == request.Id && !a.IsDeleted)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var camera in cameras)
|
||||
camera.SoftDelete();
|
||||
|
||||
foreach (var agent in agents)
|
||||
agent.SoftDelete();
|
||||
|
||||
// Soft-delete the site itself
|
||||
site.SoftDelete();
|
||||
|
||||
_context.AuditLogs.Add(new AuditLog(null, "Site.Deleted", "Site", request.Id.ToString()));
|
||||
// Audit log with cascade info
|
||||
_context.AuditLogs.Add(new AuditLog(
|
||||
null,
|
||||
"Site.Deleted",
|
||||
"Site",
|
||||
request.Id.ToString(),
|
||||
newValue: $"Cascade: {agents.Count} agent(s), {cameras.Count} camera(s) soft-deleted"));
|
||||
await _context.SaveChangesWithRetryAsync(ct: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return ServiceResult.Success();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using Heimdall.Application.Common;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Sites.Commands.DeleteSite;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of agents and cameras that would be cascade-deleted with a site.
|
||||
/// Used by the UI to display the scope before confirmation.
|
||||
/// </summary>
|
||||
public record GetSiteCascadeInfoQuery(Guid SiteId) : IRequest<ServiceResult<SiteCascadeInfoResponse>>;
|
||||
|
||||
public record SiteCascadeInfoResponse(int AgentCount, int CameraCount);
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.Sites.Commands.DeleteSite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Sites.Queries.GetSiteCascadeInfo;
|
||||
|
||||
public class GetSiteCascadeInfoHandler : IRequestHandler<GetSiteCascadeInfoQuery, ServiceResult<SiteCascadeInfoResponse>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetSiteCascadeInfoHandler(IApplicationDbContext context) => _context = context;
|
||||
|
||||
public async Task<ServiceResult<SiteCascadeInfoResponse>> Handle(GetSiteCascadeInfoQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var site = await _context.Sites.FindAsync([request.SiteId], cancellationToken).ConfigureAwait(false);
|
||||
if (site is null)
|
||||
return ServiceResult<SiteCascadeInfoResponse>.NotFound("Site not found");
|
||||
|
||||
var agentCount = await _context.Agents
|
||||
.Where(a => a.SiteId == request.SiteId && !a.IsDeleted)
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var cameraCount = await _context.Cameras
|
||||
.Where(c => c.SiteId == request.SiteId && !c.IsDeleted)
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return ServiceResult<SiteCascadeInfoResponse>.Success(
|
||||
new SiteCascadeInfoResponse(agentCount, cameraCount));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user