public async Task <IActionResult> PutEnrollment([FromRoute] int id, [FromBody] Enrollment enrollment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != enrollment.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> Create([Bind("ID,LastName,FirstMidName,EnrollmentDate")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                await _context.SaveChangesAsync(true);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }
Exemple #3
0
        public async Task <Unit> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
        {
            if (request.DepartmentID == null)
            {
                throw new NotFoundException(nameof(Department), request.DepartmentID);
            }

            var departmentToUpdate = await _context.Departments
                                     .Include(i => i.Administrator)
                                     .FirstOrDefaultAsync(m => m.DepartmentID == request.DepartmentID);

            if (departmentToUpdate == null)
            {
                throw new NotFoundException(nameof(Department), request.DepartmentID);
            }

            departmentToUpdate.Name         = request.Name;
            departmentToUpdate.Budget       = request.Budget;
            departmentToUpdate.StartDate    = request.StartDate;
            departmentToUpdate.InstructorID = request.InstructorID;

            _context.Entry <Department>(departmentToUpdate).Property("RowVersion").OriginalValue = request.RowVersion;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #4
0
        public async Task <Unit> Handle(UpdateInstructorCommand request, CancellationToken cancellationToken)
        {
            if (request.InstructorID == null)
            {
                throw new NotFoundException(nameof(Instructor), request.InstructorID);
            }

            var instructorToUpdate = await _context.Instructors
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.CourseAssignments)
                                     .ThenInclude(i => i.Course)
                                     .FirstOrDefaultAsync(m => m.ID == request.InstructorID);

            if (instructorToUpdate == null)
            {
                throw new NotFoundException(nameof(Instructor), request.InstructorID);
            }

            instructorToUpdate.LastName     = request.LastName;
            instructorToUpdate.FirstMidName = request.FirstName;
            instructorToUpdate.HireDate     = request.HireDate;

            if (instructorToUpdate.OfficeAssignment == null)
            {
                instructorToUpdate.OfficeAssignment = new OfficeAssignment();
            }

            instructorToUpdate.OfficeAssignment.Location = request.OfficeLocation;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #5
0
        public async Task<Unit> Handle(DeleteDepartmentCommand request, CancellationToken cancellationToken)
        {
            var department = await _context.Departments.FindAsync(request.ID);

            if (department == null)
                throw new NotFoundException(nameof(Department), request.ID);

            _context.Departments.Remove(department);
            await _context.SaveChangesAsync(cancellationToken);

            return Unit.Value;
        }
        public async Task <Unit> Handle(CreateStudentCommand request, CancellationToken cancellationToken)
        {
            _context.Students.Add(new Student
            {
                FirstMidName   = request.FirstName,
                LastName       = request.LastName,
                EnrollmentDate = request.EnrollmentDate
            });

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreateInstructorCommand request, CancellationToken cancellationToken)
        {
            _context.Instructors.Add(new Instructor
            {
                FirstMidName = request.FirstName,
                LastName     = request.LastName,
                HireDate     = request.HireDate
            });

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #8
0
        public async Task <Unit> Handle(CreateDepartmentCommand request, CancellationToken cancellationToken)
        {
            _context.Departments.Add(new Department
            {
                Name         = request.Name,
                Budget       = request.Budget,
                StartDate    = request.StartDate,
                InstructorID = request.InstructorID
            });

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteStudentCommand request, CancellationToken cancellationToken)
        {
            var student = await _context.Students.FindAsync(request.ID);

            if (student == null)
            {
                throw new NotFoundException(nameof(Student), request.ID);
            }

            _context.Students.Remove(student);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #10
0
        public async Task <Unit> Handle(DeleteCourseCommand request, CancellationToken cancellationToken)
        {
            var course = await _context.Courses.FindAsync(request.ID);

            if (course == null)
            {
                throw new NotFoundException(nameof(Course), request.ID);
            }

            _context.Courses.Remove(course);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreateCourseCommand request, CancellationToken cancellationToken)
        {
            var course = new Course
            {
                CourseID     = request.CourseID,
                Title        = request.Title,
                Credits      = request.Credits,
                DepartmentID = request.DepartmentID
            };

            _context.Courses.Add(course);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #12
0
        public async Task <Unit> Handle(UpdateCourseAssignmentsCommand request, CancellationToken cancellationToken)
        {
            if (request.InstructorID == null)
            {
                throw new NotFoundException(nameof(Instructor), request.InstructorID);
            }

            var instructor = await _context.Instructors.Include(x => x.CourseAssignments).Where(x => x.ID == request.InstructorID).FirstOrDefaultAsync();

            if (instructor == null)
            {
                throw new NotFoundException(nameof(Instructor), request.InstructorID);
            }

            if (request.SelectedCourses == null)
            {
                instructor.CourseAssignments = new List <Domain.Entities.CourseAssignment>();
            }

            var selectedCoursesHS = new HashSet <string>(request.SelectedCourses);
            var instructorCourses = new HashSet <int>
                                        (instructor.CourseAssignments.Select(c => c.CourseID));

            foreach (var course in _context.Courses)
            {
                if (selectedCoursesHS.Contains(course.CourseID.ToString()))
                {
                    if (!instructorCourses.Contains(course.CourseID))
                    {
                        instructor.CourseAssignments.Add(new Domain.Entities.CourseAssignment {
                            InstructorID = instructor.ID, CourseID = course.CourseID
                        });
                    }
                }
                else
                {
                    if (instructorCourses.Contains(course.CourseID))
                    {
                        Domain.Entities.CourseAssignment courseToRemove = instructor.CourseAssignments.FirstOrDefault(i => i.CourseID == course.CourseID);
                        _context.CourseAssignments.Remove(courseToRemove);
                    }
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #13
0
        public async Task <Unit> Handle(UpdateCourseCommand request, CancellationToken cancellationToken)
        {
            if (request.CourseID == null)
            {
                throw new NotFoundException(nameof(Course), request.CourseID);
            }

            var courseToUpdate = await _context.Courses
                                 .FirstOrDefaultAsync(c => c.CourseID == request.CourseID);

            courseToUpdate.Title        = request.Title;
            courseToUpdate.Credits      = request.Credits;
            courseToUpdate.DepartmentID = request.DepartmentID;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateStudentCommand request, CancellationToken cancellationToken)
        {
            if (request.StudentID == null)
            {
                throw new NotFoundException(nameof(Student), request.StudentID);
            }

            var studentToUpdate = await _context.Students
                                  .FirstOrDefaultAsync(s => s.ID == request.StudentID);

            studentToUpdate.FirstMidName   = request.FirstName;
            studentToUpdate.LastName       = request.LastName;
            studentToUpdate.EnrollmentDate = request.EnrollmentDate;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #15
0
        public async Task<Unit> Handle(DeleteInstructorCommand request, CancellationToken cancellationToken)
        {
            var instructor = await _context.Instructors
                .Include(i => i.CourseAssignments)
                .SingleAsync(i => i.ID == request.ID);

            if (instructor == null)
                throw new NotFoundException(nameof(Instructor), request.ID);

            var departments = await _context.Departments
                .Where(d => d.InstructorID == request.ID)
                .ToListAsync();
            departments.ForEach(d => d.InstructorID = null);

            _context.Instructors.Remove(instructor);
            await _context.SaveChangesAsync(cancellationToken);

            return Unit.Value;
        }
Exemple #16
0
        private async Task AddEnrollments(Student[] students, Course[] courses)
        {
            var enrollments = new Enrollment[]
            {
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alexander").ID,
                    CourseID  = courses.Single(c => c.Title == "Chemistry").CourseID,
                    Grade     = Grade.A
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alexander").ID,
                    CourseID  = courses.Single(c => c.Title == "Microeconomics").CourseID,
                    Grade     = Grade.C
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alexander").ID,
                    CourseID  = courses.Single(c => c.Title == "Macroeconomics").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alonso").ID,
                    CourseID  = courses.Single(c => c.Title == "Calculus").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alonso").ID,
                    CourseID  = courses.Single(c => c.Title == "Trigonometry").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Alonso").ID,
                    CourseID  = courses.Single(c => c.Title == "Composition").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Anand").ID,
                    CourseID  = courses.Single(c => c.Title == "Chemistry").CourseID
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Anand").ID,
                    CourseID  = courses.Single(c => c.Title == "Microeconomics").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Barzdukas").ID,
                    CourseID  = courses.Single(c => c.Title == "Chemistry").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Li").ID,
                    CourseID  = courses.Single(c => c.Title == "Composition").CourseID,
                    Grade     = Grade.B
                },
                new Enrollment {
                    StudentID = students.Single(s => s.LastName == "Justice").ID,
                    CourseID  = courses.Single(c => c.Title == "Literature").CourseID,
                    Grade     = Grade.B
                }
            };

            foreach (Enrollment e in enrollments)
            {
                var enrollmentInDataBase = _context.Enrollments.Where(
                    s =>
                    s.Student.ID == e.StudentID &&
                    s.Course.CourseID == e.CourseID).SingleOrDefault();
                if (enrollmentInDataBase == null)
                {
                    _context.Enrollments.Add(e);
                }
            }
            await _context.SaveChangesAsync();
        }