Exemplo n.º 1
0
        public ActionResult PlaceOrder(int id, PlaceOrderDTO placeOrder)
        {
            try
            {
                ApplicationUser user = _userRepository.GetUserBy(User.Identity.Name);
                if (user is null || !user.Type.Equals(UserType.PASSENGER))
                {
                    return(Unauthorized());
                }

                Order order = new Order();
                placeOrder.OrderLines.ToList().ForEach(ol =>
                {
                    Product prod   = _shopRepository.GetProductById(ol.ProductId);
                    OrderLine line = new OrderLine(prod, ol.Amount);
                    order.OrderLines.Add(line);
                });
                order.Customer = (Passenger)user;
                _shopRepository.PlaceOrder(id, order);
                _shopRepository.SaveChanges();

                return(Ok(OrderDTO.FromOrder(order)));
            } catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <OrderDTO> > GetOrderAsync(int orderId)
        {
            try
            {
                var order = await _orderingContext.Orders
                            .Include(o => o.OrderItems)
                            .SingleAsync(o => o.Id == orderId);

                return(Ok(OrderDTO.FromOrder(order)));
            }
            catch
            {
                return(NotFound());
            }
        }