From 5802e5c72edabf1303173fbf9d7be9c0e3c59461 Mon Sep 17 00:00:00 2001 From: Ivan Kozelskikh Date: Sun, 21 Apr 2024 03:20:35 +0500 Subject: [PATCH] IHS-58 Add delete tags handler and endpoint (#72) --- .../DbContexts/IImageHostingDbContext.cs | 1 + .../Features/Images/Endpoints/Images.cs | 14 ++++++++--- .../Extensions/ServiceCollectionExtensions.cs | 5 ++-- .../Images/Handlers/DeleteTagsHandler.cs | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 ImageHosting.Storage/Features/Images/Handlers/DeleteTagsHandler.cs diff --git a/ImageHosting.Persistence/DbContexts/IImageHostingDbContext.cs b/ImageHosting.Persistence/DbContexts/IImageHostingDbContext.cs index c112b02..f7a6c88 100644 --- a/ImageHosting.Persistence/DbContexts/IImageHostingDbContext.cs +++ b/ImageHosting.Persistence/DbContexts/IImageHostingDbContext.cs @@ -6,6 +6,7 @@ namespace ImageHosting.Persistence.DbContexts; public interface IImageHostingDbContext { DbSet Images { get; } + DbSet ImageTags { get; } DbSet ForbiddenCategories { get; } /// diff --git a/ImageHosting.Storage/Features/Images/Endpoints/Images.cs b/ImageHosting.Storage/Features/Images/Endpoints/Images.cs index f79f67c..3c4db62 100644 --- a/ImageHosting.Storage/Features/Images/Endpoints/Images.cs +++ b/ImageHosting.Storage/Features/Images/Endpoints/Images.cs @@ -65,7 +65,6 @@ public static RouteGroupBuilder MapImagesEndpoints(this IEndpointRouteBuilder ro [FromServices] IGetImageHandler getImageHandler, CancellationToken cancellationToken) => { var readImage = await getImageHandler.GetAsync(id, cancellationToken); - return TypedResults.Ok(readImage); }) .WithName("GetImage") @@ -76,7 +75,6 @@ public static RouteGroupBuilder MapImagesEndpoints(this IEndpointRouteBuilder ro [FromServices] IUpdateNameHandler updateNameHandler, CancellationToken cancellationToken) => { var readImage = await updateNameHandler.UpdateAsync(id, command.NewName, cancellationToken); - return Results.Ok(readImage); }) .AddEndpointFilter>() @@ -92,13 +90,23 @@ public static RouteGroupBuilder MapImagesEndpoints(this IEndpointRouteBuilder ro [FromServices] IAddTagsHandler addTagsHandler, CancellationToken cancellationToken) => { var response = await addTagsHandler.AddAsync(id, addTagsCommand.Tags, cancellationToken); - return TypedResults.Ok(response); }) .WithName("AddTag") .WithTags("Tags") .MapToApiVersion(1); + tags.MapDelete(pattern: "", + handler: async ([FromRoute] ImageId id, [FromQuery] string[] tag, + [FromServices] IDeleteTagsHandler deleteTagsHandler, CancellationToken cancellationToken) => + { + await deleteTagsHandler.DeleteAsync(id, tag, cancellationToken); + return TypedResults.Ok(); + }) + .WithName("DeleteTags") + .WithTags("Tags") + .MapToApiVersion(1); + return images; } } \ No newline at end of file diff --git a/ImageHosting.Storage/Features/Images/Extensions/ServiceCollectionExtensions.cs b/ImageHosting.Storage/Features/Images/Extensions/ServiceCollectionExtensions.cs index ca26216..90c8dfe 100644 --- a/ImageHosting.Storage/Features/Images/Extensions/ServiceCollectionExtensions.cs +++ b/ImageHosting.Storage/Features/Images/Extensions/ServiceCollectionExtensions.cs @@ -14,7 +14,7 @@ public static IServiceCollection AddImageServices(this IServiceCollection servic .ValidateDataAnnotations(); services.AddScoped(); - + return services .AddTransient() .AddTransient() @@ -25,6 +25,7 @@ public static IServiceCollection AddImageServices(this IServiceCollection servic .AddTransient() .AddTransient() .AddTransient() - .AddTransient(); + .AddTransient() + .AddTransient(); } } \ No newline at end of file diff --git a/ImageHosting.Storage/Features/Images/Handlers/DeleteTagsHandler.cs b/ImageHosting.Storage/Features/Images/Handlers/DeleteTagsHandler.cs new file mode 100644 index 0000000..af90e54 --- /dev/null +++ b/ImageHosting.Storage/Features/Images/Handlers/DeleteTagsHandler.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using ImageHosting.Persistence.DbContexts; +using ImageHosting.Persistence.ValueTypes; +using Microsoft.EntityFrameworkCore; + +namespace ImageHosting.Storage.Features.Images.Handlers; + +public interface IDeleteTagsHandler +{ + Task DeleteAsync(ImageId id, IReadOnlyList tags, CancellationToken cancellationToken = default); +} + +public class DeleteTagsHandler(IImageHostingDbContext dbContext) : IDeleteTagsHandler +{ + public Task DeleteAsync(ImageId id, IReadOnlyList tags, CancellationToken cancellationToken = default) + { + return dbContext.ImageTags + .Where(it => it.ImageId == id && tags.Contains(it.TagName)) + .ExecuteDeleteAsync(cancellationToken); + } +} \ No newline at end of file