Optimized the task management API
This commit is contained in:
parent
692b1bfef3
commit
aa2bc674eb
@ -13,7 +13,6 @@ using MarcoBMS.Services.Service;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace MarcoBMS.Services.Controllers
|
namespace MarcoBMS.Services.Controllers
|
||||||
{
|
{
|
||||||
@ -60,7 +59,7 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<Project> projects = await _projectsHelper.GetMyProjects(tenantId, LoggedInEmployee);
|
List<Project> projects = await _projectsHelper.GetMyProjects(tenantId, LoggedInEmployee);
|
||||||
|
|
||||||
|
|
||||||
// 4. Project projection to ProjectInfoVM
|
// 4. Project projection to ProjectInfoVM
|
||||||
@ -573,45 +572,65 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("task")]
|
[HttpPost("task")]
|
||||||
public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDot)
|
public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDtos)
|
||||||
{
|
{
|
||||||
Guid tenantId = GetTenantId();
|
_logger.LogInfo("CreateProjectTask called with {Count} items", workItemDtos?.Count ?? 0);
|
||||||
List<WorkItemVM> workItems = new List<WorkItemVM> { };
|
|
||||||
string responseMessage = "";
|
|
||||||
if (workItemDot != null)
|
|
||||||
{
|
|
||||||
foreach (var item in workItemDot)
|
|
||||||
{
|
|
||||||
WorkItem workItem = item.ToWorkItemFromWorkItemDto(tenantId);
|
|
||||||
|
|
||||||
if (item.Id != null)
|
// Validate request
|
||||||
{
|
if (workItemDtos == null || !workItemDtos.Any())
|
||||||
//update
|
{
|
||||||
_context.WorkItems.Update(workItem);
|
_logger.LogWarning("No work items provided in the request.");
|
||||||
await _context.SaveChangesAsync();
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item details are not valid.", 400));
|
||||||
responseMessage = "Task Added Successfully";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//create
|
|
||||||
_context.WorkItems.Add(workItem);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
responseMessage = "Task Updated Successfully";
|
|
||||||
}
|
|
||||||
var result = new WorkItemVM
|
|
||||||
{
|
|
||||||
WorkItemId = workItem.Id,
|
|
||||||
WorkItem = workItem
|
|
||||||
};
|
|
||||||
workItems.Add(result);
|
|
||||||
}
|
|
||||||
var activity = await _context.ActivityMasters.ToListAsync();
|
|
||||||
var category = await _context.WorkCategoryMasters.ToListAsync();
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(workItems, responseMessage, 200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
|
Guid tenantId = GetTenantId();
|
||||||
|
var workItemsToCreate = new List<WorkItem>();
|
||||||
|
var workItemsToUpdate = new List<WorkItem>();
|
||||||
|
var responseList = new List<WorkItemVM>();
|
||||||
|
|
||||||
|
foreach (var itemDto in workItemDtos)
|
||||||
|
{
|
||||||
|
var workItem = itemDto.ToWorkItemFromWorkItemDto(tenantId);
|
||||||
|
|
||||||
|
if (itemDto.Id != null && itemDto.Id != Guid.Empty)
|
||||||
|
{
|
||||||
|
// Update existing
|
||||||
|
workItemsToUpdate.Add(workItem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Create new
|
||||||
|
workItem.Id = Guid.NewGuid();
|
||||||
|
workItemsToCreate.Add(workItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseList.Add(new WorkItemVM
|
||||||
|
{
|
||||||
|
WorkItemId = workItem.Id,
|
||||||
|
WorkItem = workItem
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply DB changes
|
||||||
|
if (workItemsToCreate.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInfo("Adding {Count} new work items", workItemsToCreate.Count);
|
||||||
|
await _context.WorkItems.AddRangeAsync(workItemsToCreate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (workItemsToUpdate.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInfo("Updating {Count} existing work items", workItemsToUpdate.Count);
|
||||||
|
_context.WorkItems.UpdateRange(workItemsToUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("CreateProjectTask completed successfully. Created: {Created}, Updated: {Updated}", workItemsToCreate.Count, workItemsToUpdate.Count);
|
||||||
|
|
||||||
|
string responseMessage = $"{(workItemsToCreate.Any() ? "Task(s) created" : "")}{(workItemsToUpdate.Any() ? (workItemsToCreate.Any() ? " and " : "") + "updated" : "")} successfully.";
|
||||||
|
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse(responseList, responseMessage, 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("task/{id}")]
|
[HttpDelete("task/{id}")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user