public PartialViewResult AddCourses(FormCollection form) { if (string.IsNullOrEmpty(form["StudentId"])) { throw new ValidationException("id is null", ""); } if (string.IsNullOrEmpty(form["CourseId"])) { throw new ValidationException("id is null", ""); } int studentId = Convert.ToInt32(form["StudentId"]); int courseId = Convert.ToInt32(form["CourseId"]); var student = studentService.GetStudent(studentId); var course = courseService.GetCourse(courseId); studentService.AddCourse(studentId, courseId); Thread.Sleep(1000); var studentsCourses = student.Courses; var config = new MapperConfiguration(cfg => cfg.CreateMap <Course, CourseDTO>() .ForMember("NumberOfStudents", opt => opt.MapFrom(c => c.Students.Count))); var mapper = new Mapper(config); var courseDtosOfStudent = mapper.Map <List <CourseDTO> >(studentsCourses); if (student == null) { throw new ValidationException("Student with tihis id doesn't exist", ""); } var courses = courseService.GetCourses().ToList(); List <CourseAddForStudent> res = new List <CourseAddForStudent>(); List <int> idC = new List <int> { }; List <int> idCS = new List <int> { courseId }; foreach (var i in courses) { idC.Add(i.CourseId); } foreach (var i in courseDtosOfStudent) { idCS.Add(i.CourseId); } List <int> idRes = idC.Except(idCS).ToList(); foreach (var i in courses) { if (idRes.Contains(i.CourseId) && i.ForTerm == student.StudentTerm && i.CourseStart >= DateTime.Now && i.IsDeleted == false) { res.Add(new CourseAddForStudent { CourseId = i.CourseId, CourseName = i.CourseName, Lecturer = i.Lecturer, Agree = false, CourseStart = i.CourseStart.ToString().Split(' ')[0], CourseEnd = i.CourseEnd.ToString().Split(' ')[0] }); } } CourseAddForStudentID resId = new CourseAddForStudentID { Id = Convert.ToInt32(studentId), Courses = res }; ViewBag.Info = "Successfully! You will see this course on your profile page with your next visit!"; return(PartialView(resId)); }
public async Task <bool> AddCourse(int courseId, int studentId) { return(await service.AddCourse(courseId, studentId)); }