예제 #1
0
        public async Task <IActionResult> Information(InformationViewModel 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 currentPerformer = await _performerSchedulingService
                                   .GetPerformerByUserIdAsync(userId);

            if (currentPerformer?.RegistrationCompleted == false)
            {
                return(RedirectToAction(nameof(Schedule)));
            }

            var systems = await _performerSchedulingService
                          .GetSystemListWithoutExcludedBranchesAsync();

            var branchIds          = systems.SelectMany(_ => _.Branches).Select(_ => _.Id);
            var BranchAvailability = JsonConvert.DeserializeObject
                                     <List <int> >(model.BranchAvailabilityString)
                                     .Where(_ => branchIds.Contains(_)).ToList();

            if (BranchAvailability.Count == 0)
            {
                ModelState.AddModelError("BranchAvailability", "Please select the libraries where you are willing to perform.");
            }

            if (currentPerformer == null)
            {
                if (model.Images == null)
                {
                    ModelState.AddModelError("Images", "Please attach an image to submit.");
                }
                else 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 (model.References == null)
                {
                    ModelState.AddModelError("References", "Please attach a list of references to submit.");
                }
            }

            if (model.References != null &&
                !string.Equals(Path.GetExtension(model.References.FileName),
                               ".pdf",
                               StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError("References", "Please attach a .pdf file.");
            }

            if (ModelState.IsValid)
            {
                var performer = model.Performer;
                performer.AllBranches = BranchAvailability.Count == branchIds.Count();

                if (currentPerformer?.RegistrationCompleted == true)
                {
                    performer.Id = currentPerformer.Id;
                    performer    = await _performerSchedulingService.EditPerformerAsync(performer,
                                                                                        BranchAvailability);
                }
                else
                {
                    performer = await _performerSchedulingService.AddPerformerAsync(performer,
                                                                                    BranchAvailability);
                }

                if (model.References != null)
                {
                    using (var fileStream = model.References.OpenReadStream())
                    {
                        using (var ms = new MemoryStream())
                        {
                            fileStream.CopyTo(ms);
                            await _performerSchedulingService.SetPerformerReferencesAsync(
                                performer.Id, ms.ToArray(),
                                Path.GetExtension(model.References.FileName));
                        }
                    }
                }

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

                    return(RedirectToAction(nameof(Schedule)));
                }
                else
                {
                    ShowAlertSuccess("Information saved!");
                    return(RedirectToAction(nameof(Dashboard)));
                }
            }

            model.BranchCount        = systems.Sum(_ => _.Branches.Count);
            model.BranchAvailability = BranchAvailability;
            model.MaxUploadMB        = MaxUploadMB;
            model.Settings           = settings;
            model.Systems            = systems;

            PageTitle = "Performer Information";
            return(View(model));
        }