public async Task <CategoriesStoreResponse> GetCategoriesAsync() { List <StoreCategory> mongoCategoriesEntities; try { var data = _db.GetCollection <StoreCategory>(_collection); mongoCategoriesEntities = await data.Find(new BsonDocument()).ToListAsync(); } catch (Exception ex) { throw new BaseException(int.Parse(ErrorCode.DataBaseDown()), Error.DataBaseDown(), null, System.Net.HttpStatusCode.GatewayTimeout); } return(mongoCategoriesEntities.ToModel()); }
public async Task <GetProductStoreResponse> GetProductAsync(string productId) { MongoEntity mongoEntity; var productStoreCollection = _db.GetCollection <MongoEntity>(_collection); try { mongoEntity = await productStoreCollection.Find(x => x.Id == productId).FirstOrDefaultAsync(); } catch { throw new BaseException(int.Parse(ErrorCode.DataBaseDown()), Error.DataBaseDown(), null, HttpStatusCode.GatewayTimeout); } return(mongoEntity.ToGetResponseModel()); }
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)); }