67 lines
1.5 KiB
C#
67 lines
1.5 KiB
C#
using Hutopy.Infrastructure.Security;
|
|
using Hutopy.Modules.Contents.Data;
|
|
|
|
namespace Hutopy.Modules.Contents.Features;
|
|
|
|
[PublicAPI]
|
|
public record RemoveAlbumRequest(
|
|
Guid AlbumId);
|
|
|
|
[PublicAPI]
|
|
public sealed class RemoveAlbumRequestValidator : Validator<RemoveAlbumRequest>
|
|
{
|
|
public RemoveAlbumRequestValidator()
|
|
{
|
|
RuleFor(x => x.AlbumId)
|
|
.NotNull()
|
|
.NotEmpty();
|
|
}
|
|
}
|
|
|
|
[PublicAPI]
|
|
public class RemoveAlbumHandler(
|
|
ContentsDbContext context)
|
|
: Endpoint<RemoveAlbumRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/api/albums/{AlbumId}");
|
|
Options(o => o.WithTags("Albums"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
RemoveAlbumRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
Guid userId = User.GetUserId();
|
|
|
|
Album? album = await context
|
|
.Albums
|
|
.Include(a => a.Photos)
|
|
.SingleOrDefaultAsync(
|
|
a => a.Id == request.AlbumId && a.CreatedBy == userId,
|
|
ct);
|
|
|
|
if (album is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
// Soft delete the album
|
|
album.DeletedBy = userId;
|
|
album.DeletedAt = DateTimeOffset.UtcNow;
|
|
|
|
// Soft delete all photos in the album
|
|
foreach (AlbumPhoto photo in album.Photos)
|
|
{
|
|
photo.DeletedBy = userId;
|
|
photo.DeletedAt = DateTimeOffset.UtcNow;
|
|
}
|
|
|
|
await context.SaveChangesAsync(ct);
|
|
|
|
await SendNoContentAsync(ct);
|
|
}
|
|
}
|