public static GetProductsStoreEntity ToEntity(this GetProductsServiceRequest request) { if (request.PagingInfo == null) { return(null); } GetProductsStoreEntity page = new GetProductsStoreEntity() { PagingInfo = request.PagingInfo, ProductSort = request.ProductSort }; return(page); }
public async Task Get_Products_Should_Return_List_Of_Products() { MongoProductStore productStore = new MongoProductStore(); GetProductsStoreEntity getProductsStoreEntity = new GetProductsStoreEntity() { PagingInfo = new PagingInfo() { PageNumber = 1, PageSize = 20, TotalPages = 100 } }; var response = await productStore.GetProductsAsync(getProductsStoreEntity); Assert.Equal(1, response.Products.Count); }
public async Task <GetProductsStoreResponse> GetProductsAsync(GetProductsStoreEntity request) { List <MongoEntity> mongoEntities = new List <MongoEntity>(); var pageSize = request.PagingInfo.PageSize; var pageNumber = request.PagingInfo.PageNumber; var collection = _db.GetCollection <MongoEntity>(_collection); var sortType = request.ProductSort.Type.ToEntity(); var skipDocuments = (pageNumber - 1) * pageSize; var orderBy = request.ProductSort.Order; var sortDefinition = (orderBy == "Asc") ? (Builders <MongoEntity> .Sort.Ascending(sortType)) : (Builders <MongoEntity> .Sort.Descending(sortType)); try { var docCount = (int)await collection.CountAsync(new BsonDocument()); request.PagingInfo.TotalPages = (docCount >= pageSize) ? ((docCount / pageSize) + ((docCount % pageSize) == 0 ? 0 : 1)) : 1; var skipDocumentEnabled = (pageNumber - 1 * pageSize) > docCount ? false : true; if (skipDocumentEnabled) { mongoEntities = await collection.Find(FilterDefinition <MongoEntity> .Empty) .Skip(skipDocuments) .Limit(pageSize) .Sort(sortDefinition) .ToListAsync(); } } catch { throw new BaseException(int.Parse(ErrorCode.DataBaseDown()), Error.DataBaseDown(), null, HttpStatusCode.GatewayTimeout); } return(mongoEntities.ToModel(request.PagingInfo)); }
public async Task <GetProductsStoreResponse> GetProductsAsync(GetProductsStoreEntity request) { productList.Apply(request.Filters); var products = productList.Apply(request.ProductSort); int startIndex = (request.PagingInfo.PageNumber - 1) * (request.PagingInfo.PageSize); int endIndex = startIndex + request.PagingInfo.PageSize - 1; List <ProductEntity> responseProducts = new List <ProductEntity>(); for (int currentIndex = startIndex; currentIndex <= endIndex; currentIndex++) { if (currentIndex == products.Count() || currentIndex > products.Count()) { break; } responseProducts.Add(products[currentIndex].ToEntity()); } request.PagingInfo.TotalPages = (productList.Count() >= request.PagingInfo.PageSize) ? ((productList.Count() / request.PagingInfo.PageSize) + (productList.Count() % request.PagingInfo.PageSize)) : 1; var response = responseProducts.ToGetProductsStoreResponse(request.PagingInfo); return(response); }