Пример #1
0
        public async Task<IHttpActionResult> PostTeacher(TeacherModel model)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var newTeacher = new Teacher()
            {
                FirstName = model.name,
                MiddleName = model.middleName,
                LastName = model.surname,
            };
              _context.Teachers.Add(newTeacher);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                //                if (ex.IsCausedByUniqueConstraintViolation())
                throw new ApplicationException("Group with such Key is already exists.");
                //                else
                //                   throw;
            }

            model.id = newTeacher.Id;
            return CreatedAtRoute("DefaultApi", new { id = model.id }, model);
        }
Пример #2
0
 public async Task<IHttpActionResult> PutTeacher(int id, TeacherModel model)
 {
     if (id != model.id)
         return BadRequest("No teacher with such id found");
     var teacher = _context.Teachers.Where(x => x.Id == id).FirstOrDefault();
     teacher.LastName = model.surname;
     teacher.MiddleName = model.middleName;
     teacher.FirstName = model.name;
     _context.SaveChanges();
     return CreatedAtRoute("DefaultApi", new { id = model.id }, model);
 }