public async Task <ActionResult> ModifyCohort([FromBody] CohortDTO cohort) { if (CohortExists(cohort.CohortID)) { // Call BLL Cohort Add method with all the parameters object BLLResponse = new CohortBLL(_context).ModifyCohortBLL(cohort: cohort); // 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 <CohortsController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { // Find the existing record based on ID Cohort currentRecord = _context.Cohorts.Where(x => x.CohortID == cohort.CohortID).First(); // Modify the record currentRecord.Name = ((Cohort)BLLResponse).Name; currentRecord.StartDate = ((Cohort)BLLResponse).StartDate; currentRecord.EndDate = ((Cohort)BLLResponse).EndDate; // Save changes await _context.SaveChangesAsync(); Logger.Msg <CohortsController>($"[{User.FindFirstValue("email")}] [MODIFY] CohortID: {currentRecord.CohortID} successful", Serilog.Events.LogEventLevel.Information); // Return modified record as a DTO CohortDTO response = new CohortDTO(currentRecord); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <CohortsController>($"[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 ModifyCohort
public async Task <ActionResult> AddCohort(string name, DateTime startDate, DateTime endDate) { // This will by default check if there is Authorization to execute. If there isn't an authorization, then API server automatically // returns 401 response. // Call BLL Cohort Add method with all the parameters object BLLResponse = new CohortBLL(_context).AddCohortBLL(name: name, startDate: startDate, endDate: endDate); // 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 <CohortsController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { _context.Cohorts.Add((Cohort)BLLResponse); await _context.SaveChangesAsync(); Logger.Msg <CohortsController>($"[{User.FindFirstValue("email")}] [ADD] '{name}' successful", Serilog.Events.LogEventLevel.Information); CohortDTO response = new CohortDTO((Cohort)BLLResponse); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <CohortsController>($"[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 AddCohort