public HttpResponseMessage Post(int id, RememberOptionModel model) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors())); } var app = config.Applications.All.SingleOrDefault(x => x.ID == id); if (app == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } var rememberOption = RememberOptionParser.Parse(model); if (app.RememberOptions.Any(x => x.Value == rememberOption.Value)) { ModelState.AddModelError("", "That Remember Option already exists"); return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors())); } app.RememberOptions.Add(rememberOption); config.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, new { rememberOption.ID, rememberOption.OptionLabel, rememberOption.Value })); }
public HttpResponseMessage Put(int id, RememberOptionModel model) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors())); } var rememberOption = config.RememberOptions.All.SingleOrDefault(x => x.ID == id); if (rememberOption == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } var query = (from a in this.config.Applications.All from s in a.RememberOptions where s.ID == id select a); var app = query.Single(); var updatedRememberOption = RememberOptionParser.Parse(model); if (app.RememberOptions.Any(x => x.Value == updatedRememberOption.Value && x.ID != id)) { ModelState.AddModelError("", "A remember option with that value already exists"); return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors())); } rememberOption.OptionLabel = updatedRememberOption.OptionLabel; rememberOption.Value = updatedRememberOption.Value; config.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.NoContent)); }