diff --git a/Marco.Pms.Services/Controllers/DocumentController.cs b/Marco.Pms.Services/Controllers/DocumentController.cs index 1d6101d..1a6b6ef 100644 --- a/Marco.Pms.Services/Controllers/DocumentController.cs +++ b/Marco.Pms.Services/Controllers/DocumentController.cs @@ -327,7 +327,7 @@ namespace Marco.Pms.Services.Controllers } [HttpGet("list/versions/{parentAttachmentId}")] - public async Task GetAllVersionsAsync(Guid parentAttachmentId) + public async Task GetListAllVersionsAsync(Guid parentAttachmentId) { _logger.LogInfo("Start fetching document versions for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId); @@ -379,6 +379,41 @@ namespace Marco.Pms.Services.Controllers } } + [HttpGet("get/version/{id}")] + public async Task GetAllVersionsAsync(Guid id) + { + // API endpoint to get all versions of an attachment by its ID + _logger.LogInfo($"Fetching versions for attachment with ID: {id}"); + await using var _context = await _dbContextFactory.CreateDbContextAsync(); + // Create a new DbContext from the factory asynchronously + using var scope = _serviceScope.CreateScope(); + // Create a new service scope for scoped services + + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + // Get the currently logged in employee asynchronously + + var versionMapping = await _context.AttachmentVersionMappings + // Retrieve version mapping including the child attachment and its document + .Include(av => av.ChildAttachment) + .ThenInclude(da => da!.Document) + .FirstOrDefaultAsync(av => av.ChildAttachmentId == id); + + if (versionMapping == null || versionMapping.ChildAttachment == null || versionMapping.ChildAttachment.Document == null) + // Return 404 if no version mapping or related entities found + { + _logger.LogWarning($"Version mapping not found for attachment ID: {id}"); + return NotFound(ApiResponse.ErrorResponse("Version not found", "Version not found in database", 404)); + } + + var s3Service = scope.ServiceProvider.GetRequiredService(); + // Resolve S3UploadService from the scope to generate pre-signed URL + var preSignedUrl = s3Service.GeneratePreSignedUrl(versionMapping.ChildAttachment.Document.S3Key); + // Generate pre-signed URL for the document stored in S3 + _logger.LogInfo($"Generated pre-signed URL for attachment ID: {id}"); + + return Ok(ApiResponse.SuccessResponse(preSignedUrl, "Pre-Signed Url for old version fetched successfully", 200)); + // Return the pre-signed URL with a success response + } ///