Exemple #1
0
        public IHttpActionResult CreateCourse(CourseCreator courseCreator)
        {
            coursesService         = new CoursesService();
            subjectsService        = new SubjectsService();
            teachersService        = new TeachersService();
            subjectToExposeService = new SubjectToExposeService();
            courseToExposeService  = new CourseToExposeService();

            if (courseCreator == null)
            {
                return(BadRequest());
            }

            bool exists = false;

            exists = coursesService.CheckIfCourseAlreadyExists(courseCreator.Title);

            if (exists)
            {
                return(BadRequest());
            }

            List <CourseSubject> courseSubjects = coursesService.CreateCourse(courseCreator);

            List <SubjectToExpose> subjectsToExpose = new List <SubjectToExpose>();

            foreach (CourseSubject courseSubject in courseSubjects)
            {
                Subject subject = new Subject();
                subject = subjectsService.Find(courseSubject.SubjectId);

                if (subject == null)
                {
                    return(BadRequest());
                }

                if (subject.ID == 0)
                {
                    return(BadRequest());
                }

                Teacher teacher = new Teacher();

                teacher = teachersService.GetTeacherNameByID(subject.TeacherId);

                if (teacher == null)
                {
                    return(BadRequest());
                }

                SubjectToExpose subjectToExpose = subjectToExposeService.TrimSubject(subject);

                subjectsToExpose.Add(subjectToExpose);
            }

            CourseToExpose course = courseToExposeService
                                    .TrimSubject(courseSubjects, courseCreator, subjectsToExpose);

            return(Created(new Uri(Request.RequestUri + "/" + course.Id), course));
        }
Exemple #2
0
        public SubjectToExpose TrimSubject(Subject subject)
        {
            SubjectToExpose subjectToExpose;
            string          teacherName;

            Teacher teacher = db.Teachers.Find(subject.TeacherId);

            if (teacher != null)
            {
                teacherName = teacher.Name;
            }
            else
            {
                return(null);
            }

            subjectToExpose = new SubjectToExpose()
            {
                ID          = subject.ID,
                Title       = subject.Title,
                Credits     = subject.Credits,
                TeacherId   = subject.TeacherId,
                TeacherName = teacherName
            };

            return(subjectToExpose);
        }
Exemple #3
0
        public List <SubjectToExpose> TrimSubjects(List <Subject> subjects)
        {
            List <SubjectToExpose> subjectsToExpose = new List <SubjectToExpose>();

            string teacherName;

            foreach (Subject subject in subjects)
            {
                Teacher teacher = db.Teachers.Find(subject.TeacherId);
                if (teacher != null)
                {
                    teacherName = teacher.Name;
                }
                else
                {
                    teacherName = null;
                }

                SubjectToExpose subjectToExpose = new SubjectToExpose()
                {
                    ID          = subject.ID,
                    Title       = subject.Title,
                    Credits     = subject.Credits,
                    TeacherId   = subject.TeacherId,
                    TeacherName = teacherName
                };

                subjectsToExpose.Add(subjectToExpose);
            }

            return(subjectsToExpose);
        }
Exemple #4
0
        // GET /api/subject/id
        public IHttpActionResult GetSubject(int id)
        {
            subjectsService        = new SubjectsService();
            subjectToExposeService = new SubjectToExposeService();

            if (id == 0)
            {
                return(NotFound());
            }

            Subject subject = subjectsService.GetSubjectById(id);

            if (subject == null)
            {
                return(NotFound());
            }

            SubjectToExpose subjectToExpose = subjectToExposeService.TrimSubject(subject);

            return(Ok(subjectToExpose));
        }