Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync(int?companyId)
        {
            if (!await FetchData(companyId).ConfigureAwait(false))
            {
                return(RedirectToPage("/Index"));
            }

            if (!ModelState.IsValid)
            {
                return(this.TurboPage());
            }

            var selectedCompanyId = 0;

            if (Role.CanAdministerSystem)
            {
                if (string.IsNullOrWhiteSpace(Input.CompanyId))
                {
                    return(this.TurboPage());
                }

                selectedCompanyId = int.Parse(Input.CompanyId);
            }
            else
            {
                selectedCompanyId = companyId.Value;
            }

            var coords = await coordinates.GetCoordinates(Input.VenuePostCode ?? "").ConfigureAwait(false);

            string latitude  = null;
            string longitude = null;

            if (coords.HasValue)
            {
                latitude  = coords.Value.Latitude.ToString();
                longitude = coords.Value.Longitude.ToString();
            }

            int?selectedImageId = null;

            if (Input.ImageId != null)
            {
                int.Parse(Input.ImageId);
            }

            var realImage = Images.Find(i => i.ImageId == selectedImageId);

            if (realImage == null || realImage.CompanyId != selectedCompanyId)
            {
                selectedImageId = null;
            }

            var result = coords.HasValue
                ? await venues.CreateVenueWithCoordinates(new Venue
            {
                CompanyId        = selectedCompanyId,
                VenueName        = Input.VenueName,
                VenueCode        = "",
                VenueAddress     = Input.VenueAddress,
                VenueAddress2    = Input.VenueAddress2,
                VenueAddress3    = Input.VenueAddress3,
                VenueCounty      = Input.VenueCounty,
                VenuePostCode    = Input.VenuePostCode,
                VenueContact     = Input.VenueContact,
                VenuePhone       = Input.VenuePhone,
                VenueDescription = Input.VenueDescription,
                VenueNotes       = "",
                VenueLatitude    = latitude,
                VenueLongitude   = longitude,
                ImageId          = selectedImageId,
                ReferenceCode    = Guid.NewGuid().ToString()
            }).Ensure(c => c.HasValue, "Venue was created")
                         .OnSuccess(c => c.Value)
                         .ConfigureAwait(false)
                : await venues.CreateVenue(new Venue
            {
                CompanyId        = selectedCompanyId,
                VenueName        = Input.VenueName,
                VenueCode        = "",
                VenueAddress     = Input.VenueAddress,
                VenueAddress2    = Input.VenueAddress2,
                VenueAddress3    = Input.VenueAddress3,
                VenueCounty      = Input.VenueCounty,
                VenuePostCode    = Input.VenuePostCode,
                VenueContact     = Input.VenueContact,
                VenuePhone       = Input.VenuePhone,
                VenueDescription = Input.VenueDescription,
                VenueNotes       = "",
                VenueLatitude    = latitude,
                VenueLongitude   = longitude,
                ImageId          = selectedImageId,
                ReferenceCode    = Guid.NewGuid().ToString()
            }).Ensure(c => c.HasValue, "Venue was created")
                         .OnSuccess(c => c.Value)
                         .ConfigureAwait(false);

            if (!result.IsSuccess)
            {
                return(this.Page());
            }

            await AddVenuesWithExternalPaymentProvider(selectedCompanyId).ConfigureAwait(false);

            var selectedDocumentIds = new List <VenueDocument>();

            if (Input.DocumentIds?.Count > 0)
            {
                selectedDocumentIds = Input.DocumentIds
                                      .GetRange(0, Math.Min(Input.DocumentIds.Count, 100))
                                      .Select(id => int.Parse(id))
                                      .Where(id => DocumentLookup.ContainsKey(id) && DocumentLookup[id].CompanyId == selectedCompanyId)
                                      .Select(id => new VenueDocument
                {
                    VenueId    = result.Value,
                    DocumentId = id
                })
                                      .ToList();
            }

            return((await venueDocuments.CreateVenueDocuments(selectedDocumentIds)
                    .OnSuccess(() => this.RedirectToPage("/Venue", new
            {
                venueId = result.Value
            }))
                    .OnFailure(() => this.Page())
                    .ConfigureAwait(false)).Value);
        }