public async Task OnGetAsync(long?id) { if (id.HasValue) { Lawyer lawyer = await _lawyerService.GetLawyerByPrincipalAsync(User); LawyerSchedule schedule = lawyer.LawyerSchedules.FirstOrDefault(ls => ls.ID == id.Value); ScheduleId = id; TimeBegin = schedule.TimeBegin; TimeEnd = schedule.TimeEnd; Date = schedule.TimeBegin.Date; if (schedule.RecurringDays != RecurringDays.None) { IsRecurring = true; Sunday = (schedule.RecurringDays & RecurringDays.Sunday) > 0; Monday = (schedule.RecurringDays & RecurringDays.Monday) > 0; Tuesday = (schedule.RecurringDays & RecurringDays.Tuesday) > 0; Wednesday = (schedule.RecurringDays & RecurringDays.Wednesday) > 0; Thursday = (schedule.RecurringDays & RecurringDays.Thursday) > 0; Friday = (schedule.RecurringDays & RecurringDays.Friday) > 0; Saturday = (schedule.RecurringDays & RecurringDays.Saturday) > 0; } } else { Date = DateTime.Today; } }
/// <summary> /// Displays a confirmation page to determine whether the user really wants to delete the specified schedule /// </summary> /// <param name="id">The primary key for the LawyerSchedule entity to delete</param> /// <returns>PageResult if the signed in user owns the schedule and the schedule exists. HTTP 404 otherwise</returns> public async Task <IActionResult> OnGetAsync(long?id) { // Make sure the logged in user actually owns the specified schedule. If so, return PageResult if (id.HasValue) { Lawyer lawyer = await _lawyerService.GetLawyerByPrincipalAsync(User); LawyerSchedule = lawyer.LawyerSchedules.FirstOrDefault(ls => ls.ID == id.Value); if (LawyerSchedule != null) { return(Page()); } } // Either the schedule couldn't be found or it wasn't owned by the signed in user. Return HTTP 404 return(NotFound()); }
public async Task <bool> UpdateScheduleAsync(LawyerSchedule lawyerSchedule) { if (lawyerSchedule is null || !(await _context.LawyerSchedules.AnyAsync(ls => ls.ID == lawyerSchedule.ID))) { _logger.LogError("Tried to update null or non-existant schedule"); return(false); } _context.LawyerSchedules.Update(lawyerSchedule); try { await _context.SaveChangesAsync(); return(true); } catch { _logger.LogError("Could not commit lawyer schedule update"); return(false); } }
public async Task <bool> AddScheduleAsync(LawyerSchedule lawyerSchedule) { if (lawyerSchedule is null) { _logger.LogError("Tried to add null object as schedule"); return(false); } await _context.LawyerSchedules.AddAsync(lawyerSchedule); try { await _context.SaveChangesAsync(); return(true); } catch { _logger.LogError("Could not commit lawyer schedule creation"); return(false); } }
public async Task <bool> DeleteScheduleAsync(LawyerSchedule lawyerSchedule) { if (lawyerSchedule is null || !(await _context.LawyerSchedules.AnyAsync(ls => ls.ID == lawyerSchedule.ID))) { _logger.LogError("Tried to remove null or non-existant schedule from database"); return(false); } _context.LawyerSchedules.Remove(lawyerSchedule); try { await _context.SaveChangesAsync(); return(true); } catch { _logger.LogError("Unable to commit deletion of schedule from database"); return(false); } }
/// <summary> /// The user has confirmed they would like to delete the specified schedule. Delete it. /// </summary> /// <param name="id">The primary key for the LawyerSchedule entity to delete</param> /// <returns>RedirectToPageResult to Index upon successful deletion. HTTP 404 is returned if the /// signed in user does not own the schedule or the schedule couldn't be found. Returns HTTP 500 /// if a database error occurred.</returns> public async Task <IActionResult> OnPostAsync(long?id) { if (id.HasValue) { Lawyer lawyer = await _lawyerService.GetLawyerByPrincipalAsync(User); LawyerSchedule = lawyer.LawyerSchedules.FirstOrDefault(ls => ls.ID == id.Value); if (LawyerSchedule != null) { // The provided id checks out; attempt to delete the schedule if (await _lawyerService.DeleteScheduleAsync(LawyerSchedule)) { return(RedirectToPage("Index")); } // A database error has occurred; return 500 to indicate database error return(StatusCode(StatusCodes.Status500InternalServerError)); } } // The schedule could not be found or it did not belong to the signed in user return(NotFound()); }
public async Task <IActionResult> OnPostAsync(long?id) { if (!ModelState.IsValid) { return(Page()); } Lawyer lawyer = await _lawyerService.GetLawyerByPrincipalAsync(User); LawyerSchedule schedule; if (id.HasValue) { schedule = lawyer.LawyerSchedules.FirstOrDefault(ls => ls.ID == id.Value); // Prevent any fraudulent edits to schedules the lawyer does not own if (schedule is null) { return(StatusCode(StatusCodes.Status403Forbidden)); } } else { schedule = new LawyerSchedule(); } schedule.LawyerId = lawyer.ApplicationUserId; if (IsRecurring) { schedule.TimeBegin = new DateTime(TimeBegin.Ticks); schedule.TimeEnd = new DateTime(TimeEnd.Ticks); // Construct RecurringDays based on checkbox input schedule.RecurringDays = (Sunday ? RecurringDays.Sunday : RecurringDays.None) | (Monday ? RecurringDays.Monday : RecurringDays.None) | (Tuesday ? RecurringDays.Tuesday : RecurringDays.None) | (Wednesday ? RecurringDays.Wednesday : RecurringDays.None) | (Thursday ? RecurringDays.Thursday : RecurringDays.None) | (Friday ? RecurringDays.Friday : RecurringDays.None) | (Saturday ? RecurringDays.Saturday : RecurringDays.None); } else { schedule.TimeBegin = Date.Add(TimeBegin.TimeOfDay); schedule.TimeEnd = Date.Add(TimeEnd.TimeOfDay); schedule.RecurringDays = RecurringDays.None; } if (id.HasValue) { if (await _lawyerService.UpdateScheduleAsync(schedule)) { return(RedirectToPage("/Lawyers/Index")); } else { return(Page()); } } else { if (await _lawyerService.AddScheduleAsync(schedule)) { return(RedirectToPage("/Lawyers/Index")); } else { return(Page()); } } }