public async Task <ActionResult> ModifyTaskType([FromBody] TaskTypeDTO taskType) { if (TaskTypeExists(taskType.TypeID)) { // Call BLL TaskType Modify method with all the parameters object BLLResponse = new TaskTypeBLL(_context).ModifyTaskTypeBLL(taskType: taskType); if (BLLResponse.GetType().BaseType == typeof(Exception)) { // Create log entries for Debug log ((APIException)BLLResponse).Exceptions.ForEach(ex => Logger.Msg <TaskTypesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { TaskTypeDTO modTaskType = (TaskTypeDTO)BLLResponse; // Find the existing record based on ID TaskType currentRecord = _context.TaskTypes.Where(x => x.TypeID == modTaskType.TypeID).First(); // Modify the record currentRecord.Description = modTaskType.Description; // Save changes await _context.SaveChangesAsync(); Logger.Msg <TaskTypesController>($"[{User.FindFirstValue("email")}] [MODIFY] TypeID: {currentRecord.TypeID} successful", Serilog.Events.LogEventLevel.Information); // Return modified record as a DTO TaskTypeDTO response = new TaskTypeDTO(currentRecord); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <TaskTypesController>($"[MODIFY] Database sync error {ex.Message}", Serilog.Events.LogEventLevel.Error); // Return response to client return(StatusCode(500, new { errors = "Database update failed. Contact the administrator to resolve this issue." })); } } } else { return(NotFound()); } } // End of ModifyTaskType
public async Task <ActionResult> AddTaskType(string description) { // Call BLL TaskType Add method with all the parameters object BLLResponse = new TaskTypeBLL(_context).AddTaskTypeBLL(description: description); // Get the base class for the response // Ref: https://docs.microsoft.com/en-us/dotnet/api/system.type.basetype?view=netcore-3.1 if (BLLResponse.GetType().BaseType == typeof(Exception)) { // Create log entries for Debug log ((APIException)BLLResponse).Exceptions.ForEach(ex => Logger.Msg <TaskTypesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { TaskType newTaskType = new TaskType { Description = ((TaskTypeDTO)BLLResponse).Description }; // Create the record _context.TaskTypes.Add(newTaskType); await _context.SaveChangesAsync(); Logger.Msg <TaskTypesController>($"[{User.FindFirstValue("email")}] [ADD] TaskType '{description}' successful", Serilog.Events.LogEventLevel.Information); // Convert back to DTO and return to user TaskTypeDTO response = new TaskTypeDTO(newTaskType); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <TaskTypesController>($"[ADD] Database sync error {ex.Message}", Serilog.Events.LogEventLevel.Error); // Return response to client return(StatusCode(500, new { errors = "Database update failed. Contact the administrator to resolve this issue." })); } } } // End of AddTaskType