-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7885103
commit 64c274b
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Backend/src/Modules/Files/Files.Api/Endpoints/Files/DeleteFileAsset.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DeleteFileAssetResponse>(); | ||
|
||
return Results.Ok(response); | ||
}) | ||
.WithName("DeleteFileAsset") | ||
.Produces<DeleteFileAssetResponse>() | ||
.ProducesProblem(StatusCodes.Status400BadRequest) | ||
.ProducesProblem(StatusCodes.Status404NotFound) | ||
.WithSummary("Delete File Asset") | ||
.WithDescription("Delete File Asset"); | ||
} | ||
} | ||
|
||
public record DeleteFileAssetResponse(bool IsSuccess); |
13 changes: 13 additions & 0 deletions
13
...Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Files.Application.Entities.Files.Commands.DeleteFileAsset; | ||
|
||
public record DeleteFileAssetCommand(string FileAssetId) : ICommand<DeleteFileAssetResult>; | ||
|
||
public record DeleteFileAssetResult(bool IsSuccess); | ||
|
||
public class DeleteFileAssetCommandValidator : AbstractValidator<DeleteFileAssetCommand> | ||
{ | ||
public DeleteFileAssetCommandValidator() | ||
{ | ||
RuleFor(x => x.FileAssetId).NotEmpty().WithMessage("FileAssetId is required"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...Files/Files.Application/Entities/Files/Commands/DeleteFileAsset/DeleteFileAssetHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Files.Application.Entities.Files.Exceptions; | ||
|
||
namespace Files.Application.Entities.Files.Commands.DeleteFileAsset; | ||
|
||
public class DeleteFileAssetHandler(IFilesDbContext dbContext) : ICommandHandler<DeleteFileAssetCommand, DeleteFileAssetResult> | ||
{ | ||
public async Task<DeleteFileAssetResult> 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); | ||
} | ||
} |