public HttpResponseMessage Put(int id, Event mitoEvent) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != mitoEvent.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(mitoEvent).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT api/AdminUser/5 (Update in CRUD) public HttpResponseMessage PutAdminUser(int id, AdminUser user) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } // email must be unique if (db.Admins.Where(adminUser => adminUser.Email.ToLower() == user.Email.ToLower() && adminUser.ID != id).Count() > 0) { ModelState.AddModelError("EmailNotUnique", "The email address provided is not unique."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != user.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(user).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public ActionResult Edit([Bind(Include = "Id,Data,Nome,Tag, Texto")] Arquivo arquivo) { if (ModelState.IsValid) { db.Entry(arquivo).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(arquivo)); }
// PUT api/BlogPost/5 (Update in CRUD) public HttpResponseMessage PutBlogPost(int id, BlogPost post) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != post.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } // figure out if the post was previously published // we have to use a new context to mess with the states bool bWasThePostPreviouslyPublished = new SiteDB().BlogPosts.Find(id).Published.GetValueOrDefault(false); // set the last updated date. post.UpdatedDate = DateTime.Now; // check to make sure the SEO link is unique if (db.BlogPosts.FirstOrDefault(currPost => currPost.SEOLink == post.SEOLink && currPost.ID != id) != null) { ModelState.AddModelError("SEOLink", "The SEO link must be unique and cannot match an existing blog post"); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } db.Entry(post).State = EntityState.Modified; try { db.SaveChanges(); // if the post was not previously published, but now is, we need to send emails to those // that were signed up to receive emails. if (!bWasThePostPreviouslyPublished && post.Published.GetValueOrDefault(false)) { foreach (var notificationItem in db.NotifiedList) { SendPostedEmail(notificationItem.Email, post); } } } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }