84 lines
1.8 KiB
C#
84 lines
1.8 KiB
C#
using Hutopy.Modules.Contents.Data;
|
|
|
|
namespace Hutopy.Modules.Contents.Features;
|
|
|
|
[PublicAPI]
|
|
public record GetAlbumRequest(
|
|
Guid AlbumId);
|
|
|
|
[PublicAPI]
|
|
public record AlbumPhotoDto(
|
|
Guid Id,
|
|
string OriginalUrl,
|
|
string ThumbnailUrl,
|
|
string? Caption,
|
|
int Order,
|
|
DateTimeOffset CreatedAt);
|
|
|
|
[PublicAPI]
|
|
public record GetAlbumResponse(
|
|
Guid Id,
|
|
string Title,
|
|
IReadOnlyList<AlbumPhotoDto> Photos,
|
|
DateTimeOffset CreatedAt);
|
|
|
|
[PublicAPI]
|
|
public sealed class GetAlbumRequestValidator : Validator<GetAlbumRequest>
|
|
{
|
|
public GetAlbumRequestValidator()
|
|
{
|
|
RuleFor(x => x.AlbumId)
|
|
.NotNull()
|
|
.NotEmpty();
|
|
}
|
|
}
|
|
|
|
[PublicAPI]
|
|
public class GetAlbumHandler(
|
|
ContentsDbContext context)
|
|
: Endpoint<GetAlbumRequest, GetAlbumResponse>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
AllowAnonymous();
|
|
Get("/api/albums/{AlbumId}");
|
|
Options(o => o.WithTags("Albums"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
GetAlbumRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
var album = await context
|
|
.Albums
|
|
.Include(a => a.Photos.OrderBy(p => p.Order))
|
|
.SingleOrDefaultAsync(
|
|
a => a.Id == request.AlbumId,
|
|
cancellationToken: ct);
|
|
|
|
if (album is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
var photos = album.Photos
|
|
.Select(p => new AlbumPhotoDto(
|
|
p.Id,
|
|
p.OriginalUrl,
|
|
p.ThumbnailUrl,
|
|
p.Caption,
|
|
p.Order,
|
|
p.CreatedAt))
|
|
.ToList();
|
|
|
|
await SendOkAsync(
|
|
new GetAlbumResponse(
|
|
album.Id,
|
|
album.Title,
|
|
photos,
|
|
album.CreatedAt),
|
|
ct);
|
|
}
|
|
}
|