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); }
public SubjectsPageViewModel() { this.service = new SubjectsService(); this.Subjects = new ObservableCollection <SubjectDTONotify>(this.service .GetSubjects() .Select(s => new SubjectDTONotify(s))); }
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)); }
public void When_AllSubjectsRunsAgainstNonEmptyDatabase_Then_AllLecturersAreReturned() { // Arrange var service = new SubjectsService(_mockQueryAllSubjects); // Act var allSubjects = service.All(); // Assert allSubjects.Count().Should().Be(4); }
public void When_AllSubjectsRunsAgainstEmptyDatabase_Then_EmptyCollectionIsReturned() { // Arrange var service = new SubjectsService(new GetAllSubjectsQuery(TestDatabaseFactory.CreateEmptyDatabase())); // Act var allSubjects = service.All(); // Assert allSubjects.Should().BeEmpty(); }
// GET /api/subjects/ public IHttpActionResult GetSubjects() { subjectsService = new SubjectsService(); List <SubjectToExpose> subjectsToExpose = subjectsService.GetSubjects(); if (subjectsToExpose.Count() == 0) { return(NotFound()); } return(Ok(subjectsToExpose)); }
// 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)); }
// 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)); }
// 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; }