Example #1
0
        public async Task <List <Country> > GetCountries(IEnumerable <string> suppliersFilter, string languageCode)
        {
            var suppliersKeys   = suppliersFilter.ToArray();
            var countriesQuery  = _context.Countries.Where(c => c.IsActive);
            var localitiesQuery = _context.Localities.Where(l => l.IsActive);

            // Country list must be not dependant from supplier
            // if (suppliersKeys.Any())
            // {
            //     countriesQuery = countriesQuery.Where(c => EF.Functions.JsonExistAny(c.SupplierCountryCodes, suppliersKeys));
            //     localitiesQuery = localitiesQuery.Where(l => EF.Functions.JsonExistAny(l.SupplierLocalityCodes, suppliersKeys));
            // }

            var countries = await(from c in countriesQuery
                                  join l in localitiesQuery on c.Id equals l.CountryId
                                  select new
            {
                CountryId     = c.Id,
                CountryCode   = c.Code,
                CountryNames  = c.Names,
                LocalityId    = l.Id,
                LocalityNamеs = l.Names
            }).ToListAsync();

            return((from c in countries
                    group c by new { c.CountryId, c.CountryCode, c.CountryNames }
                    into gr
                    select new
                    Country(gr.Key.CountryCode, HtId.Create(MapperLocationTypes.Country, gr.Key.CountryId),
                            gr.Key.CountryNames.GetValueOrDefault(languageCode),
                            gr.Select(l => new Locality(HtId.Create(MapperLocationTypes.Locality, l.LocalityId),
                                                        l.LocalityNamеs.GetValueOrDefault(languageCode))).ToList())).ToList());
        }
        /// <summary>
        /// Get LocalityInfo of locality
        /// </summary>
        /// <param name="localityHtId">Id of locality like Locality_12345</param>
        /// <param name="languageCode"></param>
        /// <returns></returns>
        public async Task <Result <LocalityInfo> > GetLocalityInfo(string localityHtId, string languageCode)
        {
            var(_, isFailure, (type, id), error) = HtId.Parse(localityHtId);
            if (isFailure)
            {
                return(Result.Failure <LocalityInfo>(error));
            }

            if (type != MapperLocationTypes.Locality)
            {
                return(Result.Failure <LocalityInfo>($"{localityHtId} is not Locality"));
            }

            var locality = await _context.Localities
                           .Include(x => x.Country)
                           .Where(l => l.Id == id || l.LocalitySynonyms.Any(s => s.ExLocalityId == id))
                           .Where(l => l.IsActive)
                           .SingleOrDefaultAsync();

            if (locality == default)
            {
                return(Result.Failure <LocalityInfo>($"Cannot find locality for {localityHtId}"));
            }

            var localityInfo = new LocalityInfo
            {
                CountryIsoCode = locality.Country.Code,
                CountryName    = locality.Country.Names.GetValueOrDefault(languageCode),
                CountryHtId    = HtId.Create(MapperLocationTypes.Country, locality.CountryId),
                LocalityName   = locality.Names.GetValueOrDefault(languageCode),
                LocalityHtId   = localityHtId
            };

            return(localityInfo);
        }
        public async Task <List <Locality> > GetLocalities(DateTimeOffset modified, int skip, int top, string languageCode, CancellationToken cancellationToken = default)
        {
            var localitiesFromDb = await _dbContext.Localities.Include(l => l.Country)
                                   .OrderBy(l => l.Id)
                                   .Where(l => l.IsActive && l.Country.IsActive && l.Modified >= modified)
                                   .Skip(skip)
                                   .Take(top)
                                   .ToListAsync(cancellationToken);

            var tasks = localitiesFromDb.Select(async locality
                                                =>
            {
                var country      = locality.Country;
                var localityName = locality.Names.GetValueOrDefault(languageCode);
                var countryName  = country.Names.GetValueOrDefault(languageCode);
                var htId         = HtId.Create(MapperLocationTypes.Locality,
                                               locality.Id);

                var numberOfAccommodations = await _accommodationsCounter.GetNumberOfAccommodationsInLocality(locality.Id, cancellationToken);

                return(new Locality(
                           htId: htId,
                           country: countryName,
                           name: localityName,
                           numberOfAccommodations: numberOfAccommodations));
            })
                        .ToList();

            var locations = await Task.WhenAll(tasks);

            return(locations.ToList());
        }
Example #4
0
        private List <LocationMapping> Build(List <ProjectionForCountryHtIdMapping> accommodationsInfo, string languageCode)
        {
            if (!accommodationsInfo.Any())
            {
                return(new List <LocationMapping>());
            }

            return(accommodationsInfo
                   .GroupBy(a => a.CountryId)
                   .Where(g => g.Any())
                   .Select(g =>
            {
                return new LocationMapping
                (
                    location: new Location
                    (
                        country: g
                        .Select(a => a.CountryNames.GetValueOrDefault(languageCode))
                        .FirstOrDefault() ?? string.Empty,
                        countryCode: g
                        .Select(a => a.CountryCode)
                        .FirstOrDefault() ?? string.Empty,
                        countryHtId: g
                        .Select(a => HtId.Create(MapperLocationTypes.Country, a.CountryId))
                        .First(),
                        locality: string.Empty,
                        localityHtId: string.Empty,
                        name: string.Empty,
                        coordinates: default,
        public async Task <List <Country> > GetCountries(DateTimeOffset modified, int skip, int top, string languageCode, CancellationToken cancellationToken = default)
        {
            var countriesFromDb = await _dbContext.Countries.Where(c => c.IsActive && c.Modified >= modified)
                                  .OrderBy(c => c.Id)
                                  .Skip(skip)
                                  .Take(top)
                                  .ToListAsync(cancellationToken);

            var tasks = countriesFromDb.Select(async c =>
            {
                var name = c.Names.GetValueOrDefault(languageCode);
                var htId = HtId.Create(MapperLocationTypes.Country,
                                       c.Id);

                var numberOfAccommodations = await _accommodationsCounter.GetNumberOfAccommodationsInCountry(c.Id, cancellationToken);

                return(new Country(
                           htId: htId,
                           name: name,
                           code: c.Code,
                           numberOfAccommodations: numberOfAccommodations));
            });

            var countries = await Task.WhenAll(tasks);

            return(countries.ToList());
        }
Example #6
0
        public static SlimAccommodation ConvertToSlim(int htId, int htCountryId, int?htLocalityId, int?htLocalityZoneId,
                                                      MultilingualAccommodation accommodation, string language)
        {
            var name             = accommodation.Name.GetValueOrDefault(language);
            var address          = accommodation.Location.Address.GetValueOrDefault(language);
            var localityName     = accommodation.Location.Locality?.GetValueOrDefault(language);
            var countryName      = accommodation.Location.Country.GetValueOrDefault(language);
            var localityZoneName = accommodation.Location.LocalityZone?.GetValueOrDefault(language);

            return(new SlimAccommodation(
                       htId: HtId.Create(MapperLocationTypes.Accommodation, htId),
                       location: new SlimLocationInfo(
                           address: address,
                           country: countryName,
                           countryCode: accommodation.Location.CountryCode,
                           locality: localityName,
                           localityZone: localityZoneName,
                           coordinates: new GeoPoint(accommodation.Location.Coordinates.Longitude, accommodation.Location.Coordinates.Latitude)
                           ),
                       name: name,
                       photo: GetPhotos(accommodation.Photos).FirstOrDefault(),
                       rating: GetRating(accommodation.Rating),
                       propertyType: GetPropertyType(accommodation.Type)
                       ));
        }
Example #7
0
        public static Accommodation Convert(int htId, int htCountryId, int?htLocalityId, int?htLocalityZoneId,
                                            MultilingualAccommodation accommodation, string language, Dictionary <string, string> supplierAccommodationCodes, DateTimeOffset modified)
        {
            var name = accommodation.Name.GetValueOrDefault(language);
            var accommodationAmenities = accommodation.AccommodationAmenities.GetValueOrDefault(language);
            var additionalInfo         = accommodation.AdditionalInfo.GetValueOrDefault(language);
            var category            = accommodation.Category.GetValueOrDefault(language);
            var address             = accommodation.Location.Address.GetValueOrDefault(language);
            var localityName        = accommodation.Location.Locality?.GetValueOrDefault(language);
            var countryName         = accommodation.Location.Country.GetValueOrDefault(language);
            var localityZoneName    = accommodation.Location.LocalityZone?.GetValueOrDefault(language);
            var textualDescriptions = new List <TextualDescription>();
            var countryHtId         = HtId.Create(MapperLocationTypes.Country, htCountryId);
            var localityHtId        = htLocalityId is not null
                ? HtId.Create(MapperLocationTypes.Locality, htLocalityId.Value)
                : string.Empty;

            var localityZoneHtId = htLocalityZoneId is not null
                ? HtId.Create(MapperLocationTypes.LocalityZone, htLocalityZoneId.Value)
                : string.Empty;

            var suppliersInfo = supplierAccommodationCodes.Select(p => new SupplierInfo(p.Key, p.Value)).ToList();

            foreach (var descriptions in accommodation.TextualDescriptions)
            {
                var description = descriptions.Description.GetValueOrDefault(language);
                textualDescriptions.Add(new TextualDescription(GetTextualDescriptionType(descriptions.Type), description));
            }

            return(new Accommodation(
                       htId: HtId.Create(MapperLocationTypes.Accommodation, htId),
                       name: name,
                       accommodationAmenities: accommodationAmenities,
                       additionalInfo: additionalInfo,
                       category: category,
                       contacts: GetContacts(accommodation.Contacts),
                       location: new LocationInfo(
                           countryCode: accommodation.Location.CountryCode,
                           countryHtId: countryHtId,
                           country: countryName,
                           localityHtId: localityHtId,
                           locality: localityName,
                           localityZoneHtId: localityZoneHtId,
                           localityZone: localityZoneName,
                           coordinates: new GeoPoint(accommodation.Location.Coordinates.Longitude, accommodation.Location.Coordinates.Latitude),
                           address: address,
                           locationDescriptionCode: GetLocationDescriptionCode(accommodation.Location.LocationDescriptionCode),
                           pointsOfInterests: GetPoiInfos(accommodation.Location.PointsOfInterests),
                           isHistoricalBuilding: accommodation.Location.IsHistoricalBuilding
                           ),
                       photos: GetPhotos(accommodation.Photos),
                       rating: GetRating(accommodation.Rating),
                       schedule: GetScheduleInfo(accommodation.Schedule),
                       textualDescriptions: textualDescriptions,
                       type: GetPropertyType(accommodation.Type),
                       suppliers: suppliersInfo,
                       modified: modified.DateTime
                       ));
        }
Example #8
0
        public async Task <List <string> > FilterByRatings(List <string> accommodationHtIds, List <AccommodationRatings> ratings)
        {
            // TODO: replace GetRichDetails to optimized query.
            // Used GetRichDetails because this method looking for actual accommodation
            var accommodations = await GetRichDetails(accommodationHtIds);

            return(accommodations
                   .Where(a => ratings.Contains(a.FinalAccommodation.Rating))
                   .Select(a => HtId.Create(MapperLocationTypes.Accommodation, a.Id))
                   .ToList());
        }
Example #9
0
        public async ValueTask PublishRemovedCountries(List <int> countryIds)
        {
            if (countryIds.Count == 0)
            {
                return;
            }

            var countries = countryIds.Select(id => new Country(
                                                  htId: HtId.Create(MapperLocationTypes.Country, id),
                                                  name: string.Empty,
                                                  code: string.Empty,
                                                  numberOfAccommodations: default)).ToList();
Example #10
0
        public async ValueTask PublishRemovedLocalities(List <int> localityIds)
        {
            if (!localityIds.Any())
            {
                return;
            }

            var localities = localityIds.Select(id => new Locality(
                                                    htId: HtId.Create(MapperLocationTypes.Locality, id),
                                                    name: string.Empty,
                                                    country: string.Empty,
                                                    numberOfAccommodations: default
        public async Task <List <Accommodation> > GetAccommodations(DateTimeOffset modified, int skip, int top, CancellationToken cancellationToken = default)
        {
            var accommodationsFromDb = await _dbContext.Accommodations
                                       .GroupJoin(_dbContext.Localities, a => a.LocalityId, l => l.Id, (accommodation, locality) => new { accommodation, locality })
                                       .SelectMany(al => al.locality.DefaultIfEmpty(), (al, locality) => new { al.accommodation, locality })
                                       .Join(_dbContext.Countries, al => al.accommodation.CountryId, c => c.Id,
                                             (al, country) => new { al.accommodation, al.locality, country })
                                       .Where(alc =>
                                              alc.accommodation.IsActive && alc.country.IsActive && (alc.locality != null && alc.locality.IsActive || alc.locality == null) &&
                                              alc.accommodation.Modified >= modified)
                                       .OrderBy(alc => alc.accommodation.Id)
                                       .Skip(skip)
                                       .Take(top)
                                       .Select(alc
                                               => new
            {
                alc.accommodation.Id,
                alc.accommodation.LocalityId,
                alc.accommodation.KeyData,
                alc.accommodation.CountryCode,
                alc.accommodation.HasDirectContract
            })
                                       .ToListAsync(cancellationToken);

            var tasks = accommodationsFromDb.Select(async alc
                                                    =>
            {
                var htId        = HtId.Create(MapperLocationTypes.Accommodation, alc.Id);
                var name        = alc.KeyData.DefaultName;
                var locality    = alc.KeyData.DefaultLocalityName;
                var country     = alc.KeyData.DefaultCountryName;
                var coordinates = alc.KeyData.Coordinates;

                // Number of accommodations of accommodation locality
                var numberOfAccommodations = alc.LocalityId is not null
                    ? await _accommodationsCounter.GetNumberOfAccommodationsInLocality(alc.LocalityId.Value, cancellationToken)
                    : 1;

                return(new Accommodation(
                           htId: htId,
                           name: name,
                           locality: locality,
                           country: country,
                           coordinates: coordinates,
                           numberOfAccommodationsInLocality: numberOfAccommodations,
                           hasDirectContract: alc.HasDirectContract));
            });

            var accommodations = await Task.WhenAll(tasks);

            return(accommodations.ToList());
        }
Example #12
0
        public async Task <Result <List <string> > > GetDeactivatedLocalities(IEnumerable <string> htIds)
        {
            var(_, isFailure, parsedIds, error) = HtId.Parse(MapperLocationTypes.Locality, htIds);
            if (isFailure)
            {
                return(Result.Failure <List <string> >(error));
            }

            return(await _context.Localities
                   .Where(c => !c.IsActive)
                   .Where(c => parsedIds.Contains(c.Id))
                   .Select(c => HtId.Create(MapperLocationTypes.Locality, c.Id))
                   .ToListAsync());
        }
        private Result <int> GetId(string htId, MapperLocationTypes validType)
        {
            var(_, isFailure, (idType, id), error) = HtId.Parse(htId);
            if (isFailure)
            {
                return(Result.Failure <int>(error));
            }

            if (idType != validType)
            {
                return(Result.Failure <int>($"HtId has the wrong type '{idType}'. The valid type is '{validType}'"));
            }

            return(id);
        }
Example #14
0
        private Result <int> GetAccommodationId(string htId)
        {
            var(_, isFailure, (type, id), error) = HtId.Parse(htId);
            if (isFailure)
            {
                return(Result.Failure <int>(error));
            }

            if (type != MapperLocationTypes.Accommodation)
            {
                return(Result.Failure <int>($"{type} is not supported"));
            }

            return(id);
        }
        public static Result <int> RetrieveId(string htAccommodationId)
        {
            var(_, isFailure, (type, id), error) = HtId.Parse(htAccommodationId);
            if (isFailure)
            {
                return(Result.Failure <int>(error));
            }

            if (type != MapperLocationTypes.Accommodation)
            {
                return(Result.Failure <int>(
                           $"htAccommodationId '{htAccommodationId}' has an invalid type '{type.ToString()}'. The type must be equal '{MapperLocationTypes.Accommodation.ToString()}'"));
            }

            return(id);
        }
        public async Task <Result <List <LocalityAndSynonymMatch> > > GetLocalitiesAndSynonymsMatches(List <string> htIds)
        {
            var(_, isFailure, parsedIds, error) = HtId.Parse(MapperLocationTypes.Locality, htIds);
            if (isFailure)
            {
                return(Result.Failure <List <LocalityAndSynonymMatch> >(error));
            }

            return(await _context.LocalitySynonyms
                   .Where(l => l.ExLocalityId != null)
                   .Where(l => parsedIds.Contains(l.ExLocalityId !.Value))
                   .Select(l => new LocalityAndSynonymMatch(
                               HtId.Create(MapperLocationTypes.Locality, l.ExLocalityId !.Value),
                               HtId.Create(MapperLocationTypes.Locality, l.LocalityId)
                               ))
                   .ToListAsync());
        }
Example #17
0
        public async ValueTask PublishUpdatedLocalities(List <LocalityData> localitiesData)
        {
            if (!localitiesData.Any())
            {
                return;
            }

            var tasks = localitiesData.Select(async l => new Locality(htId: HtId.Create(MapperLocationTypes.Locality, l.Id),
                                                                      name: l.Name,
                                                                      country: l.CountryName,
                                                                      numberOfAccommodations: await _accommodationsCounter.GetNumberOfAccommodationsInLocality(l.Id)));

            var localities = await Task.WhenAll(tasks);

            var publicationData = localities.Select(l => BuildPublicationData(l, PublicationEventTypes.Update)).ToList();

            await _staticDataPublicationService.Publish(publicationData);

            _logger.LogUpdatedLocalitiesPublish(localities.Length, string.Join(", ", localitiesData.Select(l => l.Id)));
        }
Example #18
0
        public async ValueTask PublishAddedCountries(List <CountryData> countriesData)
        {
            if (countriesData.Count == 0)
            {
                return;
            }

            var tasks = countriesData.Select(async c => new Country(
                                                 htId: HtId.Create(MapperLocationTypes.Country, c.Id),
                                                 name: c.Name,
                                                 code: c.Code,
                                                 numberOfAccommodations: await _accommodationsCounter.GetNumberOfAccommodationsInCountry(c.Id)
                                                 ));
            var countries = await Task.WhenAll(tasks);

            var publicationData = countries.Select(c => BuildPublicationData(c, PublicationEventTypes.Add)).ToList();

            await _staticDataPublicationService.Publish(publicationData);

            _logger.LogAddedCountriesPublish(countries.Length);
        }
        private async Task <Result <Dictionary <string, Locality> > > GetActiveLocalities(List <string> htIds, CancellationToken cancellationToken = default)
        {
            var gettingLocalityResult = htIds.Select(htId => GetId(htId, MapperLocationTypes.Locality)).ToList();
            var convertingFailures    = gettingLocalityResult.Where(r => r.IsFailure).ToList();

            if (convertingFailures.Any())
            {
                return(Result.Failure <Dictionary <string, Locality> >(string.Join(Environment.NewLine, convertingFailures.Select(f => f.Error))));
            }

            var localities = await _dbContext.Localities.Include(l => l.Country)
                             .Where(l => gettingLocalityResult.Select(r => r.Value).Contains(l.Id) && l.IsActive)
                             .ToListAsync(cancellationToken);

            if (localities.Count != htIds.Count)
            {
                return(Result.Failure <Dictionary <string, Locality> >($"Failed to retrieve localities by the presented htIds {string.Join(", ", htIds)}"));
            }

            return(localities.ToDictionary(l => HtId.Create(MapperLocationTypes.Locality, l.Id), l => l));
        }
Example #20
0
        public static SlimAccommodationData Convert(int id, MultilingualAccommodation accommodation, bool isActive, string language)
        {
            var name             = accommodation.Name.GetValueOrDefault(language);
            var address          = accommodation.Location.Address.GetValueOrDefault(language);
            var localityName     = accommodation.Location.Locality?.GetValueOrDefault(language);
            var countryName      = accommodation.Location.Country.GetValueOrDefault(language);
            var localityZoneName = accommodation.Location.LocalityZone?.GetValueOrDefault(language);
            var rating           = AccommodationConverter.GetRating(accommodation.Rating);

            return(new SlimAccommodationData
            {
                HtId = HtId.Create(MapperLocationTypes.Accommodation, id),
                Name = name,
                Address = address,
                CountryName = countryName,
                LocalityName = localityName,
                LocalityZoneName = localityZoneName,
                Rating = rating,
                IsActive = isActive
            });
        }
        public async ValueTask PublishAddedLocalityZones(List <LocalityZoneData> localityZonesData)
        {
            if (!localityZonesData.Any())
            {
                return;
            }

            var tasks = localityZonesData.Select(async lz
                                                 => new LocalityZone(
                                                     htId: HtId.Create(MapperLocationTypes.LocalityZone, lz.Id),
                                                     name: lz.Name,
                                                     locality: lz.LocalityName,
                                                     country: lz.CountryName,
                                                     numberOfAccommodations: await _accommodationsCounter.GetNumberOfAccommodationsInLocalityZone(lz.Id)));

            var localityZones = await Task.WhenAll(tasks);

            var publicationData = localityZones.Select(lz => BuildPublicationData(lz, PublicationEventTypes.Add)).ToList();

            await _staticDataPublicationService.Publish(publicationData);

            _logger.LogAddedLocalityZonesPublish(localityZones.Length);
        }
Example #22
0
        public async Task <Result <Dictionary <string, string> > > GetAccommodationSynonymsHtIds(List <string> htIds)
        {
            var(_, isFailure, parsedIds, error) = HtId.Parse(MapperLocationTypes.Accommodation, htIds);
            if (isFailure)
            {
                return(Result.Failure <Dictionary <string, string> >(error));
            }

            var changedHtIds = new Dictionary <string, string>();

            var activeAccommodationIds = await _context.Accommodations
                                         .Where(a => parsedIds.Contains(a.Id) && a.IsActive)
                                         .Select(a => a.Id)
                                         .ToListAsync();

            var notFoundIds = parsedIds.Except(activeAccommodationIds);

            foreach (var id in notFoundIds)
            {
                var changedId = await _context.HtAccommodationMappings
                                .Where(h => h.IsActive)
                                .Where(h => EF.Functions.JsonContains(h.MappedHtIds, @$ "[{id}]"))
                                .Select(h => h.HtId)
                                .SingleOrDefaultAsync();

                if (changedId == default)
                {
                    return(Result.Failure <Dictionary <string, string> >($"AccommodationId {id} is not active and not mapped to a new id"));
                }

                var htId = HtId.Create(MapperLocationTypes.Accommodation, id);
                changedHtIds[htId] = HtId.Create(MapperLocationTypes.Accommodation, changedId);
            }

            return(changedHtIds);
        }
 public Task <Result <List <LocalitySynonymInfo> > > GetLocalitySynonyms(string localityHtId, CancellationToken cancellationToken)
 => HtId.Parse(localityHtId)
 .Map(l => _dbContext.LocalitySynonyms.Include(ls => ls.Locality)
      .Where(ls => ls.LocalityId == l.Id && ls.Locality.IsActive)
      .Select(ls => new LocalitySynonymInfo(ls.Id, ls.Names, ls.LocalityId, ls.ExLocalityId, ls.SynonymType))
      .ToListAsync(cancellationToken));