public IHttpActionResult UpdateCharMediaById(EditCharMedia editCharMedia) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateCharMediaService(); string errorText = service.UpdateCharMediaById(editCharMedia); if (errorText == null) { return(Ok("Update completed")); } return(BadRequest(errorText)); }
public string UpdateCharMediaById(EditCharMedia model) { using (var ctx = new ApplicationDbContext()) { if (ctx.CharMedia.Count(e => e.CharMediaId == model.CharMediaId) == 0) { return("Record not found in table"); } if (ctx.Characters.Count(e => e.CharId == model.CharId) == 0) { return($"Character {model.CharId} NOT found in table"); } if (ctx.Media.Count(e => e.MediaId == model.MediaId) == 0) { return($"Media {model.MediaId} NOT found in table"); } if (ctx.CharMedia.Count(e => e.CharId == model.CharId && e.MediaId == model.MediaId) != 0) { return("Combination already exists in table"); } var entity = ctx .CharMedia .Single(e => e.CharMediaId == model.CharMediaId); entity.CharId = model.CharId; entity.MediaId = model.MediaId; if (ctx.SaveChanges() == 1) { return(null); } return("Update failed - unknown error"); } }