//PUT or Update the data public IHttpActionResult PutCustomer(StudentDisplay student) { if (!ModelState.IsValid) { return(BadRequest("This is invalid Model. Please recheck")); } using (var x = new WebAPI_PeopleEntities()) { var checkExistingStudent = x.Students.Where(s => s.id == student.Id).FirstOrDefault <Student>(); if (checkExistingStudent != null) { checkExistingStudent.name = student.Name; checkExistingStudent.country = student.Country; checkExistingStudent.phone = student.Phone; checkExistingStudent.tempadd = student.TempAdd; x.SaveChanges(); } else { return(NotFound()); } } return(Ok()); }
//Delete a record in the database public IHttpActionResult Delete(int id) { if (id <= 0) { return(BadRequest("Please enter valid Customer Id")); } using (var x = new WebAPI_PeopleEntities()) { var student = x.Students.Where(s => s.id == id).FirstOrDefault(); x.Entry(student).State = System.Data.Entity.EntityState.Deleted; x.SaveChanges(); } return(Ok()); }
//Post The data public IHttpActionResult PostNewStudent(StudentDisplay student) { if (!ModelState.IsValid) { return(BadRequest("Invalid Data. Please Recheck!")); } using (var x = new WebAPI_PeopleEntities()) { x.Students.Add(new Student() { name = student.Name, email = student.Email, country = student.Country, phone = student.Phone, tempadd = student.TempAdd }); x.SaveChanges(); } return(Ok()); }
//GET -Retrieve data public IHttpActionResult GetAllStudent() { IList <StudentDisplay> students = null; using (var x = new WebAPI_PeopleEntities()) { students = x.Students .Select(c => new StudentDisplay() { Id = c.id, Name = c.name, Email = c.email, Country = c.country, Phone = c.phone, TempAdd = c.tempadd }).ToList <StudentDisplay>(); } if (students.Count == 0) { return(NotFound()); } return(Ok(students)); }