public IHttpActionResult Put(LawyerViewModel lawyer) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } using (var ctx = new LawyerDbEntities()) { var existingLawyer = ctx.Lawyers.Where(l => l.Id == lawyer.Id) .FirstOrDefault <Lawyer>(); if (existingLawyer != null) { existingLawyer.Name = lawyer.Name; existingLawyer.Surname = lawyer.Surname; existingLawyer.Initials = lawyer.Initials; existingLawyer.DateOfBirth = lawyer.DateOfBirth; existingLawyer.Email = lawyer.Email; existingLawyer.Gender = lawyer.Gender; existingLawyer.Title = lawyer.Title; ctx.SaveChanges(); } else { return(NotFound()); } } return(Ok()); }
//Get action methods of the previous section public IHttpActionResult PostNewLawyer(LawyerViewModel lawyer) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } using (var ctx = new LawyerDbEntities()) { ctx.Lawyers.Add(new Lawyer() { Id = lawyer.Id, Name = lawyer.Name, Surname = lawyer.Surname, Initials = lawyer.Initials, DateOfBirth = lawyer.DateOfBirth, Email = lawyer.Email, Gender = lawyer.Gender, Title = lawyer.Title }); ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult Delete(int id) { if (id <= 0) { return(BadRequest("Not a valid lawyer id")); } using (var ctx = new LawyerDbEntities()) { var lawyer = ctx.Lawyers .Where(l => l.Id == id) .FirstOrDefault(); ctx.Entry(lawyer).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }