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