Esempio n. 1
0
        public IHttpActionResult PostTeacher(TeacherModel teacher)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var dbTeacher = new Teacher();

            dbTeacher.Update(teacher);
            db.Teachers.Add(dbTeacher);
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw new Exception("Unable to add teacher to the database");
            }
            teacher.TeacherId = dbTeacher.TeacherId;

            return CreatedAtRoute("DefaultApi", new { id = teacher.TeacherId }, teacher);
        }
Esempio n. 2
0
 public void Update(TeacherModel teacher)
 {
     FirstName = teacher.FirstName;
     LastName = teacher.LastName;
     Email = teacher.Email;
 }
Esempio n. 3
0
        public IHttpActionResult PutTeacher(int id, TeacherModel teacher)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != teacher.TeacherId)
            {
                return BadRequest();
            }
            var dbTeacher = db.Teachers.Find(id);
            dbTeacher.Update(teacher);

            db.Entry(dbTeacher).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TeacherExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw new Exception("Unable to update teacher in the database.");
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }