Пример #1
0
        public async Task <IActionResult> Program(ProgramViewModel 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);

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

            var ageGroups = await _performerSchedulingService.GetAgeGroupsAsync();

            var ageSelection = ageGroups
                               .Where(_ => model.AgeSelection?.Contains(_.Id) == true)
                               .Select(_ => _.Id)
                               .ToList();

            if (ageSelection.Count == 0)
            {
                ModelState.AddModelError("AgeSelection", "Please select age groups.");
            }

            if (model.Images?.Count > 0)
            {
                var extensions = model.Images.Select(_ => Path.GetExtension(_.FileName).ToLower());
                if (extensions.Any(_ => !ValidImageExtensions.Contains(_)))
                {
                    ModelState.AddModelError("Images", $"Image must be one of the following types: {string.Join(", ", ValidImageExtensions)}");
                }
                else if (model.Images.Sum(_ => _.Length) > MaxUploadMB * MBSize)
                {
                    ModelState.AddModelError("Images", $"Please limit uploads to a max of {MaxUploadMB}MB.");
                }
            }

            if (ModelState.IsValid)
            {
                if (model.EditingProgram)
                {
                    try
                    {
                        var program = await _performerSchedulingService.UpdateProgramAsync(
                            model.Program, ageSelection);

                        ShowAlertSuccess("Program saved!");
                        return(RedirectToAction(nameof(ProgramDetails), new { id = program.Id }));
                    }
                    catch (GraException gex)
                    {
                        ShowAlertDanger("Unable to update Program: ", gex);
                        return(RedirectToAction(nameof(Dashboard)));
                    }
                }
                else
                {
                    model.Program.PerformerId = performer.Id;
                    var program = await _performerSchedulingService.AddProgramAsync(model.Program,
                                                                                    ageSelection);

                    if (model.Images != null)
                    {
                        foreach (var image in model.Images)
                        {
                            using (var fileStream = image.OpenReadStream())
                            {
                                using (var ms = new MemoryStream())
                                {
                                    fileStream.CopyTo(ms);
                                    await _performerSchedulingService.AddProgramImageAsync(
                                        program.Id, ms.ToArray(), Path.GetExtension(image.FileName));
                                }
                            }
                        }
                    }
                }

                if (!performer.RegistrationCompleted)
                {
                    await _performerSchedulingService.SetPerformerRegistrationCompeltedAsync(
                        performer.Id);

                    ShowAlertSuccess("Registration completed!");
                }
                else
                {
                    ShowAlertSuccess("Program added!");
                }

                return(RedirectToAction(nameof(Dashboard)));
            }

            var ageList = new SelectList(ageGroups, "Id", "Name");

            if (ageList.Count() == 1)
            {
                ageList.First().Selected = true;
            }
            model.AgeList     = ageList;
            model.MaxUploadMB = MaxUploadMB;
            return(View(model));
        }