Ejemplo n.º 1
0
        public async Task <IActionResult> AddStudentCourse(string id)
        {
            //var userName = User.FindFirst(ClaimTypes.GivenName).Value;
            var userEmail    = User.FindFirst(ClaimTypes.Name).Value;
            var studentId    = _studentRepository.GetStudentIdByEmail(userEmail);
            var model        = _courseRepository.GetCourses(Convert.ToInt32(id));
            var studentModel = await _studentRepository.GetStudentAsync(studentId);

            try
            {
                if (ModelState.IsValid)
                {
                    Course course = new Course
                    {
                        Instructor = model.Instructor,
                        CourseName = model.CourseName
                    };

                    Student student = new Student
                    {
                        FullName       = studentModel.FullName,
                        Email          = studentModel.Email,
                        Gender         = studentModel.Gender,
                        EnrollmentDate = studentModel.EnrollmentDate
                    };

                    StudentCourse studentCourse = new StudentCourse
                    {
                        Course    = course,
                        CourseId  = Convert.ToInt32(id),
                        StudentId = studentId,
                        Student   = student
                    };
                    _studentCourseRepository.AddStudentCourse(studentCourse);
                    _studentCourseRepository.Save();
                }
                else
                {
                    return(Ok(new { status = "error" }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("There was an error: {0}", ex.Message);
                return(Ok(new { status = "error" }));
            }

            return(Ok(new { status = "success" }));
        }