Ejemplo n.º 1
0
        public async Task <OrderModel> CreateOrderAsync(List <CreateOrderItemModel> model)
        {
            if (!model.Any())
            {
                throw new ServerException(Constants.Errors.EMPTY_ORDER);
            }

            var user = await _userService.GetUserByIdAsync();

            var payment = new Payment
            {
                TransactionId = null
            };

            await _paymentRepository.CreateAsync(payment);

            decimal totalAmount = default;

            model.ForEach(item =>
            {
                totalAmount += item.Amount;
            });

            var order = new DataAccessLayer.Entities.Order
            {
                Description = null,
                Status      = StatusType.Unpaid,
                UserId      = user.Id,
                PaymentId   = payment.Id,
                TotalAmount = totalAmount
            };

            await _orderRepository.CreateAsync(order);

            await _orderItemService.CreateOrderItemAsync(order.Id, model);

            var orderModel = _mapper.Map <OrderModel>(order);

            return(orderModel);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] CreateOrderRequest orderRequest)
        {
            try
            {
                var order = new Order
                {
                    UserId          = orderRequest.UserId,
                    DeliveryMethod  = orderRequest.DeliveryMethod,
                    PaymentMethod   = orderRequest.PaymentMethod,
                    CouponId        = orderRequest.CouponId,
                    UserAddressesId = orderRequest.UserAddressesId,
                    Status          = 1,
                    CreatedAt       = DateTime.Now
                };

                var AddOrder = await _orderService.CreateOrderAsync(order);

                if (AddOrder == 1)
                {
                    var cartItem = await _cartItemService.GetCartItemAsync(orderRequest.CartId);

                    foreach (var item in cartItem)
                    {
                        var orderItem = new OrderItems
                        {
                            OrderId   = order.Id,
                            ProductId = item.ProductId,
                            Quantity  = item.Quantity,
                            Price     = (item.product.SalePrice != 0 ? item.product.SalePrice : item.product.Price),
                            Total     = (item.product.SalePrice != 0 ? item.product.SalePrice : item.product.Price) * item.Quantity,
                            CreatedAt = DateTime.Now
                        };

                        var addOrderItem = await _orderItemService.CreateOrderItemAsync(orderItem);

                        if (addOrderItem == 1)
                        {
                            await _cartItemService.DeleteCartItemAsync(item.Id);
                        }

                        ProductQuantityMinus(item.ProductId, item.Quantity);
                    }

                    if (orderRequest.CouponId != null)
                    {
                        if (orderRequest.CouponId != 0)
                        {
                            PromoCodeQuantityMinusOne(Convert.ToInt32(orderRequest.CouponId));
                        }
                    }

                    var resp = new
                    {
                        status  = Ok().StatusCode,
                        message = "Successfully add order"
                    };

                    return(Ok(resp));
                }

                return(BadRequest(new
                {
                    status = BadRequest().StatusCode,
                    message = "internal server error"
                }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new
                {
                    status = BadRequest().StatusCode,
                    message = ex.Message
                }));
            }
        }