36 lines
1.3 KiB
C#
36 lines
1.3 KiB
C#
using Heimdall.Application.Common;
|
|||
|
|
using Heimdall.Application.DTOs;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using MediatR;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Groups.Commands.RemoveCameraFromGroup;
|
||
|
|
|
||
|
|
public record RemoveCameraFromGroupCommand(
|
||
|
|
Guid GroupId,
|
||
|
|
Guid CameraId) : IRequest<ServiceResult>;
|
||
|
|
|
||
|
|
public class RemoveCameraFromGroupHandler : IRequestHandler<RemoveCameraFromGroupCommand, ServiceResult>
|
||
|
|
{
|
||
|
|
private readonly IApplicationDbContext _db;
|
||
|
|
|
||
|
|
public RemoveCameraFromGroupHandler(IApplicationDbContext db) => _db = db;
|
||
|
|
|
||
|
|
public async Task<ServiceResult> Handle(RemoveCameraFromGroupCommand request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var access = await _db.GroupCameraAccesses
|
||
|
|
.FirstOrDefaultAsync(gca => gca.GroupId == request.GroupId && gca.CameraId == request.CameraId, ct)
|
||
|
|
.ConfigureAwait(false);
|
||
|
|
if (access is null)
|
||
|
|
return ServiceResult.NotFound("Group camera access entry not found");
|
||
|
|
|
||
|
|
_db.GroupCameraAccesses.Remove(access);
|
||
|
|
|
||
|
|
_db.AuditLogs.Add(new AuditLog(null, "GroupCameraAccess Revoked", "GroupCameraAccess", access.Id.ToString(),
|
||
|
|
oldValue: $"Group={access.GroupId}, Camera={access.CameraId}"));
|
||
|
|
|
||
|
|
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||
|
|
|
||
|
|
return ServiceResult.Success();
|
||
|
|
}
|
||
|
|
}
|