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