public async Task <Result> AddProductAsync(Guid id, string userId)
        {
            var hasProduct = await Data.Products
                             .AnyAsync(x => x.Id == id)
                             .ConfigureAwait(false);

            if (!hasProduct)
            {
                return(NotFound);
            }

            var hasWishlist = await AllAsNoTracking
                              .CountAsync(x => x.ProductId == id);

            if (hasWishlist > 0)
            {
                return(HasAlready);
            }

            var wishlist = new Wishlist
            {
                UserId    = userId,
                ProductId = id
            };

            await Data.AddAsync(wishlist).ConfigureAwait(false);

            await Data.SaveChangesAsync().ConfigureAwait(false);

            return(Result.Success);
        }
 public async Task <IEnumerable <WishlistResponseModel> > AllByUserId(string id)
 {
     return(await Mapper.ProjectTo <WishlistResponseModel>(AllAsNoTracking
                                                           .Where(w => w.UserId == id))
            .ToListAsync()
            .ConfigureAwait(false));
 }
 public async Task <IEnumerable <AddressResponseModel> > ByUserAsync(string userId)
 {
     return(await Mapper.ProjectTo <AddressResponseModel>(AllAsNoTracking
                                                          .Where(a => a.UserId == userId))
            .ToListAsync()
            .ConfigureAwait(false));
 }
 public async Task <int> GetProductStockQuantityByIdAsync(Guid productId, string userId)
 {
     return(await AllAsNoTracking
            .Where(x => x.ProductAttribute.ProductId == productId && x.UserId == userId)
            .CountAsync()
            .ConfigureAwait(false));
 }
 public async Task <OrdersDetailsResponseModel> DetailsAsync(Guid id, string userId)
 {
     return(await Mapper
            .ProjectTo <OrdersDetailsResponseModel>(AllAsNoTracking
                                                    .Where(o => o.Id == id && o.UserId == userId))
            .FirstOrDefaultAsync()
            .ConfigureAwait(false));
 }
Beispiel #6
0
        public virtual Task <T> FirstOrDefaultAsync(Expression <Func <T, bool> > predicate, bool asNoTracking = false)
        {
            if (asNoTracking)
            {
                return(AllAsNoTracking.FirstOrDefaultAsync(predicate));
            }

            return(Context.Set <T>().FirstOrDefaultAsync(predicate));
        }
        public async Task <Result> AddProductAsync(CartIteamRequestModel model, string userId)
        {
            var isHas = await AllAsNoTracking
                        .AnyAsync(x => x.ProductAttributeId == model.ProductAttributeId &&
                                  x.UserId == userId)
                        .ConfigureAwait(false);

            var productId = model.ProductAttributeId;
            var quantity  = model.Quantity;

            var productAttribute = await GetProductAttribute(productId)
                                   .ConfigureAwait(false);

            if (productAttribute == null)
            {
                return(NotFound);
            }
            if (quantity > productAttribute.StockQuantity)
            {
                return(NotEnoughProductsMessage);
            }

            if (!isHas)
            {
                var cartItem = new CartItem
                {
                    UserId           = userId,
                    ProductAttribute = productAttribute,
                    Quantity         = quantity
                };

                await Data.AddAsync(cartItem)
                .ConfigureAwait(false);
            }
            else
            {
                var result = await All
                             .FirstOrDefaultAsync(x => x.ProductAttributeId == productId &&
                                                  x.UserId == userId)
                             .ConfigureAwait(false);

                if (result == null)
                {
                    return(InvalidErrorMessage);
                }

                result.Quantity = quantity;
            }

            await Data.SaveChangesAsync()
            .ConfigureAwait(false);

            return(Result.Success);
        }
        private async Task <int> GetTotalPages(ProductsSearchRequestModel model)
        {
            var specification = GetProductSpecification(model);

            var total = await AllAsNoTracking
                        .Where(specification)
                        .CountAsync()
                        .ConfigureAwait(false);

            return((int)Math.Ceiling((double)total / ProductsPerPage));
        }
        public async Task <ProductsDetailsResponseModel> DetailsAsync(Guid id)
        {
            var products = AllAsNoTracking
                           .Where(p => p.Id == id);

            var result = await Mapper
                         .ProjectTo <ProductsDetailsResponseModel>(products)
                         .FirstOrDefaultAsync()
                         .ConfigureAwait(false);

            return(result);
        }
        public async Task <ProductsSearchResponseModel> ProductSearchAsync(ProductsSearchRequestModel model)
        {
            var specification = GetProductSpecification(model);

            var products = await Mapper
                           .ProjectTo <ProductsListingResponseModel>(AllAsNoTracking
                                                                     .Where(specification)
                                                                     .Skip((model.Page - 1) * ProductsPerPage)
                                                                     .Take(ProductsPerPage))
                           .ToListAsync()
                           .ConfigureAwait(false);

            var totalPages = await GetTotalPages(model).ConfigureAwait(false);

            return(new ProductsSearchResponseModel
            {
                Products = products,
                Page = model.Page,
                TotalPages = totalPages
            });
        }
 private IQueryable <CartItem> AllByUserId(string userId)
 {
     return(AllAsNoTracking
            .Where(c => c.UserId == userId));
 }