public async Task <IActionResult> Post([FromBody] OrderViewModel model) { //add it to the db try { _logger.LogError($"Model:{model}"); if (ModelState.IsValid) { //Dont NEED to do a conversion mannualy because we use IMapper instead //var newOrder = new Order() //{ // OrderDate = model.OrderDate, // OrderNumber = model.OrderNumber, // Id = model.OrderID //}; var newOrder = _mapper.Map <OrderViewModel, Order>(model); //if they didn't specify the date if (newOrder.OrderDate == DateTime.MinValue) { newOrder.OrderDate = DateTime.Now; } //adding to repository // _repository.AddEntity(newOrder); _repository.AddOrder(newOrder); //mapping back from OrderModel to OrderViewModel //Dont NEED to do a conversion mannualy because we use IMapper instead //var vm = new OrderViewModel() //{ // OrderID = newOrder.Id, // OrderDate = newOrder.OrderDate, // OrderNumber = newOrder.OrderNumber //}; var vm = _mapper.Map <Order, OrderViewModel>(newOrder); if (_repository.SaveAll()) { return(Created($"/api/orders/{vm.OrderID}", vm)); //"Created" matching 201 code } } else { return(BadRequest(ModelState)); } } catch (Exception ex) { _logger.LogError($"Failed to save a new order:{ex}"); } return(BadRequest("Failed to save new order")); }
public async Task <IActionResult> Post([FromBody] OrderModel model)//parametru=date noi care le trimitem pe post /*2 schimbam din Order in OrderModel*/ { //adaugam la baza de date try { /*2*/ if (ModelState.IsValid)//daca required si minlenght din ContactModel sunt valide { var newOrder = _mapper.Map <OrderModel, Order>(model); /*3 - in loc de de partea de sus am facut cu mapping: * var newOrder = new Order()//2 trebuie sa convertim model(OrderModel) la Order * { * OrderDate = model.OrderDate, * OrderNumber = model.OrderNumber, * Id = model.OrderId * };*/ //2 validare: vom forta sa puna OrderDate daca userul nu a pus fiindca OrderDate nu a fost required in OrderModel if (newOrder.OrderDate == DateTime.MinValue) //= daca n-au specificat OrderDate { newOrder.OrderDate = DateTime.Now; //se specifica acum } var currentUser = await _userManager.FindByNameAsync(User.Identity.Name); newOrder.User = currentUser; //adaugam comanda noua la db _repository.AddEntity(newOrder); //2 ca avem OrdersModel entitatea devine newOrder(in loc de model) if (_repository.SaveAll()) //daca e salvat,returneaza Created { /* 3 inlocuim cu mapping: * //2 convertim din newOrder inapoi in Model (inversam): * var m = new OrderModel() * { * OrderId = newOrder.Id, * OrderDate = newOrder.OrderDate, * OrderNumber = newOrder.OrderNumber * }; */ //nu ->return Ok(model);//in post daca ai creat un nou obiect obligatoriu returnezi Created (nu Ok) return(Created($"/api/orders/{newOrder.Id}", _mapper.Map <Order, OrderModel>(newOrder)));//Created e 201,nu 200 //3 fara mapping era: return Created($"/api/orders/{m.OrderId}", m);//return Created($"/api/orders/{m.OrderId}",m); /* {model.Id} - sa se potriveasca cu HttpGet de la Get(id) * 2 = model devine m*/ } } else //de la isValid { return(BadRequest(ModelState));//erorile care vin din partea modelState trimise de la user } } catch (Exception ex) { _logger.LogError($"Failed to save a new order: {ex}"); } return(BadRequest("Failed to save new order")); }