public async Task <IActionResult> Post([FromBody] OrderViewModel model) { //add it to the db try { if (ModelState.IsValid) { var newOrder = new Order() { OrderDate = model.OrderDate, OrderNumber = model.OrderNumber, Id = model.OrderId }; var currentUser = await _userManager.FindByNameAsync(User.Identity.Name); newOrder.User = null; _repository.AddEntity(newOrder); if (_repository.SaveAll()) { return(Created($"/api/order/{newOrder.Id}", model)); //201 } } else { return(BadRequest(ModelState)); } } catch (Exception ex) { _logger.LogError($"Failed to save a new order:{ex}"); } return(BadRequest("Failed to save")); }
public async Task <IActionResult> Post([FromBody] OrderViewModel model) { try { if (ModelState.IsValid) { var order = _mapper.Map <OrderViewModel, Order>(model); if (order.OrderDate == DateTime.MinValue) { order.OrderDate = DateTime.Now; } order.User = await _userManager.FindByNameAsync(User.Identity.Name); _repository.AddEntity(order); if (_repository.SaveAll()) { var vm = _mapper.Map <Order, OrderViewModel>(order); return(Created($"/api/orders/{vm.OrderId}", vm)); } } return(BadRequest(ModelState)); } catch (Exception e) { _logger.LogError($"Failed to post order: {e}"); return(BadRequest("Failed")); } }