public IHttpActionResult GetAllTeachers() { string role = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value; try { switch (role) { case "admin": var retVal1 = service.GetAll(); logger.Info("Returning all teachers."); return(Ok(retVal1)); case "teacher": string teacherId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; var retVal2 = service.GetById(teacherId); logger.Info("Returning teacher with id {0} to self.", teacherId); return(Ok(retVal2)); default: logger.Warn("BadRequest. There is no method for this role! {0}", role); return(BadRequest()); } } catch (Exception e) { logger.Warn("Caught exception with message {0}", e.Message); throw new HttpResponseException(HttpStatusCode.NotFound); } }
public IHttpActionResult AddTeacherToStudentSubject(string studentId, int subjectId, string teacherId) { Student student = studentsService.GetById(studentId); Teacher teacher = teachersService.GetById(teacherId); if (student == null || teacher == null || subjectsService.GetById(subjectId) == null) { return(NotFound()); } try { if (!student.StudentAttendsSubject.Select(x => x.Subject.SubjectId).Contains(subjectId)) { throw new NullReferenceException("Student doesn't attend this subject!"); } else if (!teacher.TeacherTeachesSubject.Select(x => x.Subject?.SubjectId).Contains(subjectId)) { throw new NullReferenceException("Teacher doesn't teach this subject and therefore cannot be added to this student-subject pair!"); } else { logger.Info("Linking teacher with student-subject"); studentsService.AddTeacherToStudentSubject(studentId, subjectId, teacherId); return(Ok(student.StudentAttendsSubject.Select(x => x.TacherTeachesSubject?.Teacher))); } } catch (NullReferenceException e) { logger.Error(e.Message, "Adding teacher to student-subject pair"); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message))); } }
public SubjectTeacherDTOOut AddTeacherToSubject(int id, SubjectTEacherDTOIn subjectToUpdt) { Subject subject = GetSubjectByID(id); HashSet <Teacher> teachers = new HashSet <Teacher>(); foreach (var ID in subjectToUpdt.TeacherID) { Teacher teacher = teachersService.GetById(ID); teachers.Add(teacher); } subject.Teachers = teachers; db.SubjectsRepository.Update(subject); db.Save(); IEnumerable <TeacherDTOOutReg> teachersUpdate = teachers.Select(x => Utilities.ConverterDTO.SimpleDTOConverter <TeacherDTOOutReg>(x)); SubjectTeacherDTOOut updatedSubject = new SubjectTeacherDTOOut() { Subject = Utilities.ConverterDTO.SimpleDTOConverter <SubjectDTO>(subject), Teachers = teachersUpdate }; return(updatedSubject); }
public async Task <IActionResult> EditTeacher(int id) { var user = await _userManager.GetUserAsync(User); var isAdmin = await IsAdmin(); var schools = _schoolsServices.GetAllByUserId <SchoolViewModel>(user?.UniqueGradebookId, isAdmin); var teacher = _teachersService.GetById <TeacherInputModel>(id); var inputModel = new TeacherModifyInputModel { Id = id, Schools = schools.Select(s => new SelectListItem(s.Name, s.Id.ToString())).ToList(), Teacher = teacher }; return(View(inputModel)); }
public IHttpActionResult GetTeacherById(int id) { Teacher teacher = teachersService.GetById(id); if (teacher == null) { return(NotFound()); } return(Ok(teacher)); }
public IHttpActionResult GetTeacherById(string id) { Teacher teacher = teachersService.GetById(id); if (teacher == null) { return(NotFound()); } logger.Info("Requesting teacher by id"); return(Ok(teacher)); }
public HttpResponseMessage GetTeacherById(string id) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value; logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Teacher by id: " + id); try { Teacher teacher = teachersService.GetById(id); if (teacher == null) { logger.Info("The teacher with id: " + id + " was not found."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "The teacher with id: " + id + " was not found.")); } if (userRole == "admin") { logger.Info("Requesting found teacher convert for " + userRole + "role."); TeacherDTOForAdmin dto = toDTO.ConvertToTeacherDTOForAdmin(teacher, (List <IdentityUserRole>)teacher.Roles); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (userRole == "teacher" && teacher.IsStillWorking == true) { logger.Info("Requesting found teacher convert for " + userRole + "role."); TeacherDTOForTeacher dto = toDTO.ConvertToTeacherDTOForTeacher(teacher); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (teacher.IsStillWorking == true && (userRole == "student" || userRole == "parent")) { logger.Info("Requesting found teacher convert for " + userRole + "role."); TeacherDTOForStudentAndParent dto = toDTO.ConvertToTeacherDTOForStudentAndParent(teacher); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else //zbog provere teacher.IsStillWorking { logger.Info("Authorisation failure. User " + userId + " is not authorised for this request."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Access Denied. " + "We’re sorry, but you are not authorized to perform the requested operation.")); } } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public TeachDTOOut CreateTeach(TeachDTOIn newTeach) { logger.Info("Get student department over student department service, create teach teaches service"); StudentDepartment studentDepartment = studentDepartmentsService.GetStudentDepartmentByID(newTeach.StudentDepartmentID); logger.Info("Get teacher over teacher service, create teach teaches service"); Teacher teacher = teachersService.GetById(newTeach.TeacherID); logger.Info("Get subject over subject service, create teach teaches service"); Subject subject = subjectsService.GetSubjectByID(newTeach.SubjectID); logger.Info("Get teaches over teach service, create teach teaches service"); IEnumerable <Teach> teach = GetAllTeach(); //Check if subject is for students grade logger.Info("Checking if subject is for the selected grade, create teaches, teaches service"); var isGradeOk = subject.Grades.Any(x => x.Id == studentDepartment.Departments.Grades.Id); if (!isGradeOk) { throw new SubjectIsNotForThisGrade("Wrong subject"); } //check if teacher is teaching this subject logger.Info("Checking if teacher is teaching subject"); var result = teacher.Subjects.Contains(subject); if (!result) { logger.Info("Throwing exception, wrong teacher subject pair"); throw new TeacherDontTeachThisSubjectException("Teacher - subject pair does not match"); } //check if subject is alreaady added to student logger.Info("Checking if subject is already added to student"); var subjectExists = teach.Any(x => x.Subject.Equals(subject) && x.StudentDepartments.Equals(studentDepartment)); if (subjectExists) { logger.Info("Throwing exception subject already added to student"); throw new SubjectExistsException($"Subject {subject.SubjectName} already added to student {studentDepartment.Students.UserName}"); } //check if teacher teach to this department, and finds who teach logger.Info("Checking if theacher is teach in department"); var isDepartmentOK = teach.Any(x => x.Subject.Equals(subject) && x.Teachers.Equals(teacher) && x.StudentDepartments.Departments.Id.Equals(studentDepartment.Departments.Id)); var isSubjectInDepartment = teach.Any(x => x.Subject.Equals(subject) && x.StudentDepartments.Departments.Id.Equals(studentDepartment.Departments.Id)); var whoTeach = teach.Where(x => x.Subject.Equals(subject) && x.StudentDepartments.Equals(studentDepartment)).Select(x => x.Teachers.UserName); if (!isDepartmentOK) { if (isSubjectInDepartment) { logger.Info("Throwing exception teacher doesn't teach in department"); throw new TeacherDontTeachThisDepartment($"Teacher {teacher.UserName} doesn't teach to department {studentDepartment.Departments.DepartmentName}, {whoTeach} does."); } } logger.Info("Preparing teach for save in db"); Teach teachIn = new Teach() { StudentDepartments = studentDepartment, Teachers = teacher, Subject = subject }; logger.Info("Access to teaches repository for insert new teach"); db.TeachesRepository.Insert(teachIn); logger.Info("db save new teach"); db.Save(); logger.Info("Converting to dto for output"); TeachDTOOut teachDTOOut = new TeachDTOOut() { Id = teachIn.Id, Student = Utilities.ConverterDTO.SimpleDTOConverter <StudentDTO>(teachIn.StudentDepartments.Students), Department = Utilities.ConverterDTO.SimpleDTOConverter <DepartmentDTOStudent>(teachIn.StudentDepartments.Departments), TeacherSubject = studentDepartment.Teaches.Select(x => new TeacherSubjectDTO() { Teacher = Utilities.ConverterDTO.SimpleDTOConverter <TeacherDTO>(x.Teachers), Subject = Utilities.ConverterDTO.SimpleDTOConverter <SubjectDTO>(x.Subject) }) }; return(teachDTOOut); }