public async Task <ActionResult> ModifyAttendanceState([FromBody] AttendanceStateDTO attendanceState) { if (AttendanceStateExists(attendanceState.StateID)) { // Call BLL AttendanceState Modify method with all the parameters object BLLResponse = new AttendanceStateBLL(_context).ModifyAttendanceStateBLL(attendanceState: attendanceState); if (BLLResponse.GetType().BaseType == typeof(Exception)) { // Create log entries for Debug log ((APIException)BLLResponse).Exceptions.ForEach(ex => Logger.Msg <AttendanceStatesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { AttendanceStateDTO modAttendanceState = (AttendanceStateDTO)BLLResponse; // Find the existing record based on ID AttendanceState currentRecord = _context.AttendanceStates.Where(x => x.StateID == modAttendanceState.StateID).First(); // Modify the record currentRecord.Description = modAttendanceState.Description; // Save changes await _context.SaveChangesAsync(); Logger.Msg <AttendanceStatesController>($"[{User.FindFirstValue("email")}] [MODIFY] StateID: {currentRecord.StateID} successful", Serilog.Events.LogEventLevel.Information); // Return modified record as a DTO AttendanceStateDTO response = new AttendanceStateDTO(currentRecord); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <AttendanceStatesController>($"[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 ModifyAttendanceState
public async Task <ActionResult> AddAttendanceState(string description) { // Call BLL Attendance Add method with all the parameters object BLLResponse = new AttendanceStateBLL(_context).AddAttendanceStateBLL(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 <AttendanceStatesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { AttendanceState newAttendanceState = new AttendanceState { Description = ((AttendanceStateDTO)BLLResponse).Description }; // Create the record _context.AttendanceStates.Add(newAttendanceState); await _context.SaveChangesAsync(); Logger.Msg <AttendanceStatesController>($"[{User.FindFirstValue("email")}] [ADD] AttendanceState '{description}' successful", Serilog.Events.LogEventLevel.Information); // Convert back to DTO and return to user AttendanceStateDTO response = new AttendanceStateDTO(newAttendanceState); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <AttendanceStatesController>($"[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 AddAttendanceState