public async Task<IHttpActionResult> PostOrder(Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Insert order _unitOfWork.OrderRepository.Insert(order); try { // Save and accept changes await _unitOfWork.SaveChangesAsync(); } catch (UpdateException) { if (_unitOfWork.OrderRepository.Find(order.OrderId) == null) { return Conflict(); } throw; } // Load related entities and accept changes await _unitOfWork.OrderRepository.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return CreatedAtRoute("DefaultApi", new { id = order.OrderId }, order); }
// POST api/Orders public HttpResponseMessage PostOrder(Order order) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } order.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(order); try { _dbContext.SaveChanges(); } catch (DbUpdateException ex) { if (_dbContext.Orders.Any(o => o.OrderId == order.OrderId)) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex); } throw; } _dbContext.LoadRelatedEntities(order); order.AcceptChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, order); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = order.OrderId })); return response; }
public async Task<IHttpActionResult> PostOrder(Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } order.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(order); await _dbContext.SaveChangesAsync(); await _dbContext.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return CreatedAtRoute("DefaultApi", new { id = order.OrderId }, order); }
public async Task<IHttpActionResult> PutOrder(Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _dbContext.ApplyChanges(order); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbContext.Orders.Any(o => o.OrderId == order.OrderId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return Ok(order); }
public async Task<IHttpActionResult> PutOrder(Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Update order _unitOfWork.OrderRepository.Update(order); try { await _unitOfWork.SaveChangesAsync(); } catch (UpdateConcurrencyException) { if (_unitOfWork.OrderRepository.Find(order.OrderId) == null) { return Conflict(); } throw; } // Load related entities and accept changes await _unitOfWork.OrderRepository.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return Ok(order); }
// PUT api/Orders public HttpResponseMessage PutOrder(Order order) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } _dbContext.ApplyChanges(order); try { _dbContext.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { if (!_dbContext.Orders.Any(o => o.OrderId == order.OrderId)) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex); } throw; } _dbContext.LoadRelatedEntities(order); order.AcceptChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, order); return response; }