Esempio n. 1
0
        public async Task <ActionResult> UpdateOrderAsync(int orderNumber, OrderDto order)
        {
            if (order == null || !order.IsValid())
            {
                return(BadRequest("Order and order information cannot be null."));
            }

            if (!await parkingLotService.IsParkingLotExistedAsync(order.ParkingLotName))
            {
                return(NotFound("ParkingLot not existed."));
            }

            var orderInMemory = await orderService.GetOrderAsync(orderNumber);

            if (orderInMemory == null || !orderInMemory.Equals(order))
            {
                return(BadRequest("Order not existed."));
            }

            if (!await orderService.IsOrderOpenedAsync(orderNumber))
            {
                return(BadRequest("Order is used."));
            }

            await orderService.CloseOrderAsync(orderNumber);

            return(NoContent());
        }
Esempio n. 2
0
        public IActionResult Create([FromBody] OrderDto orderDto)
        {
            if (!orderDto.IsValid())
            {
                return(BadRequest());
            }

            if (orderService.Create(orderDto))
            {
                return(NoContent());
            }

            return(BadRequest());
        }
Esempio n. 3
0
        public IActionResult Update([FromRoute] int id, [FromBody] OrderDto orderDto)
        {
            if (!orderDto.IsValid())
            {
                return(BadRequest());
            }

            orderDto.Id = id;

            if (orderService.Update(orderDto))
            {
                return(NoContent());
            }

            return(BadRequest());
        }
        public bool Create(OrderDto orderDto)
        {
            if (!orderDto.IsValid())
            {
                throw new ArgumentException("Invalid order.");
            }

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                var user    = unitOfWork.UserRepository.GetById(orderDto.User.Id);
                var product = unitOfWork.ProductRepository.GetById(orderDto.Product.Id);

                if (user == null || product == null)
                {
                    return(false);
                }

                if (product.Quantity < orderDto.Quantity)
                {
                    return(false);
                }

                var order = new Order()
                {
                    Id        = orderDto.Id,
                    User      = user,
                    Product   = product,
                    Quantity  = orderDto.Quantity,
                    Remarks   = orderDto.Remarks,
                    CreatedOn = DateTime.Now
                };

                product.Quantity -= order.Quantity;
                order.TotalPrice  = order.Quantity * product.Price;

                unitOfWork.OrderRepository.Add(order);

                return(unitOfWork.Save());
            }
        }
        public bool Update(OrderDto orderDto)
        {
            if (!orderDto.IsValid())
            {
                throw new ArgumentException("Invalid order.");
            }

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                var order   = unitOfWork.OrderRepository.GetById(orderDto.Id);
                var user    = unitOfWork.UserRepository.GetById(orderDto.User.Id);
                var product = unitOfWork.ProductRepository.GetById(orderDto.Product.Id);

                if (order == null || user == null || product == null)
                {
                    return(false);
                }

                if (product.Quantity < orderDto.Quantity)
                {
                    return(false);
                }

                order.Id         = orderDto.Id;
                order.User       = user;
                order.Product    = product;
                order.Quantity   = orderDto.Quantity;
                order.TotalPrice = orderDto.Quantity * product.Price;
                order.Remarks    = orderDto.Remarks;
                order.UpdatedOn  = DateTime.Now;

                unitOfWork.OrderRepository.Update(order);

                return(unitOfWork.Save());
            }
        }