Exemplo n.º 1
0
        private async ValueTask <int> GetNumberOfAccommodations(int id, MapperLocationTypes locationType, CancellationToken cancellationToken = default)
        {
            var cacheKey = BuildKey(id, locationType);

            if (_memoryCache.TryGetValue(cacheKey, out int?numberOfAccommodations))
            {
                return(numberOfAccommodations !.Value);
            }

            using (await Mutex.LockAsync(cancellationToken))
            {
                if (!IsCacheInitialized(locationType))
                {
                    switch (locationType)
                    {
                    case MapperLocationTypes.Locality:
                        await AddNumberOfAccommodationsInLocalitisToCache(locationType, cancellationToken);

                        break;

                    case MapperLocationTypes.Country:
                        await AddNumberOfAccommodationsInCountriesToCache(locationType, cancellationToken);

                        break;

                    case MapperLocationTypes.LocalityZone:
                        await AddNumberOfAccommodationsInLocalityZonesToCache(locationType, cancellationToken);

                        break;
                    }
                }
            }

            return(_memoryCache.TryGetValue(cacheKey, out numberOfAccommodations) ? numberOfAccommodations !.Value : default);
Exemplo n.º 2
0
        public static Result <List <int> > Parse(MapperLocationTypes type, IEnumerable <string> htIds)
        {
            var parsed = htIds.Select(h => Parse(type, h)).ToList();

            if (parsed.Any(r => r.IsFailure))
            {
                return(Result.Failure <List <int> >("Cannot parse some htIds in the list"));
            }

            return(parsed.Where(r => r.IsSuccess)
                   .Select(r => r.Value)
                   .Distinct()
                   .ToList());
        }
        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);
        }
Exemplo n.º 4
0
        public static Result <int> Parse(MapperLocationTypes type, string htId)
        {
            var(_, isFailure, value, error) = Parse(htId);
            if (isFailure)
            {
                return(Result.Failure <int>(error));
            }

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

            return(value.Id);
        }
Exemplo n.º 5
0
 public static string Create(MapperLocationTypes type, int id)
 => $"{type}{Delimiter}{id}";