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> 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());
        }