Implement an API endpoint to delete a task #34
@ -8,6 +8,7 @@ using Marco.Pms.Model.Utilities;
|
|||||||
using Marco.Pms.Model.ViewModels;
|
using Marco.Pms.Model.ViewModels;
|
||||||
using Marco.Pms.Model.ViewModels.Employee;
|
using Marco.Pms.Model.ViewModels.Employee;
|
||||||
using MarcoBMS.Services.Helpers;
|
using MarcoBMS.Services.Helpers;
|
||||||
|
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;
|
||||||
@ -21,12 +22,14 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
{
|
{
|
||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context;
|
||||||
private readonly UserHelper _userHelper;
|
private readonly UserHelper _userHelper;
|
||||||
|
private readonly ILoggingService _logger;
|
||||||
|
|
||||||
|
|
||||||
public ProjectController(ApplicationDbContext context, UserHelper userHelper)
|
public ProjectController(ApplicationDbContext context, UserHelper userHelper, ILoggingService logger)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_userHelper = userHelper;
|
_userHelper = userHelper;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
[HttpGet("list")]
|
[HttpGet("list")]
|
||||||
public async Task<IActionResult> GetAll()
|
public async Task<IActionResult> GetAll()
|
||||||
@ -522,6 +525,45 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpDelete("task/{id}")]
|
||||||
|
public async Task<IActionResult> DeleteProjectTask(Guid id)
|
||||||
|
{
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
WorkItem? task = await _context.WorkItems.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id && t.TenantId == tenantId);
|
||||||
|
if (task != null)
|
||||||
|
{
|
||||||
|
if (task.CompletedWork == 0)
|
||||||
|
{
|
||||||
|
var assignedTask = await _context.TaskAllocations.Where(t => t.WorkItemId == id).ToListAsync();
|
||||||
|
if (assignedTask.Count == 0)
|
||||||
|
{
|
||||||
|
_context.WorkItems.Remove(task);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_logger.LogInfo("Task with ID {WorkItemId} has been successfully deleted.", task.Id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Task with ID {WorkItemId} is currently assigned and cannot be deleted.", task.Id);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Task is currently assigned and cannot be deleted.", "Task is currently assigned and cannot be deleted.", 400));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double percentage = (task.CompletedWork / task.PlannedWork) * 100;
|
||||||
|
percentage = Math.Round(percentage, 2);
|
||||||
|
_logger.LogWarning("Task with ID {WorkItemId} is {CompletionPercentage}% complete and cannot be deleted", task.Id, percentage);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse(System.String.Format("Task is {0}% complete and cannot be deleted", percentage), System.String.Format("Task is {0}% complete and cannot be deleted", percentage), 400));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogError("Task with ID {WorkItemId} not found ID database", id);
|
||||||
|
}
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Task deleted successfully", 200));
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost("manage-infra")]
|
[HttpPost("manage-infra")]
|
||||||
public async Task<IActionResult> ManageProjectInfra(List<InfraDot> infraDots)
|
public async Task<IActionResult> ManageProjectInfra(List<InfraDot> infraDots)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user