Optimized the task management API
This commit is contained in:
parent
2e925efcf7
commit
c9bb18d8e5
@ -578,7 +578,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
projectIds.Add(projectAllocation.ProjectId);
|
projectIds.Add(projectAllocation.ProjectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -596,59 +595,82 @@ 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);
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
||||||
List<WorkItemVM> workItems = new List<WorkItemVM> { };
|
|
||||||
List<Guid> projectIds = new List<Guid>();
|
|
||||||
string message = "";
|
|
||||||
string responseMessage = "";
|
|
||||||
if (workItemDot != null)
|
|
||||||
{
|
|
||||||
foreach (var item in workItemDot)
|
|
||||||
{
|
|
||||||
WorkItem workItem = item.ToWorkItemFromWorkItemDto(tenantId);
|
|
||||||
|
|
||||||
|
// Validate request
|
||||||
|
if (workItemDtos == null || !workItemDtos.Any())
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No work items provided in the request.");
|
||||||
|
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>();
|
||||||
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
string message = "";
|
||||||
|
List<Guid> projectIds = new List<Guid>();
|
||||||
|
|
||||||
|
foreach (var itemDto in workItemDtos)
|
||||||
|
{
|
||||||
|
var workItem = itemDto.ToWorkItemFromWorkItemDto(tenantId);
|
||||||
var workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == workItem.WorkAreaId) ?? new WorkArea();
|
var workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == workItem.WorkAreaId) ?? new WorkArea();
|
||||||
|
|
||||||
Building building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty)) ?? new Building();
|
Building building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty)) ?? new Building();
|
||||||
|
|
||||||
if (item.Id != null)
|
if (itemDto.Id != null && itemDto.Id != Guid.Empty)
|
||||||
{
|
{
|
||||||
//update
|
// Update existing
|
||||||
_context.WorkItems.Update(workItem);
|
workItemsToUpdate.Add(workItem);
|
||||||
await _context.SaveChangesAsync();
|
message = $"Task Added in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
||||||
responseMessage = "Task Updated Successfully";
|
|
||||||
message = $"Task Updated in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//create
|
// Create new
|
||||||
_context.WorkItems.Add(workItem);
|
workItem.Id = Guid.NewGuid();
|
||||||
await _context.SaveChangesAsync();
|
workItemsToCreate.Add(workItem);
|
||||||
responseMessage = "Task Added Successfully";
|
message = $"Task Updated in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
||||||
message = $"Task Added in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
||||||
}
|
}
|
||||||
var result = new WorkItemVM
|
|
||||||
|
responseList.Add(new WorkItemVM
|
||||||
{
|
{
|
||||||
WorkItemId = workItem.Id,
|
WorkItemId = workItem.Id,
|
||||||
WorkItem = workItem
|
WorkItem = workItem
|
||||||
};
|
});
|
||||||
workItems.Add(result);
|
|
||||||
projectIds.Add(building.ProjectId);
|
projectIds.Add(building.ProjectId);
|
||||||
}
|
}
|
||||||
var activity = await _context.ActivityMasters.ToListAsync();
|
string responseMessage = "";
|
||||||
var category = await _context.WorkCategoryMasters.ToListAsync();
|
// Apply DB changes
|
||||||
|
if (workItemsToCreate.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInfo("Adding {Count} new work items", workItemsToCreate.Count);
|
||||||
|
await _context.WorkItems.AddRangeAsync(workItemsToCreate);
|
||||||
|
responseMessage = "Task Added Successfully";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (workItemsToUpdate.Any())
|
||||||
|
{
|
||||||
|
_logger.LogInfo("Updating {Count} existing work items", workItemsToUpdate.Count);
|
||||||
|
_context.WorkItems.UpdateRange(workItemsToUpdate);
|
||||||
|
responseMessage = "Task Updated Successfully";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("CreateProjectTask completed successfully. Created: {Created}, Updated: {Updated}", workItemsToCreate.Count, workItemsToUpdate.Count);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
|
||||||
|
|
||||||
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(workItems, responseMessage, 200));
|
|
||||||
}
|
|
||||||
|
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
|
|
||||||
|
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse(responseList, responseMessage, 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("task/{id}")]
|
[HttpDelete("task/{id}")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user