Added Pagenation to Image List API

This commit is contained in:
ashutosh.nehete 2025-07-05 12:23:28 +05:30
parent c9ff53a7ac
commit 6d8939d942
2 changed files with 58 additions and 25 deletions

View File

@ -0,0 +1,10 @@
using Marco.Pms.Model.DocumentManager;
namespace Marco.Pms.Model.Dtos.DocumentManager
{
public class DocumentBatchDto
{
public Guid? BatchId { get; set; }
public List<Document>? Documents { get; set; }
}
}

View File

@ -1,6 +1,7 @@
using System.Text.Json;
using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Activities;
using Marco.Pms.Model.Dtos.DocumentManager;
using Marco.Pms.Model.Employees;
using Marco.Pms.Model.Mapper;
using Marco.Pms.Model.Projects;
@ -10,6 +11,7 @@ using MarcoBMS.Services.Helpers;
using MarcoBMS.Services.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
namespace Marco.Pms.Services.Controllers
@ -37,7 +39,7 @@ namespace Marco.Pms.Services.Controllers
[HttpGet("images/{projectId}")]
public async Task<IActionResult> GetImageList(Guid projectId, [FromQuery] string? filter)
public async Task<IActionResult> GetImageList(Guid projectId, [FromQuery] string? filter, [FromQuery] int? pageNumber = 1, [FromQuery] int? pageSize = 10)
{
_logger.LogInfo("[GetImageList] Called by Employee for ProjectId: {ProjectId}", projectId);
@ -66,8 +68,9 @@ namespace Marco.Pms.Services.Controllers
try
{
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
//string unescapedJsonString = JsonSerializer.Deserialize<string>(filter, options) ?? "";
imageFilter = JsonSerializer.Deserialize<ImageFilter>(filter, options);
string unescapedJsonString = JsonSerializer.Deserialize<string>(filter, options) ?? "";
imageFilter = JsonSerializer.Deserialize<ImageFilter>(unescapedJsonString, options);
//imageFilter = JsonSerializer.Deserialize<ImageFilter>(filter, options);
}
catch (Exception ex)
{
@ -86,12 +89,9 @@ namespace Marco.Pms.Services.Controllers
var uploadedByIds = imageFilter?.UploadedByIds;
// Step 5: Fetch building > floor > area > work item hierarchy
// Step 3: Fetch building > floor > work area > work item hierarchy
List<Building>? buildings = null;
List<Floor>? floors = null;
List<WorkArea>? workAreas = null;
//List<WorkItem>? workItems = null;
//List<Document>? documents = null;
if (buildingIds != null && buildingIds.Count > 0)
{
@ -164,18 +164,35 @@ namespace Marco.Pms.Services.Controllers
var documentIds = attachments.Select(ta => ta.DocumentId).ToList();
// Step 7: Fetch and filter documents
List<DocumentBatchDto> documents = new List<DocumentBatchDto>();
var docQuery = _context.Documents.Include(d => d.UploadedBy)
.Where(d => documentIds.Contains(d.Id) && d.TenantId == tenantId);
if (startDate != null && endDate != null)
{
docQuery = docQuery.Where(d => d.UploadedAt.Date >= startDate.Value.Date && d.UploadedAt.Date <= endDate.Value.Date);
}
var documents = await docQuery.ToListAsync();
if (pageNumber != null && pageSize != null)
{
documents = await docQuery
.GroupBy(d => d.BatchId)
.OrderBy(g => g.Key)
.Skip((pageNumber.Value - 1) * pageSize.Value)
.Take(pageSize.Value)
.Select(g => new DocumentBatchDto
{
BatchId = g.Key,
Documents = g.ToList()
})
.ToListAsync();
Console.Write("Pagenation Success");
}
// Step 8: Build response
var documentVM = documents.Select(d =>
{
var refId = attachments.FirstOrDefault(ta => ta.DocumentId == d.Id)?.ReferenceId;
var docIds = d.Documents?.Select(x => x.Id).ToList() ?? new List<Guid>();
var refId = attachments.FirstOrDefault(ta => docIds.Contains(ta.DocumentId))?.ReferenceId;
var task = tasks.FirstOrDefault(t => t.Id == refId);
var comment = comments.FirstOrDefault(c => c.Id == refId);
@ -194,12 +211,16 @@ namespace Marco.Pms.Services.Controllers
return new
{
Id = d.Id,
BatchId = d.BatchId,
thumbnailUrl = d.ThumbS3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.ThumbS3Key) : (d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null),
ImageUrl = d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null,
UploadedBy = d.UploadedBy?.ToBasicEmployeeVMFromEmployee() ?? uploadedBy?.ToBasicEmployeeVMFromEmployee(),
UploadedAt = d.UploadedAt,
Documents = d.Documents?.Select(x => new
{
Id = x.Id,
thumbnailUrl = x.ThumbS3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.ThumbS3Key) : (x.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.S3Key) : null),
Url = x.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.S3Key) : null,
UploadedBy = x.UploadedBy?.ToBasicEmployeeVMFromEmployee() ?? uploadedBy?.ToBasicEmployeeVMFromEmployee(),
UploadedAt = x.UploadedAt,
}).ToList(),
Source = source,
ProjectId = projectId,
BuildingId = building?.Id,
@ -220,7 +241,7 @@ namespace Marco.Pms.Services.Controllers
if (uploadedByIds?.Any() == true)
{
documentVM = documentVM.Where(d => uploadedByIds.Contains(d.UploadedBy?.Id ?? Guid.Empty)).ToList();
documentVM = documentVM.Where(d => d.Documents != null && d.Documents.Any(x => uploadedByIds.Contains(x.UploadedBy?.Id ?? Guid.Empty))).ToList();
}
_logger.LogInfo("[GetImageList] Fetched {Count} documents for ProjectId: {ProjectId}", documentVM.Count, projectId);
@ -302,16 +323,18 @@ namespace Marco.Pms.Services.Controllers
.FirstOrDefaultAsync(b => b.Id == buildingId);
// Step 6: Construct the response
var response = documents.Select(d => new
var response = new
{
Id = d.Id,
BatchId = d.BatchId,
thumbnailUrl = d.ThumbS3Key != null
? _s3Service.GeneratePreSignedUrlAsync(d.ThumbS3Key)
: (d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null),
ImageUrl = d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null,
UploadedBy = d.UploadedBy?.ToBasicEmployeeVMFromEmployee() ?? uploadedBy?.ToBasicEmployeeVMFromEmployee(),
UploadedAt = d.UploadedAt,
BatchId = batchId,
Documents = documents?.Select(x => new
{
Id = x.Id,
thumbnailUrl = x.ThumbS3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.ThumbS3Key) : (x.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.S3Key) : null),
Url = x.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(x.S3Key) : null,
UploadedBy = x.UploadedBy?.ToBasicEmployeeVMFromEmployee() ?? uploadedBy?.ToBasicEmployeeVMFromEmployee(),
UploadedAt = x.UploadedAt,
}).ToList(),
Source = source,
ProjectId = building?.ProjectId,
BuildingId = building?.Id,
@ -327,9 +350,9 @@ namespace Marco.Pms.Services.Controllers
WorkCategoryName = workItem?.WorkCategoryMaster?.Name,
CommentId = comment?.Id,
Comment = comment?.Comment
}).ToList();
};
_logger.LogInfo("Fetched {Count} image(s) for BatchId: {BatchId}", response.Count, batchId);
_logger.LogInfo("Fetched {Count} image(s) for BatchId: {BatchId}", response.Documents?.Count ?? 0, batchId);
return Ok(ApiResponse<object>.SuccessResponse(response, "Images for provided batchId fetched successfully", 200));
}