public IActionResult Post([FromBody] OrderViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newOrder = _mapper.Map <OrderViewModel, Order>(model);

                    if (newOrder.OrderDate == DateTime.MinValue)
                    {
                        newOrder.OrderDate = DateTime.Now;
                    }

                    _repository.AddOrder(newOrder);

                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/orders/{newOrder.Id}", _mapper.Map <Order, OrderViewModel>(newOrder)));
                    }
                    else
                    {
                        return(BadRequest(ModelState));
                    }
                }
            }
            catch
            {
                //
            }
            return(BadRequest("could not create order"));
        }
        public IActionResult Post([FromBody] OrderItemViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newOrderItem = _mapper.Map <OrderItemViewModel, OrderItem>(model);

                    _repository.AddOrderItem(newOrderItem);

                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/orders/orderitems{newOrderItem.Id}", _mapper.Map <OrderItem, OrderItemViewModel>(newOrderItem)));
                    }
                    else
                    {
                        return(BadRequest(ModelState));
                    }
                }
            }
            catch
            {
                //
            }

            return(BadRequest("could not create orderitem"));
        }
Ejemplo n.º 3
0
        public IActionResult Post([FromBody] OrderViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Order newOrder = _orderService.NewOrder(model);

                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/orders/{newOrder.Id}", _mapper.Map <Order, OrderViewModel>(newOrder)));
                    }
                    else
                    {
                        return(BadRequest(ModelState));
                    }
                }
            }
            catch
            {
                //
            }
            return(BadRequest("could not create order"));
        }