public async Task <ActionResult <IEnumerable <NoticeDTO> > > Get(int noticeID, int cohortID, int staffID, DateTime validFrom) { // Check if the current user is a staff member (or Super Admin) bool isUserStaff = User.IsInRole("Staff") || User.IsInRole("SuperAdmin"); // Call GetNoticekBLL method with all the parameters object BLLResponse = new NoticeBLL(_context).GetNoticeBLL(noticeID: noticeID, cohortID: cohortID, staffID: staffID, validFrom: validFrom, isUserStaff: isUserStaff); // 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 <NoticesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { NoticeDTO BLLResponseDTO = (NoticeDTO)BLLResponse; // Apply all the criteria with supplied or default values from BLL. Limit Student access to notices issued up to current date. IQueryable <Notice> dbRequest = _context.Notices .Where(x => x.ValidFrom >= BLLResponseDTO.ValidFrom && (isUserStaff || x.ValidFrom <= DateTime.Today)); if (BLLResponseDTO.CohortID != 0) { dbRequest = dbRequest.Where(x => x.CohortID == BLLResponseDTO.CohortID); } if (BLLResponseDTO.StaffID != 0 && isUserStaff) { dbRequest = dbRequest.Where(x => x.StaffID == BLLResponseDTO.StaffID); } List <Notice> dbResponse = await dbRequest.ToListAsync(); // Convert result to TaskDTO List <NoticeDTO> response = new List <NoticeDTO>(); dbResponse.ForEach(x => response.Add(new NoticeDTO(x))); Logger.Msg <NoticesController>($"[{User.FindFirstValue("email")}] [GET] ", Serilog.Events.LogEventLevel.Debug); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <TasksController>($"[GET] 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 Get
public async Task <ActionResult> ModifyNotice([FromBody] NoticeDTO notice) { if (NoticeExists(notice.NoticeID)) { // Call BLL Notice Modify method with all the parameters object BLLResponse = new NoticeBLL(_context).ModifyNoticeBLL(notice: notice, userClaims: User); if (BLLResponse.GetType().BaseType == typeof(Exception)) { // Create log entries for Debug log ((APIException)BLLResponse).Exceptions.ForEach(ex => Logger.Msg <NoticesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { NoticeDTO modNotice = (NoticeDTO)BLLResponse; // Find the existing record based on ID Notice currentRecord = _context.Notices.Where(x => x.NoticeID == modNotice.NoticeID).First(); // Modify the record currentRecord.HTML = modNotice.HTML; currentRecord.Markdown = modNotice.Markdown; currentRecord.ValidFrom = modNotice.ValidFrom; currentRecord.StaffID = modNotice.StaffID; // Save changes await _context.SaveChangesAsync(); Logger.Msg <NoticesController>($"[{User.FindFirstValue("email")}] [MODIFY] NoticeID: {currentRecord.NoticeID} successful", Serilog.Events.LogEventLevel.Information); // Return modified record as a DTO NoticeDTO response = new NoticeDTO(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 ModifyNotice
public async Task <ActionResult> AddNotice(NoticeDTO notice) { // Call BLL Notice Add method with all the parameters object BLLResponse = new NoticeBLL(_context).AddNoticeBLL(notice: notice, userClaims: User); // 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 <NoticesController>((Exception)ex, Serilog.Events.LogEventLevel.Debug)); // Return response from API return(BadRequest(new { errors = ((APIException)BLLResponse).Exceptions.Select(x => x.Message).ToArray() })); } else { try { // Convert BLLResponse to NoticeDTO object NoticeDTO newNoticeDTO = (NoticeDTO)BLLResponse; // Create a new Notice object Notice newNotice = new Notice { HTML = newNoticeDTO.HTML, Markdown = newNoticeDTO.Markdown, CohortID = newNoticeDTO.CohortID, StaffID = newNoticeDTO.StaffID, ValidFrom = newNoticeDTO.ValidFrom }; // Create the record _context.Notices.Add(newNotice); await _context.SaveChangesAsync(); Logger.Msg <NoticesController>($"[{User.FindFirstValue("email")}] [ADD] Notice '{newNotice.NoticeID}' successful", Serilog.Events.LogEventLevel.Information); // Convert back to DTO and return to user NoticeDTO response = new NoticeDTO(newNotice); return(Ok(response)); } catch (Exception ex) { // Local log entry. Database reconciliation issues are more serious so reported as Error Logger.Msg <NoticesController>($"[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 AddNotice