diff --git a/Backend/src/Modules/Files/Files.Api/Endpoints/Files/DeleteFileAsset.cs b/Backend/src/Modules/Files/Files.Api/Endpoints/Files/DeleteFileAsset.cs new file mode 100644 index 0000000..066afc3 --- /dev/null +++ b/Backend/src/Modules/Files/Files.Api/Endpoints/Files/DeleteFileAsset.cs @@ -0,0 +1,25 @@ +using Files.Application.Entities.Files.Commands.DeleteFileAsset; + +namespace Files.Api.Endpoints.Files; + +public class DeleteFileAsset : ICarterModule +{ + public void AddRoutes(IEndpointRouteBuilder app) + { + app.MapDelete("/Files/{fileId}", async (string fileId, ISender sender) => + { + var result = await sender.Send(new DeleteFileAssetCommand(fileId)); + var response = result.Adapt(); + + return Results.Ok(response); + }) + .WithName("DeleteFileAsset") + .Produces() + .ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status404NotFound) + .WithSummary("Delete File Asset") + .WithDescription("Delete File Asset"); + } +} + +public record DeleteFileAssetResponse(bool IsSuccess); diff --git a/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetCommand.cs b/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetCommand.cs new file mode 100644 index 0000000..9655434 --- /dev/null +++ b/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetCommand.cs @@ -0,0 +1,13 @@ +namespace Files.Application.Entities.Files.Commands.DeleteFileAsset; + +public record DeleteFileAssetCommand(string FileAssetId) : ICommand; + +public record DeleteFileAssetResult(bool IsSuccess); + +public class DeleteFileAssetCommandValidator : AbstractValidator +{ + public DeleteFileAssetCommandValidator() + { + RuleFor(x => x.FileAssetId).NotEmpty().WithMessage("FileAssetId is required"); + } +} diff --git a/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetHandler.cs b/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetHandler.cs new file mode 100644 index 0000000..5cd6443 --- /dev/null +++ b/Backend/src/Modules/Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetHandler.cs @@ -0,0 +1,22 @@ +using Files.Application.Entities.Files.Exceptions; + +namespace Files.Application.Entities.Files.Commands.DeleteFileAsset; + +public class DeleteFileAssetHandler(IFilesDbContext dbContext) : ICommandHandler +{ + public async Task Handle(DeleteFileAssetCommand command, CancellationToken cancellationToken) + { + var fileAsset = await dbContext.FileAssets + .AsNoTracking() + .SingleOrDefaultAsync(f => f.Id == FileAssetId.Of(Guid.Parse(command.FileAssetId)), cancellationToken); + + if (fileAsset is null) + throw new FileAssetNotFoundException(command.FileAssetId); + + + dbContext.FileAssets.Remove(fileAsset); + await dbContext.SaveChangesAsync(cancellationToken); + + return new DeleteFileAssetResult(true); + } +}