예제 #1
0
        public IHttpActionResult Put(StudentViewModel student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new SchoolMgmtEntities())
            {
                var existingStudent = ctx.Students.Where(s => s.StudentID == student.Id)
                                      .FirstOrDefault <Student>();

                if (existingStudent != null)
                {
                    existingStudent.FirstName = student.FirstName;
                    existingStudent.LastName  = student.LastName;

                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
예제 #2
0
        public IHttpActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return(BadRequest("Not a valid student id"));
            }

            using (var ctx = new SchoolMgmtEntities())
            {
                var student = ctx.Students
                              .Where(s => s.StudentID == id)
                              .FirstOrDefault();

                ctx.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                ctx.SaveChanges();
            }

            return(Ok());
        }
예제 #3
0
        public IHttpActionResult PostNewStudent(StudentViewModel student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }

            using (var ctx = new SchoolMgmtEntities())
            {
                ctx.Students.Add(new Student()
                {
                    StudentID = student.Id,
                    FirstName = student.FirstName,
                    LastName  = student.LastName
                });

                ctx.SaveChanges();
            }

            return(Ok());
        }