Esempio n. 1
0
        public async Task <int> PlaceOrder(ShoppingCart carts, OrderVM order, string userEmail)
        {
            var orderToAdd = _mapper.Map <OrderVM, Order>(order);

            orderToAdd.PaymentStatus = false;
            orderToAdd.CreatedDate   = DateTime.Now;
            orderToAdd.CreatedBy     = "admin";
            orderToAdd.Status        = OrderStatus.Waiting;

            if (!string.IsNullOrEmpty(userEmail))
            {
                var user = _applicationUserRepository.GetUserByUserName(userEmail);
                orderToAdd.User = user;
            }

            if (!string.IsNullOrEmpty(carts.CartPromoCode))
            {
                var discount = await _discountRepository.GetDiscountByPromoCode(carts.CartPromoCode);

                orderToAdd.Discount = discount;
            }
            else
            {
                var discount = await _discountRepository.GetDiscountById(1);

                orderToAdd.Discount = discount;
            }

            await _orderRepository.AddAsync(orderToAdd);

            foreach (var item in carts.Cart)
            {
                var product = await _productRepository.GetSingleByIDAsync(item.Product.ProductId);

                if (product.AvailableQuantity >= item.Quantity)
                {
                    var orderDetail = new OrderDetail()
                    {
                        OrderId = orderToAdd.OrderId, ProductId = item.Product.ProductId, Quantity = item.Quantity
                    };
                    await _orderDetailRepository.AddAsync(orderDetail);

                    //Update product sold
                    var rank = await _productRankRepository.GetSingleByIDAsync(item.Product.ProductId);

                    rank.Sold += item.Quantity;
                    await _productRankRepository.UpdateAsync(rank);

                    product.AvailableQuantity -= item.Quantity;
                    await _productRepository.UpdateAsync(product);
                }
                else
                {
                    return(0);
                }
            }

            return(await _unitOfWork.SaveAsync());
        }
Esempio n. 2
0
        public async Task <int> SaveDataAsync(ProductVM viewModel)
        {
            var entity = _mapper.Map <ProductVM, Product>(viewModel);

            if (viewModel.PublicationDate == DateTime.MinValue)
            {
                entity.PublicationDate = DateTime.Now;
            }
            //Update product rating
            var rank = new ProductRank
            {
                ProductId  = entity.ProductId,
                Name       = entity.Name,
                CategoryId = entity.CategoryId,
                Rate       = 0,
                Sold       = 0
            };

            if (viewModel.ProductId == 0)
            {
                entity.CreateBy        = "admin"; entity.UpdateBy = "admin";
                entity.CreateDate      = DateTime.Now;
                entity.UpdatedDate     = DateTime.Now;
                entity.UniqueStringKey = Guid.NewGuid();
                entity.Authors         = new List <Author>()
                {
                    await _authorDetailRepository.GetAuthorById(1)
                };

                var zeroDiscount = await _discountRepository.GetSingleByIDAsync(1);

                entity.Discount = zeroDiscount;

                await _productRepository.AddAsync(entity);

                await _productRankRepository.AddAsync(rank);
            }
            else
            {
                entity.UpdatedDate = DateTime.Now;
                await _productRepository.UpdateAsync(entity);

                await _productRankRepository.UpdateAsync(rank);
            }

            return(await _unitOfWork.SaveAsync());
        }
Esempio n. 3
0
        public async Task <int> AddCommentToDB(CommentVM comment)
        {
            var user = _applicationUserRepository.GetUserByUserName(comment.UserId);

            comment.UserId = user.Id;
            var cmt = _mapper.Map <Comment>(comment);
            await _commentRepository.AddAsync(cmt);

            if (comment.Rating > 0)
            {
                var listCmt = await _commentRepository.GetListCommentByProductId(comment.ProductId);

                //update product rating
                var rank = await _productRankRepository.GetSingleByIDAsync(comment.ProductId);

                rank.Rate = listCmt.Average(m => m.Rating);
                await _productRankRepository.UpdateAsync(rank);
            }
            return(await _unitOfWork.SaveAsync());
        }