using Socialize.Infrastructure.Security; using Socialize.Modules.ContentItems.Data; namespace Socialize.Modules.ContentItems.Handlers; public record GetContentItemsRequest(Guid? WorkspaceId, Guid? ClientId, Guid? ProjectId); public record ContentItemDto( Guid Id, Guid WorkspaceId, Guid ClientId, Guid ProjectId, 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 = User.GetWorkspaceScopeIds(); IReadOnlyCollection clientScopeIds = User.GetClientScopeIds(); IReadOnlyCollection projectScopeIds = User.GetProjectScopeIds(); query = query.Where(item => workspaceScopeIds.Contains(item.WorkspaceId)); if (clientScopeIds.Count > 0) { query = query.Where(item => clientScopeIds.Contains(item.ClientId)); } if (projectScopeIds.Count > 0) { query = query.Where(item => projectScopeIds.Contains(item.ProjectId)); } } if (request.WorkspaceId.HasValue) { query = query.Where(item => item.WorkspaceId == request.WorkspaceId.Value); } if (request.ProjectId.HasValue) { query = query.Where(item => item.ProjectId == request.ProjectId.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.ProjectId, item.Title, item.PublicationMessage, item.PublicationTargets, item.Hashtags, item.Status, item.DueDate, item.CurrentRevisionLabel, item.CurrentRevisionNumber)) .ToListAsync(ct); await SendOkAsync(items, ct); } }