public async Task <IActionResult> PutStudent(double id, Student student)
        {
            if (id != student.Id)
            {
                return(BadRequest());
            }

            _context.Entry(student).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutTeacher(int id, Teacher teacher)
        {
            if (id != teacher.Id)
            {
                return(BadRequest());
            }

            _context.Entry(teacher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TeacherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#3
0
        public async Task <IActionResult> PutCourse([FromRoute] int id, [FromBody] CourseStudentEditViewModel model)
        {
            if (id != model.Course.Id)
            {
                return(BadRequest());
            }

            _context.Entry(model.Course).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                IEnumerable <double>    listStudents = model.SelectedStudents;
                IQueryable <Enrollment> toBeRemoved  = _context.Enrollment.Where(s => !listStudents.Contains(s.StudentId) && s.CourseId == id);
                _context.Enrollment.RemoveRange(toBeRemoved);
                IEnumerable <double> existStudents = _context.Enrollment.Where(s => listStudents.Contains(s.StudentId) && s.CourseId == id).Select(s => s.StudentId);
                IEnumerable <double> newStudents   = listStudents.Where(s => !existStudents.Contains(s));
                foreach (int studentId in newStudents)
                {
                    _context.Enrollment.Add(new Enrollment {
                        StudentId = studentId, CourseId = id
                    });
                }
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CourseExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }