public async Task <IActionResult> Post([FromBody] OrderViewModel model)
        {
            // add it to the db
            try
            {
                if (ModelState.IsValid)
                {
                    var newOrder = _mapper.Map <OrderViewModel, Order>(model);

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

                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    newOrder.User = currentUser;

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

            return(BadRequest("Failed to save new order"));
        }
Beispiel #2
0
 public void AddOrder(Order order)
 {
     cartRepository.AddOrder(order);
 }