public async Task <ActionResult> Patch([FromBody] Patch[] patches, int id) { if (patches == null) { return(new BadRequestResult()); } var city = await _database.FetchCityAsync(id); if (city == null) { return(new HttpNotFoundResult()); } var validationResult = _validator.Validate(patches, city); if (validationResult.HasErrors) { return(new UnprocessableEntityObjectResult(validationResult.Errors)); } ModelPatcher.Apply(patches, city); var result = await _database.UpdateCityAsync(city); return(new HttpOkObjectResult(result)); }
public async Task<IHttpActionResult> Patch( [FromODataUri] int? key, JObject json ) { string jsonString = string.Format( "{0}", json ); ModelPatcher<WebUser> patcher = new ModelPatcher<WebUser>(); patcher.Patch( json ); Validate( ( WebUser ) patcher.GetPatch() ); if ( !ModelState.IsValid ) { return BadRequest( ModelState ); } WebUser webUser = await db.WebUserSet.FindAsync( key ); if ( webUser == null ) { return NotFound(); } try { if ( !webUser.Equals( patcher.GetPatch() ) ) { throw new DBConcurrencyException( "Entity changed in the meantime!" ); } patcher.Merge( webUser ); await db.SaveChangesAsync(); string currentVersion = webUser.Version; } catch ( DBConcurrencyException e ) { string msg = e.Message; Log.Error( msg, e ); return new ErrorResult( e.Message, Request, HttpStatusCode.Conflict ); } catch ( Exception e ) { Log.Error( e ); if ( !WebUserExists( key ) ) { return NotFound(); } else { throw; } } return Updated( webUser ); //return StatusCode( HttpStatusCode.NoContent ); }