Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(int venueId)
        {
            if (!ModelState.IsValid)
            {
                await FetchData(venueId).ConfigureAwait(false);

                return(this.TurboPage());
            }

            if (!await FetchData(venueId).ConfigureAwait(false))
            {
                return(this.RedirectToPage("/Index"));
            }

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

            string latitude  = null;
            string longitude = null;

            int?selectedImageId = Input.ImageId == null ? null : int.Parse(Input.ImageId) as int?;

            if (Input.ImageId == null || !Images.Exists(i => i.ImageId == selectedImageId))
            {
                selectedImageId = null;
            }

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

            var servingType = ServingTypeHelper.ValidateServingType(Input.ServingType) ? Input.ServingType : 0;

            var patch = new VenuePatch
            {
                ResourceId = venueId,
                VenueName  = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueName
                },
                VenueAddress = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueAddress
                },
                VenueAddress2 = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueAddress2
                },
                VenueAddress3 = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueAddress3
                },
                VenueCounty = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueCounty
                },
                VenuePostCode = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenuePostCode
                },
                VenuePhone = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenuePhone
                },
                VenueContact = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueContact
                },
                VenueDescription = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = Input.VenueDescription
                },
                VenueLatitude = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = latitude
                },
                VenueLongitude = new PatchOperation <string> {
                    Operation = OperationKind.Update, Value = longitude
                },
                ServingType = new PatchOperation <int> {
                    Operation = OperationKind.Update, Value = servingType
                }
            };

            if (selectedImageId != null)
            {
                patch.ImageId = new PatchOperation <int> {
                    Operation = OperationKind.Update, Value = selectedImageId.Value
                };
            }

            if (Input.VenueProgress != null && Role.CanAdministerSystem)
            {
                patch.Progress = new PatchOperation <int> {
                    Operation = OperationKind.Update, Value = Input.VenueProgress.Value
                };
            }

            var result = await venues.UpdateVenue(patch)
                         .OnSuccess(() => tags.ReplaceVenueTags(venueId, (Input.Tags ?? "").Trim().Split(',').ToList())
                                    .OnSuccess(() => venueDocuments.ReplaceVenueDocuments(venueId, (Input.DocumentIds ?? new List <string>()).Select(id => new VenueDocument {
                VenueId = venueId, DocumentId = int.Parse(id)
            }).ToList())))
                         .OnSuccess(() => venues.UpdateVenueCoordinates(venueId))
                         .ConfigureAwait(false);

            if (result.IsSuccess)
            {
                await UpdateVenuesWithExternalPaymentProvider(Venue.CompanyId).ConfigureAwait(false);
                await UpdateVenueOpeningTimes().ConfigureAwait(false);

                return(this.RedirectToPage("/Venue", new
                {
                    venueId
                }));
            }
            else
            {
                return(this.Page());
            }
        }
        public void CreateInitialReportShell(int reportId, CoordinatePartsModel coordinatePart)
        {
            // check dbo.component.name on coordinatePart.Name
            // if it exists, return the id
            // else create, return the id
            var component   = _componentRepository.SelectByName(coordinatePart.Name.Trim());
            var componentId = component.Id;

            if (componentId == 0)
            {
                componentId = _componentRepository.Insert(new ComponentModel()
                {
                    Name = coordinatePart.Name.Trim()
                });
            }

            // check dbo.oss_index.component_id
            // if it exists, check `expire_date`
            //    - if older than 30 days, call remote api
            //    - insert results if any to dbo.oss_index_vulnerabilities
            //    - update `expire_date` = NOW
            // else
            //    - create with `expire_date` = NOW
            //    - call remote api
            //    - insert results if any to dbo.oss_index_vulnerabilities
            var ossIndex   = _ossIndexRepository.SelectByComponentId(componentId);
            var ossIndexId = ossIndex.Id;

            if (ossIndexId == 0)
            {
                ossIndex = new OssIndexModel()
                {
                    ComponentId    = componentId,
                    ExpireDate     = DateTime.Now.AddMonths(1),
                    HttpStatus     = (int)HttpStatusCode.Processing,
                    HttpStatusDate = DateTime.Now
                };

                ossIndexId = _ossIndexRepository.Insert(ossIndex);
                ossIndex   = _ossIndexRepository.Select(ossIndexId);
            }

            /* TODO
             *
             * 1. this is always zero as we cannot pass things like `1.4.0` as a decimal, consider deprecating `[vulnuscloud].[dbo].[oss_index].[version]` as this data is already in `[vulnuscloud].[dbo].[oss_index].[coordinates]`
             * 2. [vulnuscloud].[dbo].[oss_index].[coordinates] should be normalized:
             *      `pkg:Nuget/[email protected]`
             *      > pkg: is known, comes from `_coordinatesService`
             *      > Nuget/ should rather be stored as `[vulnuscloud].[dbo].[oss_index].[package_type_id]` - then this links to PackageTypeRepository
             *      > BeITMemcached@ can be read from [vulnuscloud].[dbo].[component].[id] = [vulnuscloud].[dbo].[oss_index].[component_id]
             *      > 1.4.0 could then be stored as [vulnuscloud].[dbo].[oss_index].[version]
             *
             *      [vulnuscloud].[dbo].[oss_index].[coordinates] could then be generated when needed.
             */

            if (decimal.TryParse(coordinatePart.Version, out decimal coordinatePartVersion))
            {
                ossIndex.Version = coordinatePartVersion;
            }

            ossIndex.Coordinates = _coordinatesService.GetCoordinates(coordinatePart);
            _ossIndexRepository.Update(ossIndex);

            _reportLinesRepository.Insert(new ReportLinesModel()
            {
                OssIndexId = ossIndexId,
                ReportId   = reportId
            });
        }
Beispiel #3
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);
        }