Skip to content

Commit

Permalink
IHS-58 Add delete tags handler and endpoint (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
adedw authored Apr 20, 2024
1 parent b02bb44 commit 5802e5c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ImageHosting.Persistence.DbContexts;
public interface IImageHostingDbContext
{
DbSet<Image> Images { get; }
DbSet<ImageTag> ImageTags { get; }
DbSet<ForbiddenCategory> ForbiddenCategories { get; }

/// <inheritdoc cref="DbContext"/>
Expand Down
14 changes: 11 additions & 3 deletions ImageHosting.Storage/Features/Images/Endpoints/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<ValidationFilter<UpdateNameCommand>>()
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static IServiceCollection AddImageServices(this IServiceCollection servic
.ValidateDataAnnotations();

services.AddScoped<INewImageProducer, NewImageProducer>();

return services
.AddTransient<IFileService, FileService>()
.AddTransient<IFileUploadCommandFactory, FileUploadCommandFactory>()
Expand All @@ -25,6 +25,7 @@ public static IServiceCollection AddImageServices(this IServiceCollection servic
.AddTransient<IGetImageAssetHandler, GetImageAssetHandler>()
.AddTransient<IGetImageHandler, GetImageHandler>()
.AddTransient<IUpdateNameHandler, UpdateNameHandler>()
.AddTransient<IAddTagsHandler, AddTagsHandler>();
.AddTransient<IAddTagsHandler, AddTagsHandler>()
.AddTransient<IDeleteTagsHandler, DeleteTagsHandler>();
}
}
24 changes: 24 additions & 0 deletions ImageHosting.Storage/Features/Images/Handlers/DeleteTagsHandler.cs
Original file line number Diff line number Diff line change
@@ -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<string> tags, CancellationToken cancellationToken = default);
}

public class DeleteTagsHandler(IImageHostingDbContext dbContext) : IDeleteTagsHandler
{
public Task DeleteAsync(ImageId id, IReadOnlyList<string> tags, CancellationToken cancellationToken = default)
{
return dbContext.ImageTags
.Where(it => it.ImageId == id && tags.Contains(it.TagName))
.ExecuteDeleteAsync(cancellationToken);
}
}

0 comments on commit 5802e5c

Please sign in to comment.