Ejemplo n.º 1
0
        public PaginationExtension <TDataObject> SearchPaginate(int pageSize, int page)
        {
            var entities = Repository.Get(null, x => x.OrderBy(r => r.CreatedDate), false);
            var _objs    = SFactory.ListMappingEntitiesToObjects(entities);

            int Count = entities.Count();

            _objs = _objs.Skip((page - 1) * pageSize).Take(pageSize);
            int totalPages = (int)Math.Ceiling(Count / (double)pageSize);

            int itemEnd   = pageSize * page;
            int itemStart = (itemEnd - pageSize) + 1;

            if (itemEnd > Count)
            {
                itemEnd = Count;
            }

            var msg = string.Format("Showing {0} to {1} of {2} Records | Total Pages: {3}",
                                    itemStart, itemEnd, Count, totalPages);

            var extension = new PaginationExtension <TDataObject>();

            extension.Summary         = Count > 0 ? msg : string.Empty;
            extension.TotalCount      = Count;
            extension.ReturnedList    = _objs;
            extension.StaticPagedList = new StaticPagedList <TDataObject>(_objs, page, pageSize, Count);
            return(extension);
        }
Ejemplo n.º 2
0
        public PaginationExtension <TDataObject> SearchPaginate(IQueryable <TDataObject> listData, int pageSize, int page)
        {
            int Count = listData.Count();

            listData = listData.Skip((page - 1) * pageSize).Take(pageSize);
            int totalPages = (int)Math.Ceiling(Count / (double)pageSize);

            int itemEnd   = pageSize * page;
            int itemStart = (itemEnd - pageSize) + 1;

            if (itemEnd > Count)
            {
                itemEnd = Count;
            }

            var msg = string.Format("Showing {0} to {1} of {2} Records | Total Pages: {3}",
                                    itemStart, itemEnd, Count, totalPages);

            var extension = new PaginationExtension <TDataObject>();

            extension.Summary         = Count > 0 ? msg : string.Empty;
            extension.TotalCount      = Count;
            extension.ReturnedList    = listData;
            extension.StaticPagedList = new StaticPagedList <TDataObject>(listData, page, pageSize, Count);

            return(extension);
        }
Ejemplo n.º 3
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>());
            }
        }
Ejemplo n.º 4
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>());
            }
        }
Ejemplo n.º 5
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>());
            }
        }
Ejemplo n.º 6
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>());
            }
        }