Esempio n. 1
0
        public ActionResult <Order> PlaceOrder(string flightId, string passengerSeat, List <OrderLineDTO> orderLineDTOs)
        {
            Passenger passenger = _passengerRepository.GetByFlightIdAndSeatNumber(flightId, passengerSeat);

            if (passenger == null)
            {
                return(NotFound("Passenger not found"));
            }
            Order order = new Order()
            {
                Passenger = passenger
            };

            _orderRepository.AddNewOrder(order);
            _orderRepository.SaveChanges();

            order = _orderRepository.GetLastOrder();

            foreach (OrderLineDTO orderLineDTO in orderLineDTOs)
            {
                var food = _foodRepository.GetById(orderLineDTO.Food.FoodId);

                if (food == null)
                {
                    return(NotFound("Food not found"));
                }

                OrderLine newOrderLine = new OrderLine
                {
                    FoodId   = food.FoodId,
                    Quantity = orderLineDTO.Quantity
                };

                order.AddOrderLine(newOrderLine);
            }

            passenger.Orders.Add(order);

            _passengerRepository.SaveChanges();

            return(Ok(order));
        }