Esempio n. 1
0
        public IActionResult Post([FromBody] OrderViewModel model)
        {
            //ad it to the db
            try
            {
                if (ModelState.IsValid)
                {
                    var newOrder = _mapper.Map <OrderViewModel, Order>(model);
                    if (newOrder.OrderDate == DateTime.MinValue)
                    {
                        newOrder.OrderDate = DateTime.Now;
                    }

                    _repository.AddEntity(newOrder);

                    if (_repository.SaveAll())
                    {
                        var vm = _mapper.Map <Order, OrderViewModel>(newOrder);
                        return(Created($"api/orders/{vm.OrderId}", vm));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a order: {ex}");
            }
            return(BadRequest("Failed to save new order"));
        }