diff --git a/DotnetFoundation/DotnetFoundation.Api/Controllers/TaskController.cs b/DotnetFoundation/DotnetFoundation.Api/Controllers/TaskController.cs index 021c8d3..6a70fd9 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Controllers/TaskController.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Controllers/TaskController.cs @@ -1,14 +1,14 @@ +using DotnetFoundation.Application.Exceptions; using DotnetFoundation.Application.Interfaces.Services; using DotnetFoundation.Application.Models.Common; using DotnetFoundation.Application.Models.DTOs.TaskDetailsDTO; using DotnetFoundation.Application.Models.Enums; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace DotnetFoundation.Api.Controllers; [ApiController] -[Route("api/tasks")] +[Route("api")] public class TaskController : ControllerBase { private readonly ITaskDetailsService _taskDetailsService; @@ -20,40 +20,56 @@ public TaskController(ITaskDetailsService TaskDetailsService) /// /// Get all tasks. /// - [HttpGet] + [HttpGet("tasks")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task>>> GetAllTasksAsync() { BaseResponse> response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.GetAllTasksAsync().ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.GetAllTasksAsync().ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } /// /// Get all active tasks. /// - [HttpGet("active")] + [HttpGet("tasks/active")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task>>> GetActiveTasksAsync() { BaseResponse> response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.GetActiveTasksAsync().ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.GetActiveTasksAsync().ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } /// /// Get task details by Id. /// /// Id of task record - [HttpGet("{taskId}")] + [HttpGet("tasks/{taskId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -61,29 +77,57 @@ public async Task>>> GetActi public async Task>> GetTaskByIdAsync(int taskId) { BaseResponse response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.GetTaskByIdAsync(taskId).ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.GetTaskByIdAsync(taskId).ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (NotFoundException ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return BadRequest(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } /// /// Add new task. /// /// Role request details - [HttpPost] + [HttpPost("task")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task>> InsertTaskAsync(TaskDetailsRequest detailRequest) { BaseResponse response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.InsertTaskAsync(detailRequest).ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.InsertTaskAsync(detailRequest).ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (NotFoundException ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return BadRequest(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } /// @@ -91,33 +135,65 @@ public async Task>> InsertTaskAsy /// /// Id of task record /// Modified details for task record - [HttpPut("{taskId}")] + [HttpPut("tasks/{taskId}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task>> UpdateTaskAsync(int taskId, TaskDetailsRequest modifiedDetails) { BaseResponse response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.UpdateTaskAsync(taskId, modifiedDetails).ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.UpdateTaskAsync(taskId, modifiedDetails).ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (NotFoundException ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return BadRequest(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } /// /// Change status of task to inactive. /// /// Id of task record - [HttpDelete("{taskId}")] + [HttpDelete("tasks/{taskId}")] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task>> InactiveTaskAsync(int taskId) { BaseResponse response = new(ResponseStatus.Fail); - - response.Data = await _taskDetailsService.InactiveTaskAsync(taskId).ConfigureAwait(false); - response.Status = ResponseStatus.Success; - - return Ok(response); + try + { + response.Data = await _taskDetailsService.InactiveTaskAsync(taskId).ConfigureAwait(false); + response.Status = ResponseStatus.Success; + + return Ok(response); + } + catch (NotFoundException ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return BadRequest(response); + } + catch (Exception ex) + { + response.Message = ex.Message; + response.Status = ResponseStatus.Error; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } } } diff --git a/DotnetFoundation/DotnetFoundation.Application/Models/DTOs/TaskDetailsDTO/TaskDetailsRequest.cs b/DotnetFoundation/DotnetFoundation.Application/Models/DTOs/TaskDetailsDTO/TaskDetailsRequest.cs index ac8a60f..4d0351c 100644 --- a/DotnetFoundation/DotnetFoundation.Application/Models/DTOs/TaskDetailsDTO/TaskDetailsRequest.cs +++ b/DotnetFoundation/DotnetFoundation.Application/Models/DTOs/TaskDetailsDTO/TaskDetailsRequest.cs @@ -8,12 +8,13 @@ public record TaskDetailsRequest /// Gets the Description of the task. /// [Required(ErrorMessage = "Description is required")] + [StringLength(100, ErrorMessage = "Description Max Length is 100")] public string Description { get; init; } = string.Empty; /// /// Gets the Budgeted Hours of the task. /// [Required(ErrorMessage = "BudgetedHours is required")] - [Range(0, int.MaxValue, ErrorMessage = "BudgetedHours should be in range of 0 to int.Maxvalue")] + [Range(0, 50, ErrorMessage = "BudgetedHours should be in range of 0 to 50")] public int BudgetedHours { get; init; } /// /// Gets the user Id of the user the task is assigned to. @@ -23,5 +24,6 @@ public record TaskDetailsRequest /// /// Gets the Category of the task. /// + [StringLength(100, ErrorMessage = "Category Max Length is 100")] public string? Category { get; init; } } diff --git a/DotnetFoundation/DotnetFoundation.Services/Services/TaskDetailsService/TaskDetailsService.cs b/DotnetFoundation/DotnetFoundation.Services/Services/TaskDetailsService/TaskDetailsService.cs index 675de83..b1a8154 100644 --- a/DotnetFoundation/DotnetFoundation.Services/Services/TaskDetailsService/TaskDetailsService.cs +++ b/DotnetFoundation/DotnetFoundation.Services/Services/TaskDetailsService/TaskDetailsService.cs @@ -1,4 +1,5 @@ using AutoMapper; +using DotnetFoundation.Application.Exceptions; using DotnetFoundation.Application.Interfaces.Persistence; using DotnetFoundation.Application.Interfaces.Services; using DotnetFoundation.Application.Models.DTOs.TaskDetailsDTO; @@ -35,15 +36,15 @@ public async Task> GetActiveTasksAsync() public async Task GetTaskByIdAsync(int id) { - TaskDetails response = await _taskDetailsRepository.GetTaskByIdAsync(id).ConfigureAwait(false) - ?? throw new Exception($"Task with Id={id} does not exist"); + TaskDetails response = await _taskDetailsRepository.GetTaskByIdAsync(id).ConfigureAwait(false) + ?? throw new NotFoundException($"Task with Id={id} does not exist"); return _mapper.Map(response); } public async Task InsertTaskAsync(TaskDetailsRequest detailsRequest) { User? user = await _userRepository.GetUserByIdAsync(detailsRequest.AssignedTo).ConfigureAwait(false) - ?? throw new Exception($"AssignedTo with userId = \"{detailsRequest.AssignedTo}\" does not exist. Cannot add task."); + ?? throw new NotFoundException($"AssignedTo with userId = \"{detailsRequest.AssignedTo}\" does not exist. Cannot add task."); // Create new TaskDetails object and add relevant details TaskDetails taskDetails = new TaskDetails @@ -57,8 +58,8 @@ public async Task InsertTaskAsync(TaskDetailsRequest detail ModifiedBy = detailsRequest.AssignedTo, ModifiedOn = DateTime.UtcNow, }; - - int? taskId = await _taskDetailsRepository.InsertTaskAsync(taskDetails).ConfigureAwait(false) + + int? taskId = await _taskDetailsRepository.InsertTaskAsync(taskDetails).ConfigureAwait(false) ?? throw new Exception($"Error inserting TaskDetails for \"{detailsRequest.Description}\""); taskDetails.Id = (int)taskId; @@ -69,10 +70,10 @@ public async Task InsertTaskAsync(TaskDetailsRequest detail public async Task UpdateTaskAsync(int id, TaskDetailsRequest modifiedDetails) { TaskDetails? existingDetails = await _taskDetailsRepository.GetTaskByIdAsync(id).ConfigureAwait(false) - ?? throw new Exception($"Task with Id={id} does not exist"); + ?? throw new NotFoundException($"Task with Id={id} does not exist"); User? user = await _userRepository.GetUserByIdAsync(modifiedDetails.AssignedTo).ConfigureAwait(false) - ?? throw new Exception($"AssignedTo with userId = \"{modifiedDetails.AssignedTo}\" does not exist. Cannot add task."); + ?? throw new NotFoundException($"AssignedTo with userId = \"{modifiedDetails.AssignedTo}\" does not exist. Cannot add task."); TaskDetails? modifiedTask = await _taskDetailsRepository.UpdateTaskAsync(modifiedDetails, existingDetails).ConfigureAwait(false) ?? throw new Exception($"An error occurred while updating Task with id = \"{id}\""); @@ -85,7 +86,7 @@ public async Task InactiveTaskAsync(int id) TaskDetails? existingDetails = await _taskDetailsRepository.GetTaskByIdAsync(id).ConfigureAwait(false); if (existingDetails == null) { - throw new Exception($"Task with Id = \"{id}\" does not exist"); + throw new NotFoundException($"Task with Id = \"{id}\" does not exist"); } TaskDetails? response = await _taskDetailsRepository.InactiveTaskAsync(existingDetails).ConfigureAwait(false)