using FastEndpoints; using Microsoft.EntityFrameworkCore; using Socialize.Api.Data; using Socialize.Api.Infrastructure.Security; using Socialize.Api.Modules.ContentItems.Data; namespace Socialize.Api.Modules.ContentItems.Handlers; public record GetContentItemsRequest(Guid? WorkspaceId, Guid? ClientId, Guid? CampaignId); public record ContentItemDto( Guid Id, Guid WorkspaceId, Guid ClientId, Guid CampaignId, string Title, string PublicationMessage, string PublicationTargets, string? Hashtags, string Status, DateTimeOffset? DueDate, string CurrentRevisionLabel, int CurrentRevisionNumber); public class GetContentItemsHandler( AppDbContext dbContext, AccessScopeService accessScopeService) : Endpoint> { public override void Configure() { Get("/api/content-items"); Options(o => o.WithTags("Content Items")); } public override async Task HandleAsync(GetContentItemsRequest request, CancellationToken ct) { IQueryable query = dbContext.ContentItems.AsQueryable(); if (!accessScopeService.IsManager(User)) { IReadOnlyCollection workspaceScopeIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct); IReadOnlyCollection clientScopeIds = User.GetClientScopeIds(); IReadOnlyCollection campaignScopeIds = User.GetCampaignScopeIds(); query = query.Where(item => workspaceScopeIds.Contains(item.WorkspaceId)); if (clientScopeIds.Count > 0) { query = query.Where(item => clientScopeIds.Contains(item.ClientId)); } if (campaignScopeIds.Count > 0) { query = query.Where(item => campaignScopeIds.Contains(item.CampaignId)); } } if (request.WorkspaceId.HasValue) { query = query.Where(item => item.WorkspaceId == request.WorkspaceId.Value); } if (request.CampaignId.HasValue) { query = query.Where(item => item.CampaignId == request.CampaignId.Value); } if (request.ClientId.HasValue) { query = query.Where(item => item.ClientId == request.ClientId.Value); } List items = await query .OrderBy(item => item.DueDate) .ThenBy(item => item.Title) .Select(item => new ContentItemDto( item.Id, item.WorkspaceId, item.ClientId, item.CampaignId, item.Title, item.PublicationMessage, item.PublicationTargets, item.Hashtags, item.Status, item.DueDate, item.CurrentRevisionLabel, item.CurrentRevisionNumber)) .ToListAsync(ct); await SendOkAsync(items, ct); } }