Example #1
0
        public async Task <ActionResult <Course> > PostCourse(CoursePostDTO course)
        {
            var enrollments = new List <Enrollment>();
            var newCourse   = new Course
            {
                Title       = course.Title,
                CourseId    = course.CourseId,
                Subject     = course.Department,
                TeacherId   = course.TeacherAssigned.Id,
                CourseLevel = course.CourseLevel
            };

            context.Courses.Add(newCourse);
            await context.SaveChangesAsync();

            foreach (var student in course.EnrolledStudents)
            {
                enrollments.Add(new Enrollment
                {
                    StudentId = student.Id,
                    CourseId  = newCourse.Id
                });
            }

            context.Enrollments.AddRange(enrollments);
            await context.SaveChangesAsync();

            return(CreatedAtAction("GetCourse", new { id = newCourse.Id }, course));
        }
Example #2
0
        public async Task <IActionResult> PutCourse(int id, CoursePostDTO course)
        {
            if (!CourseExists(id))
            {
                return(NotFound());
            }

            // get course by id and update values
            var existingCourse = context.Courses.Find(id);

            existingCourse.Title       = course.Title;
            existingCourse.CourseId    = course.CourseId;
            existingCourse.Subject     = course.Department;
            existingCourse.Teacher     = course.TeacherAssigned;
            existingCourse.CourseLevel = course.CourseLevel;

            // get all enrollments for this course in db
            var enrollments = await context.Enrollments
                              .Where(e => e.CourseId == id)
                              .ToListAsync();

            var previousStudentIds = enrollments.Select(e => e.StudentId).ToList();
            var currentStudentIds  = course.EnrolledStudents.Select(s => s.Id).ToList();

            // remove all enrollments that aren't in new list
            var enrollmentsToRemove = enrollments
                                      .Where(e => !currentStudentIds.Contains(e.StudentId))
                                      .ToList();

            context.Enrollments.RemoveRange(enrollmentsToRemove);

            // add new enrollments that did not exist
            var studentsToEnroll = course.EnrolledStudents
                                   .Where(s => !previousStudentIds.Contains(s.Id))
                                   .ToList();

            var newEnrollments = new List <Enrollment>();

            foreach (var student in studentsToEnroll)
            {
                newEnrollments.Add(
                    new Enrollment
                {
                    StudentId = student.Id,
                    CourseId  = existingCourse.Id
                });
            }

            context.Enrollments.AddRange(newEnrollments);

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

            return(NoContent());
        }