示例#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));
        }
        public void SubjectsServiceTestsInitialize()
        {
            var algebra = new Subject()
            {
                Id      = 1,
                Title   = "Алгебра",
                Teacher = new Teacher()
                {
                    Name = "Эйлер"
                },
                Description  = "Описание алгебры",
                Difficulty   = Difficulty.Hard,
                FromSemester = Semester.First,
                ToSemester   = Semester.Second
            };

            var analysis = new Subject()
            {
                Id      = 2,
                Title   = "Матан",
                Teacher = new Teacher()
                {
                    Name = "Коши"
                },
                Description  = "Описание матана",
                Difficulty   = Difficulty.Death,
                FromSemester = Semester.First,
                ToSemester   = Semester.Third
            };

            var subjects = new List <Subject>()
            {
                algebra, analysis
            };

            _mock = new Mock <IRepository <Subject> >();
            _mock.Setup(m => m.Add(It.IsAny <Subject>())).Callback <Subject>(subjects.Add);
            _mock.Setup(m => m.Count()).Returns(subjects.Count);
            _mock.Setup(m => m.Delete(It.IsAny <int>()))
            .Callback <int>(id =>
            {
                var subject = subjects.Find(item => item.Id == id);
                subjects.Remove(subject);
            });
            _mock.Setup(m => m.GetAll()).Returns(subjects.AsQueryable <Subject>());
            _mock.Setup(m => m.GetById(It.IsAny <int>()))
            .Returns <int>(id => subjects.Find(item => item.Id == id));
            _mock.Setup(m => m.SaveOrUpdate(It.IsAny <Subject>()))
            .Callback <Subject>(subject =>
            {
                var oldSubject = subjects.Find(item => item.Id == subject.Id);
                if (oldSubject != null)
                {
                    subjects.Remove(oldSubject);
                }
                subjects.Add(subject);
            });

            _subjectsService = new SubjectsService(_mock.Object);
        }
示例#3
0
 public SubjectsPageViewModel()
 {
     this.service  = new SubjectsService();
     this.Subjects = new ObservableCollection <SubjectDTONotify>(this.service
                                                                 .GetSubjects()
                                                                 .Select(s => new SubjectDTONotify(s)));
 }
示例#4
0
        public IHttpActionResult UpdateSubject(int id, SubjectDTO subjectDTO)
        {
            subjectsService        = new SubjectsService();
            subjectToExposeService = new SubjectToExposeService();

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

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

            Subject subject    = Mapper.Map <SubjectDTO, Subject>(subjectDTO);
            Subject theSubject = new Subject();

            theSubject = subjectsService.UpdateSubject(id, subject);

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

            //SubjectToExpose subjectToExpose = subjectToExposeService.TrimSubject(subject);

            SubjectDTO theSubjectDTO = Mapper.Map <Subject, SubjectDTO>(theSubject);

            return(Ok(theSubjectDTO));
        }
示例#5
0
        public void When_AllSubjectsRunsAgainstNonEmptyDatabase_Then_AllLecturersAreReturned()
        {
            // Arrange
            var service = new SubjectsService(_mockQueryAllSubjects);

            // Act
            var allSubjects = service.All();

            // Assert
            allSubjects.Count().Should().Be(4);
        }
示例#6
0
        public void When_AllSubjectsRunsAgainstEmptyDatabase_Then_EmptyCollectionIsReturned()
        {
            // Arrange
            var service = new SubjectsService(new GetAllSubjectsQuery(TestDatabaseFactory.CreateEmptyDatabase()));

            // Act
            var allSubjects = service.All();

            // Assert
            allSubjects.Should().BeEmpty();
        }
示例#7
0
        // GET /api/subjects/
        public IHttpActionResult GetSubjects()
        {
            subjectsService = new SubjectsService();

            List <SubjectToExpose> subjectsToExpose = subjectsService.GetSubjects();

            if (subjectsToExpose.Count() == 0)
            {
                return(NotFound());
            }

            return(Ok(subjectsToExpose));
        }
示例#8
0
        // POST /api/subjects
        public IHttpActionResult CreateSubject(SubjectDTO subjectDTO)
        {
            if (subjectDTO == null)
            {
                return(BadRequest());
            }

            subjectsService = new SubjectsService();
            teachersService = new TeachersService();

            Subject subject = Mapper.Map <SubjectDTO, Subject>(subjectDTO);

            // check if the subject already exists
            Subject newSubject = subjectsService.CheckIfSubjectExistsByName(subject);

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

            // if the subject teacher already has a subject it's a bad request
            bool teacherHasASubject = false;

            teacherHasASubject = teachersService.CheckIfTeacherHasSubject(subject.TeacherId);

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

            // you can only create a subject if there's a teacher available to teach it
            if (teachersService.GetNonAllocatedTeachers().Count() <= 0)
            {
                return(BadRequest());
            }
            ;

            // will give the subject to a non-allocated teacher
            Subject theSubject = subjectsService.CreateSubject(subject);

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

            SubjectDTO theSubjectDTO = Mapper.Map <Subject, SubjectDTO>(theSubject);

            return(Created(new Uri(Request.RequestUri + "/" + theSubjectDTO.ID), theSubjectDTO));
        }
示例#9
0
        // DELETE /api/subjects/id
        public IHttpActionResult DeleteSubject(int id)
        {
            if (id == 0)
            {
                return(NotFound());
            }

            subjectsService = new SubjectsService();

            if (!subjectsService.CheckIfSubjectExists(id))
            {
                return(NotFound());
            }

            subjectsService.DeleteSubject(id);

            List <SubjectToExpose> subjectsToExpose = subjectsService.GetSubjects();

            return(Ok(subjectsToExpose));
        }
示例#10
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));
        }
 public SubjectsController(SubjectsService service)
 {
     _service = service;
 }