コード例 #1
0
        public async Task <IActionResult> Schedule(ScheduleViewModel model)
        {
            var settings = await _performerSchedulingService.GetSettingsAsync();

            var schedulingStage = _performerSchedulingService.GetSchedulingStage(settings);

            if (schedulingStage != PsSchedulingStage.RegistrationOpen)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var userId    = GetId(ClaimType.UserId);
            var performer = await _performerSchedulingService.GetPerformerByUserIdAsync(userId,
                                                                                        includeSchedule : true);

            if (performer == null)
            {
                return(RedirectToAction(nameof(Information)));
            }
            else if (performer.SetSchedule && !performer.RegistrationCompleted)
            {
                return(RedirectToAction(nameof(Program)));
            }

            var schedule = new List <PsScheduleDate>();

            try
            {
                schedule = JsonConvert.DeserializeObject <List <PsScheduleDate> >(model.JsonSchedule);

                foreach (var date in schedule)
                {
                    date.ParsedDate = DateTime.Parse($"{date.Year}-{date.Month}-{date.Date}");
                    if (Enum.TryParse(date.Availability, out PsScheduleDateStatus status))
                    {
                        date.Status = status;
                    }
                    else
                    {
                        date.Status = PsScheduleDateStatus.Available;
                    }
                }

                try
                {
                    await _performerSchedulingService.EditPerformerScheduleAsync(performer.Id,
                                                                                 schedule);

                    if (!performer.RegistrationCompleted)
                    {
                        return(RedirectToAction(nameof(Program)));
                    }
                    else
                    {
                        ShowAlertSuccess("Schedule saved!");
                        return(RedirectToAction(nameof(Dashboard)));
                    }
                }
                catch (GraException gex)
                {
                    ShowAlertDanger("Error saving schedule: ", gex);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error submitting schedule for user {userId}: {ex}", ex);
                ShowAlertDanger($"There was an error with your schedule, please try submitting " +
                                $"it again or contact {settings.ContactEmail} for assistance.");
            }

            model.BlackoutDates = (await _performerSchedulingService.GetBlackoutDatesAsync())
                                  .ToList();
            model.EditingSchedule = performer.RegistrationCompleted;
            model.StartDate       = settings.ScheduleStartDate.Value;
            model.EndDate         = settings.ScheduleEndDate.Value;

            if (performer.Schedule != null)
            {
                model.ScheduleDates = performer.Schedule.ToList();
            }

            PageTitle = "Schedule";
            return(View(model));
        }