Example #1
0
        private static List <StopInfo> findStops(string postcode, string type, int num)
        {
            PostcodeAPI postAPI = new PostcodeAPI();

            string  postResponse = postAPI.Execute(postcode);
            JObject json         = JObject.Parse(postResponse);

            string result = json["result"].ToString();

            PostcodeInfo postInfo = JsonConvert.DeserializeObject <PostcodeInfo>(result);

            int range = 100;

            string  tflResponse = tflAPI.stopTypes(type, range, postInfo.latitude, postInfo.longitude);
            JObject jStops      = JObject.Parse(tflResponse);

            string array = jStops["stopPoints"].ToString();

            List <StopInfo> stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);

            while (stops.Count == 0 && range <= 1000)
            {
                range      += 100;
                tflResponse = tflAPI.stopTypes(type, range, postInfo.latitude, postInfo.longitude);
                jStops      = JObject.Parse(tflResponse);

                array = jStops["stopPoints"].ToString();

                stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);
            }

            stops.Sort();

            return(stops);
        }
Example #2
0
 public CommandValidator(PostcodeInfo postcodeInfo)
 {
     RuleFor(c => c.AddressLine1).AddressLine1();
     RuleFor(c => c.AddressLine2).AddressLine2();
     RuleFor(c => c.Town).Town();
     RuleFor(c => c.County).County();
     RuleFor(c => c.Postcode).Postcode(_ => postcodeInfo);
 }
Example #3
0
            public CommandValidator(
                IEnumerable <VenueUploadRow> otherRows,
                PostcodeInfo postcodeInfo)
            {
                RuleFor(c => c.ProviderVenueRef)
                .ProviderVenueRef(_ => Task.FromResult(otherRows.Select(r => r.ProviderVenueRef)));

                RuleFor(c => c.Name)
                .VenueName(_ => Task.FromResult(otherRows.Select(r => r.VenueName)));

                RuleFor(c => c.AddressLine1).AddressLine1();
                RuleFor(c => c.AddressLine2).AddressLine2();
                RuleFor(c => c.Town).Town();
                RuleFor(c => c.County).County();
                RuleFor(c => c.Postcode).Postcode(_ => postcodeInfo);
                RuleFor(c => c.Email).Email();
                RuleFor(c => c.Telephone).PhoneNumber();
                RuleFor(c => c.Website).Website();
            }
Example #4
0
        private static List <StopInfo> findStops(string postcode, int num)
        {
            PostcodeAPI postAPI = new PostcodeAPI();

            string  postResponse = postAPI.Execute(postcode);
            JObject json         = JObject.Parse(postResponse);

            string result = json["result"].ToString();

            PostcodeInfo postInfo = JsonConvert.DeserializeObject <PostcodeInfo>(result);

            string  tflResponse = tflAPI.stopTypes("NaptanPublicBusCoachTram", postInfo.latitude, postInfo.longitude);
            JObject jStops      = JObject.Parse(tflResponse);

            string array = jStops["stopPoints"].ToString();

            List <StopInfo> stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);

            stops.Sort();

            return(stops.Take(num).ToList());
        }
        // internal for testing
        internal async Task <(UploadStatus uploadStatus, IReadOnlyCollection <VenueUploadRow> Rows)> ValidateVenueUploadFile(
            ISqlQueryDispatcher sqlQueryDispatcher,
            Guid venueUploadId,
            Guid providerId,
            VenueDataUploadRowInfoCollection rows)
        {
            // We need to ensure that any venues that have live offerings attached are not removed when publishing this
            // upload. We do that by adding an additional row to this upload for any venues that are not included in
            // this file that have live offerings attached. This must be done *before* validation so that duplicate
            // checks consider these additional added rows.

            var originalRowCount = rows.Count;

            var existingVenues = await sqlQueryDispatcher.ExecuteQuery(new GetVenueMatchInfoForProvider()
            {
                ProviderId = providerId
            });

            // For each row in the file try to match it to an existing venue
            var rowVenueIdMapping = MatchRowsToExistingVenues(rows, existingVenues);

            // Add a row for any existing venues that are linked to live offerings and haven't been matched
            var matchedVenueIds = rowVenueIdMapping.Where(m => m.HasValue).Select(m => m.Value).ToArray();
            var venuesWithLiveOfferingsNotInFile = existingVenues
                                                   .Where(v => v.HasLiveOfferings && !matchedVenueIds.Contains(v.VenueId))
                                                   .ToArray();

            rows = new VenueDataUploadRowInfoCollection(
                lastRowNumber: rows.LastRowNumber + venuesWithLiveOfferingsNotInFile.Length,
                rows: rows.Concat(
                    venuesWithLiveOfferingsNotInFile.Select((v, i) =>
                                                            new VenueDataUploadRowInfo(CsvVenueRow.FromModel(v), rowNumber: rows.LastRowNumber + i + 1, isSupplementary: true))));

            rowVenueIdMapping = rowVenueIdMapping.Concat(venuesWithLiveOfferingsNotInFile.Select(v => (Guid?)v.VenueId)).ToArray();

            // Grab PostcodeInfo for all of the valid postcodes in the file.
            // We need this for both the validator and to track whether the venue is outside of England
            var allPostcodeInfo = await GetPostcodeInfoForRows(sqlQueryDispatcher, rows);

            var uploadIsValid = true;
            var validator     = new VenueUploadRowValidator(rows, allPostcodeInfo);

            var upsertRecords = new List <SetVenueUploadRowsRecord>();

            for (int i = 0; i < rows.Count; i++)
            {
                var row       = rows[i].Data;
                var rowNumber = rows[i].RowNumber;

                var venueId            = rowVenueIdMapping[i] ?? Guid.NewGuid();
                var isSupplementaryRow = i >= originalRowCount;

                // A row is deletable if it is *not* matched to an existing venue that has attached offerings
                var isDeletable = rowVenueIdMapping[i] is null ||
                                  !existingVenues.Single(v => v.VenueId == rowVenueIdMapping[i]).HasLiveOfferings;

                row.ProviderVenueRef = row.ProviderVenueRef?.Trim();
                row.Postcode         = Postcode.TryParse(row.Postcode, out var postcode) ? postcode : row.Postcode;

                PostcodeInfo postcodeInfo = null;
                if (postcode != null)
                {
                    allPostcodeInfo.TryGetValue(postcode, out postcodeInfo);
                }

                var rowValidationResult = validator.Validate(row);
                var errors     = rowValidationResult.Errors.Select(e => e.ErrorCode).ToArray();
                var rowIsValid = rowValidationResult.IsValid;
                uploadIsValid &= rowIsValid;

                upsertRecords.Add(new SetVenueUploadRowsRecord()
                {
                    RowNumber        = rowNumber,
                    IsValid          = rowIsValid,
                    Errors           = errors,
                    IsSupplementary  = isSupplementaryRow,
                    OutsideOfEngland = postcodeInfo != null ? !postcodeInfo.InEngland : (bool?)null,
                    VenueId          = venueId,
                    IsDeletable      = isDeletable,
                    ProviderVenueRef = row.ProviderVenueRef,
                    VenueName        = row.VenueName,
                    AddressLine1     = row.AddressLine1,
                    AddressLine2     = row.AddressLine2,
                    Town             = row.Town,
                    County           = row.County,
                    Postcode         = row.Postcode,
                    Email            = row.Email,
                    Telephone        = row.Telephone,
                    Website          = row.Website
                });
            }

            var updatedRows = await sqlQueryDispatcher.ExecuteQuery(new SetVenueUploadRows()
            {
                VenueUploadId = venueUploadId,
                ValidatedOn   = _clock.UtcNow,
                Records       = upsertRecords
            });

            await sqlQueryDispatcher.ExecuteQuery(new SetVenueUploadProcessed()
            {
                VenueUploadId         = venueUploadId,
                ProcessingCompletedOn = _clock.UtcNow,
                IsValid = uploadIsValid
            });

            var uploadStatus = uploadIsValid ? UploadStatus.ProcessedSuccessfully : UploadStatus.ProcessedWithErrors;

            return(uploadStatus, updatedRows);
        }