Add Create, Update, and Delete endpoints for DocumentMaster with tenant scoping and validation
This commit is contained in:
parent
83c3443c4f
commit
176b9572ed
@ -667,5 +667,139 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return NotFound(ApiResponse<object>.ErrorResponse("Work category not found", "Work category not found", 404));
|
return NotFound(ApiResponse<object>.ErrorResponse("Work category not found", "Work category not found", 404));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------Document-------------------------------------
|
||||||
|
|
||||||
|
[HttpPost("document")]
|
||||||
|
public async Task<IActionResult> CreateDocument([FromBody] DocumentMasterDto documentMaster)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (documentMaster == null)
|
||||||
|
{
|
||||||
|
return BadRequest(ApiResponse<object>.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<object>.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<object>.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<string>.SuccessResponse("Document created successfully", "Success", 200));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An unexpected error occurred", ex.Message, 500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("document/edit/{id}")]
|
||||||
|
public async Task<IActionResult> UpdateDocument([FromBody] DocumentMasterDto documentMaster, [FromRoute] Guid id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
|
||||||
|
if (documentMaster == null)
|
||||||
|
{
|
||||||
|
return BadRequest(ApiResponse<object>.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<object>.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<object>.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<object>.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<string>.SuccessResponse("Document updated successfully", "Success", 200));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An error occurred", ex.Message, 500));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpDelete("document/{id}")]
|
||||||
|
public async Task<IActionResult> 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<object>.ErrorResponse("Document not found", $"No document found with ID {id}", 404));
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.DocumentMasters.Remove(document);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok(ApiResponse<string>.SuccessResponse("Document deleted successfully", "Success", 200));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An error occurred", ex.Message, 500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user