예제 #1
0
        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());
                }
            }
        }