fix: types in teacher
This commit is contained in:
parent
c8cff2e820
commit
cf5b446a47
1 changed files with 47 additions and 37 deletions
|
@ -2,7 +2,7 @@ import {
|
|||
getClassJoinRequestRepository,
|
||||
getClassRepository,
|
||||
getLearningObjectRepository,
|
||||
getQuestionRepository, getStudentRepository,
|
||||
getQuestionRepository,
|
||||
getTeacherRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
|
||||
|
@ -12,12 +12,22 @@ import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher
|
|||
import {ConflictException, NotFoundException} from "../exceptions";
|
||||
import {Teacher} from "../entities/users/teacher.entity";
|
||||
import {fetchStudent} from "./students";
|
||||
import {ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity";
|
||||
import {mapToStudentRequestDTO} from "../interfaces/student-request";
|
||||
import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity";
|
||||
import {mapToStudentRequestDTO, StudentRequestDTO} from "../interfaces/student-request";
|
||||
import {TeacherRepository} from "../data/users/teacher-repository";
|
||||
import {ClassRepository} from "../data/classes/class-repository";
|
||||
import {Class} from "../entities/classes/class.entity";
|
||||
import {StudentDTO} from "../interfaces/student";
|
||||
import {LearningObjectRepository} from "../data/content/learning-object-repository";
|
||||
import {LearningObject} from "../entities/content/learning-object.entity";
|
||||
import {QuestionRepository} from "../data/questions/question-repository";
|
||||
import {Question} from "../entities/questions/question.entity";
|
||||
import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository";
|
||||
import {Student} from "../entities/users/student.entity";
|
||||
|
||||
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const users = await teacherRepository.findAll();
|
||||
const teacherRepository: TeacherRepository = getTeacherRepository();
|
||||
const users: Teacher[] = await teacherRepository.findAll();
|
||||
|
||||
if (full) {
|
||||
return users.map(mapToTeacherDTO);
|
||||
|
@ -26,8 +36,8 @@ export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | stri
|
|||
}
|
||||
|
||||
export async function fetchTeacher(username: string): Promise<Teacher> {
|
||||
const studentRepository = getStudentRepository();
|
||||
const user = await studentRepository.findByUsername(username);
|
||||
const teacherRepository: TeacherRepository = getTeacherRepository();
|
||||
const user: Teacher | null = await teacherRepository.findByUsername(username);
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException("Teacher with username not found");
|
||||
|
@ -36,26 +46,26 @@ export async function fetchTeacher(username: string): Promise<Teacher> {
|
|||
return user;
|
||||
}
|
||||
|
||||
export async function getTeacher(username: string): Promise<TeacherDTO | null> {
|
||||
const user = await fetchTeacher(username);
|
||||
export async function getTeacher(username: string): Promise<TeacherDTO> {
|
||||
const user: Teacher = await fetchTeacher(username);
|
||||
return mapToTeacherDTO(user);
|
||||
}
|
||||
|
||||
export async function createTeacher(userData: TeacherDTO): Promise<void> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teacherRepository: TeacherRepository = getTeacherRepository();
|
||||
|
||||
const user = await teacherRepository.findByUsername(userData.username);
|
||||
const user: Teacher | null = await teacherRepository.findByUsername(userData.username);
|
||||
|
||||
if (user){
|
||||
throw new ConflictException("Teacher with that username already exists");
|
||||
}
|
||||
|
||||
const newTeacher = teacherRepository.create(mapToTeacher(userData));
|
||||
const newTeacher: Teacher = teacherRepository.create(mapToTeacher(userData));
|
||||
await teacherRepository.save(newTeacher);
|
||||
}
|
||||
|
||||
export async function deleteTeacher(username: string): Promise<void> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teacherRepository: TeacherRepository = getTeacherRepository();
|
||||
|
||||
await fetchTeacher(username); // throws error if it does not exist
|
||||
|
||||
|
@ -63,15 +73,15 @@ export async function deleteTeacher(username: string): Promise<void> {
|
|||
}
|
||||
|
||||
async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
|
||||
const teacher = await fetchTeacher(username);
|
||||
const teacher: Teacher = await fetchTeacher(username);
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.findByTeacher(teacher);
|
||||
const classRepository: ClassRepository = getClassRepository();
|
||||
const classes: Class[] = await classRepository.findByTeacher(teacher);
|
||||
return classes.map(mapToClassDTO);
|
||||
}
|
||||
|
||||
export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
const classes = await fetchClassesByTeacher(username);
|
||||
const classes: ClassDTO[] = await fetchClassesByTeacher(username);
|
||||
|
||||
if (full) {
|
||||
return classes;
|
||||
|
@ -80,15 +90,15 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom
|
|||
}
|
||||
|
||||
export async function getStudentsByTeacher(username: string, full: boolean) {
|
||||
const classes = await fetchClassesByTeacher(username);
|
||||
const classes: ClassDTO[] = await fetchClassesByTeacher(username);
|
||||
|
||||
if (!classes || classes.length === 0){
|
||||
return [];
|
||||
}
|
||||
|
||||
const classIds = classes.map((cls) => cls.id);
|
||||
const classIds: string[] = classes.map((cls) => cls.id);
|
||||
|
||||
const students = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat();
|
||||
const students: StudentDTO[] = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat();
|
||||
if (full) {
|
||||
return students;
|
||||
}
|
||||
|
@ -96,20 +106,20 @@ export async function getStudentsByTeacher(username: string, full: boolean) {
|
|||
}
|
||||
|
||||
export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
|
||||
const teacher = await fetchTeacher(username);
|
||||
const teacher: Teacher = await fetchTeacher(username);
|
||||
|
||||
// Find all learning objects that this teacher manages
|
||||
const learningObjectRepository = getLearningObjectRepository();
|
||||
const learningObjects = await learningObjectRepository.findAllByTeacher(teacher);
|
||||
const learningObjectRepository: LearningObjectRepository = getLearningObjectRepository();
|
||||
const learningObjects: LearningObject[] = await learningObjectRepository.findAllByTeacher(teacher);
|
||||
|
||||
if (!learningObjects || learningObjects.length === 0){
|
||||
return [];
|
||||
}
|
||||
|
||||
// Fetch all questions related to these learning objects
|
||||
const questionRepository = getQuestionRepository();
|
||||
const questions = await questionRepository.findAllByLearningObjects(learningObjects);
|
||||
const questionsDTO = questions.map(mapToQuestionDTO);
|
||||
const questionRepository: QuestionRepository = getQuestionRepository();
|
||||
const questions: Question[] = await questionRepository.findAllByLearningObjects(learningObjects);
|
||||
const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO);
|
||||
|
||||
if (full) {
|
||||
return questionsDTO;
|
||||
|
@ -118,31 +128,31 @@ export async function getTeacherQuestions(username: string, full: boolean): Prom
|
|||
return questionsDTO.map(mapToQuestionId);
|
||||
}
|
||||
|
||||
export async function getJoinRequestsByClass( classId: string ){
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
export async function getJoinRequestsByClass( classId: string ): Promise<StudentRequestDTO[]> {
|
||||
const classRepository: ClassRepository = getClassRepository();
|
||||
const cls: Class | null = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
throw new NotFoundException("Class with id not found");
|
||||
}
|
||||
|
||||
const requestRepo = getClassJoinRequestRepository();
|
||||
const requests = await requestRepo.findAllOpenRequestsTo(cls);
|
||||
const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository();
|
||||
const requests: ClassJoinRequest[] = await requestRepo.findAllOpenRequestsTo(cls);
|
||||
return requests.map(mapToStudentRequestDTO);
|
||||
}
|
||||
|
||||
export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true) {
|
||||
const requestRepo = getClassJoinRequestRepository();
|
||||
const classRepo = getClassRepository();
|
||||
export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true): Promise<void> {
|
||||
const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository();
|
||||
const classRepo: ClassRepository = getClassRepository();
|
||||
|
||||
const student = await fetchStudent(studentUsername);
|
||||
const cls = await classRepo.findById(classId);
|
||||
const student: Student = await fetchStudent(studentUsername);
|
||||
const cls: Class | null = await classRepo.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
throw new NotFoundException('Class not found');
|
||||
}
|
||||
|
||||
const request = await requestRepo.findByStudentAndClass(student, cls);
|
||||
const request: ClassJoinRequest | null = await requestRepo.findByStudentAndClass(student, cls);
|
||||
|
||||
if (!request) {
|
||||
throw new NotFoundException('Join request not found');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue