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.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 a new order")); } return(BadRequest("Failed to save a new order")); }