diff --git a/Marco.Pms.Model/Dtos/DocumentManager/DocumentBatchDto.cs b/Marco.Pms.Model/Dtos/DocumentManager/DocumentBatchDto.cs new file mode 100644 index 0000000..a3befae --- /dev/null +++ b/Marco.Pms.Model/Dtos/DocumentManager/DocumentBatchDto.cs @@ -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? Documents { get; set; } + } +} diff --git a/Marco.Pms.Services/Controllers/ImageController.cs b/Marco.Pms.Services/Controllers/ImageController.cs index 44952c6..5c3cdc5 100644 --- a/Marco.Pms.Services/Controllers/ImageController.cs +++ b/Marco.Pms.Services/Controllers/ImageController.cs @@ -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 GetImageList(Guid projectId, [FromQuery] string? filter) + public async Task 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(filter, options) ?? ""; - imageFilter = JsonSerializer.Deserialize(filter, options); + string unescapedJsonString = JsonSerializer.Deserialize(filter, options) ?? ""; + imageFilter = JsonSerializer.Deserialize(unescapedJsonString, options); + //imageFilter = JsonSerializer.Deserialize(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? buildings = null; List? floors = null; List? workAreas = null; - //List? workItems = null; - //List? 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 documents = new List(); 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(); + 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.SuccessResponse(response, "Images for provided batchId fetched successfully", 200)); }