94 lines
2.9 KiB
C#
94 lines
2.9 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Socialize.Api.Data;
|
|
using Socialize.Api.Infrastructure.Security;
|
|
|
|
namespace Socialize.Api.Modules.Assets.Handlers;
|
|
|
|
public record GetAssetsRequest(Guid ContentItemId);
|
|
|
|
public record AssetRevisionDto(
|
|
Guid Id,
|
|
Guid AssetId,
|
|
int RevisionNumber,
|
|
string SourceReference,
|
|
string? PreviewUrl,
|
|
string? Notes,
|
|
Guid? CreatedByUserId,
|
|
DateTimeOffset CreatedAt);
|
|
|
|
public record AssetDto(
|
|
Guid Id,
|
|
Guid WorkspaceId,
|
|
Guid ContentItemId,
|
|
string AssetType,
|
|
string SourceType,
|
|
string DisplayName,
|
|
string? GoogleDriveFileId,
|
|
string? GoogleDriveLink,
|
|
string? PreviewUrl,
|
|
int CurrentRevisionNumber,
|
|
DateTimeOffset CreatedAt,
|
|
IReadOnlyCollection<AssetRevisionDto> Revisions);
|
|
|
|
public class GetAssetsHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: Endpoint<GetAssetsRequest, IReadOnlyCollection<AssetDto>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/assets");
|
|
Options(o => o.WithTags("Assets"));
|
|
}
|
|
|
|
public override async Task HandleAsync(GetAssetsRequest request, CancellationToken ct)
|
|
{
|
|
var item = await dbContext.ContentItems
|
|
.SingleOrDefaultAsync(candidate => candidate.Id == request.ContentItemId, ct);
|
|
if (item is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (!await accessScopeService.CanReviewContentAsync(User, item.WorkspaceId, item.ClientId, item.CampaignId, ct))
|
|
{
|
|
await SendForbiddenAsync(ct);
|
|
return;
|
|
}
|
|
|
|
List<AssetDto> assets = await dbContext.Assets
|
|
.Where(asset => asset.ContentItemId == request.ContentItemId)
|
|
.OrderBy(asset => asset.DisplayName)
|
|
.Select(asset => new AssetDto(
|
|
asset.Id,
|
|
asset.WorkspaceId,
|
|
asset.ContentItemId,
|
|
asset.AssetType,
|
|
asset.SourceType,
|
|
asset.DisplayName,
|
|
asset.GoogleDriveFileId,
|
|
asset.GoogleDriveLink,
|
|
asset.PreviewUrl,
|
|
asset.CurrentRevisionNumber,
|
|
asset.CreatedAt,
|
|
dbContext.AssetRevisions
|
|
.Where(revision => revision.AssetId == asset.Id)
|
|
.OrderByDescending(revision => revision.RevisionNumber)
|
|
.Select(revision => new AssetRevisionDto(
|
|
revision.Id,
|
|
revision.AssetId,
|
|
revision.RevisionNumber,
|
|
revision.SourceReference,
|
|
revision.PreviewUrl,
|
|
revision.Notes,
|
|
revision.CreatedByUserId,
|
|
revision.CreatedAt))
|
|
.ToList()))
|
|
.ToListAsync(ct);
|
|
|
|
await SendOkAsync(assets, ct);
|
|
}
|
|
}
|