From 176b9572edbf3a2d39228816f167f22e51a8d693 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Fri, 6 Jun 2025 23:56:11 +0530 Subject: [PATCH] Add Create, Update, and Delete endpoints for DocumentMaster with tenant scoping and validation --- .../Controllers/MasterController.cs | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/Marco.Pms.Services/Controllers/MasterController.cs b/Marco.Pms.Services/Controllers/MasterController.cs index b89dc91..5a76e4f 100644 --- a/Marco.Pms.Services/Controllers/MasterController.cs +++ b/Marco.Pms.Services/Controllers/MasterController.cs @@ -667,5 +667,139 @@ namespace Marco.Pms.Services.Controllers return NotFound(ApiResponse.ErrorResponse("Work category not found", "Work category not found", 404)); } } + + + + //-------------------Document------------------------------------- + + [HttpPost("document")] + public async Task CreateDocument([FromBody] DocumentMasterDto documentMaster) + { + try + { + if (documentMaster == null) + { + return BadRequest(ApiResponse.ErrorResponse("User sent empty payload", "User sent empty payload", 400)); + } + + if (string.IsNullOrWhiteSpace(documentMaster.Name) || + string.IsNullOrWhiteSpace(documentMaster.Type) || + string.IsNullOrWhiteSpace(documentMaster.Description) || + string.IsNullOrWhiteSpace(documentMaster.ValidationException)) + { + return BadRequest(ApiResponse.ErrorResponse("One or more required fields are empty", "Invalid input", 400)); + } + + var tenantId = _userHelper.GetTenantId(); + + var existingDocument = await _context.DocumentMasters + .FirstOrDefaultAsync(u => u.Name == documentMaster.Name && u.TenantId == tenantId); + + if (existingDocument != null) + { + return Conflict(ApiResponse.ErrorResponse("Document already exists", "Document already exists", 409)); + } + + var newDocument = new DocumentMaster + { + Name = documentMaster.Name, + Type = documentMaster.Type, + Description = documentMaster.Description, + ValidationException = documentMaster.ValidationException, + IsRequired = documentMaster.IsRequired, + TenantId = tenantId + }; + + await _context.DocumentMasters.AddAsync(newDocument); + await _context.SaveChangesAsync(); + + return Ok(ApiResponse.SuccessResponse("Document created successfully", "Success", 200)); + } + catch (Exception ex) + { + return StatusCode(500, ApiResponse.ErrorResponse("An unexpected error occurred", ex.Message, 500)); + } + } + + [HttpPost("document/edit/{id}")] + public async Task UpdateDocument([FromBody] DocumentMasterDto documentMaster, [FromRoute] Guid id) + { + try + { + Guid tenantId = _userHelper.GetTenantId(); + + if (documentMaster == null) + { + return BadRequest(ApiResponse.ErrorResponse("User sent empty payload", "User sent empty payload", 400)); + } + + if (string.IsNullOrWhiteSpace(documentMaster.Name) || + string.IsNullOrWhiteSpace(documentMaster.Type) || + string.IsNullOrWhiteSpace(documentMaster.Description) || + string.IsNullOrWhiteSpace(documentMaster.ValidationException)) + { + return BadRequest(ApiResponse.ErrorResponse("One or more required fields are empty", "Invalid input", 400)); + } + var existingDocument = await _context.DocumentMasters + .FirstOrDefaultAsync(d => d.Id == id && d.TenantId == tenantId); + + if (existingDocument == null) + { + return NotFound(ApiResponse.ErrorResponse("Document not found", $"No document found with ID {id}", 404)); + } + + var nameExisten = await _context.DocumentMasters.AnyAsync(d => d.Name == documentMaster.Name && d.Id != id && d.TenantId == tenantId); + + if (nameExisten) + { + return Conflict(ApiResponse.ErrorResponse("Another document with the same name exists", "Duplicate name", 409)); + } + + existingDocument.Name = documentMaster.Name; + existingDocument.Type = documentMaster.Type; + existingDocument.Description = documentMaster.Description; + existingDocument.ValidationException = documentMaster.ValidationException; + existingDocument.IsRequired = documentMaster.IsRequired; + + _context.DocumentMasters.Update(existingDocument); + await _context.SaveChangesAsync(); + + return Ok(ApiResponse.SuccessResponse("Document updated successfully", "Success", 200)); + } + catch (Exception ex) + { + return StatusCode(500, ApiResponse.ErrorResponse("An error occurred", ex.Message, 500)); + } + + } + + + [HttpDelete("document/{id}")] + public async Task DeleteDocument([FromRoute] Guid id) + { + try + { + Guid tenantId = _userHelper.GetTenantId(); + + var document = await _context.DocumentMasters + .FirstOrDefaultAsync(d => d.Id == id && d.TenantId == tenantId); + + if (document == null) + { + return NotFound(ApiResponse.ErrorResponse("Document not found", $"No document found with ID {id}", 404)); + } + + _context.DocumentMasters.Remove(document); + await _context.SaveChangesAsync(); + + return Ok(ApiResponse.SuccessResponse("Document deleted successfully", "Success", 200)); + } + catch (Exception ex) + { + return StatusCode(500, ApiResponse.ErrorResponse("An error occurred", ex.Message, 500)); + } + } + + } }