Example #1
0
        public async Task <Page <ShopModel> > GetShopsByDistance(UserModel user, PageModel page)
        {
            try
            {
                if (user is null)
                {
                    throw new ArgumentNullException(nameof(user));
                }
                if (page is null)
                {
                    throw new ArgumentNullException(nameof(page));
                }

                var idFilter = Builders <UserModel> .Filter.Eq(nameof(user.Id), user.Id);

                UserModel userModel = await _shopDatabase.UsersCollection.Find(idFilter)
                                      .SingleOrDefaultAsync()
                                      .ConfigureAwait(false);

                IEnumerable <ShopModel> shops =
                    await _shopDatabase.ShopsCollection.Find(Builders <ShopModel> .Filter.Empty)
                    .ToListAsync().ConfigureAwait(false);

                IEnumerable <ShopModel> shopsByDistance = shops.OrderBy(shop => shop.Coordinates.DistanceTo(userModel.Location));
                _logger.LogInformation($"All shops had gotten ordered by distance by the user {user}");

                return(shopsByDistance.GetPage(page));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error while getting shops ordered by distance by the user {user}");

                return(PaginationExtension.GetEmptyPage <ShopModel>());
            }
        }
Example #2
0
        public async Task <Page <ShopApi> > GetShops(UserApi user, int page, int pageSize)
        {
            try
            {
                if (user is null)
                {
                    throw new ArgumentNullException(nameof(user));
                }

                var userModel             = _mapper.Map <UserModel>(user);
                var pagingModel           = new PageModel(page, pageSize);
                Page <ShopModel> shopList = await _shopRepository.GetShops(userModel, pagingModel);

                var shops = _mapper.Map <Page <ShopApi> >(shopList);
                _logger.LogInformation($"The shops had been gotten by the user {user}");

                return(shops);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error getting shops for the user {user}");

                return(PaginationExtension.GetEmptyPage <ShopApi>());
            }
        }
Example #3
0
        public async Task <Page <ShopModel> > GetShops(UserModel user, PageModel page)
        {
            try
            {
                if (user is null)
                {
                    throw new ArgumentNullException(nameof(user));
                }
                if (page is null)
                {
                    throw new ArgumentNullException(nameof(page));
                }
                ShopModel shopModel;
                var       idFilter = Builders <UserModel> .Filter.Eq(nameof(user.Id), user.Id);

                UserModel userModel = await _shopDatabase.UsersCollection.Find(idFilter).SingleOrDefaultAsync().ConfigureAwait(false);

                var excludedShops = new List <ObjectId>();
                if (userModel.LikedShops != null)
                {
                    excludedShops.AddRange(userModel.LikedShops);
                }
                if (userModel.DislikedShops != null)
                {
                    excludedShops.AddRange(userModel.DislikedShops);
                }
                var likedFilter = Builders <ShopModel> .Filter.Nin(nameof(shopModel.Id), excludedShops);

                Page <ShopModel> shops = await _shopDatabase.ShopsCollection.GetPagedAsync(page, likedFilter).ConfigureAwait(false);

                _logger.LogInformation($"All shops had gotten");

                return(shops);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error while getting shops");

                return(PaginationExtension.GetEmptyPage <ShopModel>());
            }
        }
Example #4
0
        public async Task <Page <ShopModel> > GetPreferedShops(UserModel user, PageModel page)
        {
            try
            {
                if (user is null)
                {
                    throw new ArgumentNullException(nameof(user));
                }
                if (page is null)
                {
                    throw new ArgumentNullException(nameof(page));
                }
                ShopModel shopModel;
                var       idFilter = Builders <UserModel> .Filter.Eq(nameof(user.Id), user.Id);

                UserModel userModel = await _shopDatabase.UsersCollection.Find(idFilter).SingleOrDefaultAsync().ConfigureAwait(false);

                Page <ShopModel> shops = null;
                if (userModel.LikedShops == null)
                {
                    shops = await _shopDatabase.ShopsCollection.GetPagedAsync(page).ConfigureAwait(false);
                }
                var likedFilter = Builders <ShopModel> .Filter.In(nameof(shopModel.Id), userModel.LikedShops);

                Page <ShopModel> preferedShops = await _shopDatabase.ShopsCollection.GetPagedAsync(page, likedFilter).ConfigureAwait(false);

                _logger.LogInformation($"All preffered shops had gotten by the user {user}");

                return(shops ?? preferedShops);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error while getting prefered shops by the user {user}");

                return(PaginationExtension.GetEmptyPage <ShopModel>());
            }
        }