From f75c31b3ceecaf62d05cd4a074e9af78bee678fd Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Wed, 10 Sep 2025 11:52:13 +0530 Subject: [PATCH] Added the check for checking uniquness of document ID --- .../Controllers/DocumentController.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Marco.Pms.Services/Controllers/DocumentController.cs b/Marco.Pms.Services/Controllers/DocumentController.cs index fbb5f5d..ef73d3c 100644 --- a/Marco.Pms.Services/Controllers/DocumentController.cs +++ b/Marco.Pms.Services/Controllers/DocumentController.cs @@ -703,6 +703,13 @@ namespace Marco.Pms.Services.Controllers return NotFound(ApiResponse.ErrorResponse($"{(entityType == EmployeeEntity ? "Employee" : "Project")} Not Found", "Entity not found in database", 404)); } + var isDocumentExist = await _context.DocumentAttachments.AnyAsync(da => da.DocumentId == model.DocumentId && da.TenantId == tenantId); + if (isDocumentExist) + { + _logger.LogWarning("{DocumentId} is already existed in database", model.DocumentId ?? string.Empty); + return StatusCode(409, ApiResponse.ErrorResponse("Document already existed", "Document already existed in database", 409)); + } + // Map DTO to DB entity var attachment = _mapper.Map(model); attachment.UploadedAt = DateTime.UtcNow; @@ -1031,6 +1038,13 @@ namespace Marco.Pms.Services.Controllers return NotFound(ApiResponse.ErrorResponse($"{(entityType == EmployeeEntity ? "Employee" : "Project")} Not Found", "Entity not found in database", 404)); } + var isDocumentExist = await _context.DocumentAttachments.AnyAsync(da => da.Id != model.Id && da.DocumentId == model.DocumentId && da.TenantId == tenantId); + if (isDocumentExist) + { + _logger.LogWarning("{DocumentId} is already existed in database", model.DocumentId ?? string.Empty); + return StatusCode(409, ApiResponse.ErrorResponse("Document already existed", "Document already existed in database", 409)); + } + // Prepare for versioning var oldVersionMapping = await _context.AttachmentVersionMappings .FirstOrDefaultAsync(av => av.ChildAttachmentId == oldAttachment.Id && av.TenantId == tenantId);