fix: integratie user + errors gefixt zodat het runt + format
This commit is contained in:
parent
6c4ea0eefb
commit
1b096b411b
55 changed files with 858 additions and 594 deletions
|
|
@ -1,7 +1,17 @@
|
|||
import { getAssignmentRepository, getClassRepository } from "../data/repositories.js";
|
||||
import { AssignmentDTO, mapToAssignmentDTO, mapToAssignmentDTOId } from "../interfaces/assignments.js";
|
||||
import {
|
||||
getAssignmentRepository,
|
||||
getClassRepository,
|
||||
} from '../data/repositories.js';
|
||||
import {
|
||||
AssignmentDTO,
|
||||
mapToAssignmentDTO,
|
||||
mapToAssignmentDTOId,
|
||||
} from '../interfaces/assignment.js';
|
||||
|
||||
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
|
||||
export async function getAllAssignments(
|
||||
classid: string,
|
||||
full: boolean
|
||||
): Promise<AssignmentDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
|
|
@ -10,7 +20,8 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
|
|||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
|
||||
const assignments =
|
||||
await assignmentRepository.findAllAssignmentsInClass(cls);
|
||||
|
||||
if (full) {
|
||||
return assignments.map(mapToAssignmentDTO);
|
||||
|
|
@ -19,7 +30,10 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
|
|||
return assignments.map(mapToAssignmentDTOId);
|
||||
}
|
||||
|
||||
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> {
|
||||
export async function getAssignment(
|
||||
classid: string,
|
||||
id: number
|
||||
): Promise<AssignmentDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
import { getClassRepository } from "../data/repositories";
|
||||
import { Class } from "../entities/classes/class.entity";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/class";
|
||||
import { mapToStudentDTO, StudentDTO } from "../interfaces/student";
|
||||
import {
|
||||
getClassRepository,
|
||||
getTeacherInvitationRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
|
||||
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
|
||||
import {
|
||||
mapToTeacherInvitationDTO,
|
||||
mapToTeacherInvitationDTOIds,
|
||||
TeacherInvitationDTO,
|
||||
} from '../interfaces/teacher-invitation.js';
|
||||
|
||||
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
export async function getAllClasses(
|
||||
full: boolean
|
||||
): Promise<ClassDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.find({}, { populate: ["students", "teachers"] });
|
||||
const classes = await classRepository.find(
|
||||
{},
|
||||
{ populate: ['students', 'teachers'] }
|
||||
);
|
||||
|
||||
if (!classes) {
|
||||
return [];
|
||||
|
|
@ -13,27 +25,30 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
|
|||
|
||||
if (full) {
|
||||
return classes.map(mapToClassDTO);
|
||||
} else {
|
||||
return classes.map((cls) => cls.classId);
|
||||
}
|
||||
return classes.map((cls) => {
|
||||
return cls.classId;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getClass(classId: string): Promise<ClassDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) return null;
|
||||
else {
|
||||
return mapToClassDTO(cls);
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
async function fetchClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
|
||||
async function fetchClassStudents(classId: string): Promise<StudentDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls)
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return cls.students.map(mapToStudentDTO);
|
||||
}
|
||||
|
|
@ -43,6 +58,36 @@ export async function getClassStudents(classId: string): Promise<StudentDTO[]> {
|
|||
}
|
||||
|
||||
export async function getClassStudentsIds(classId: string): Promise<string[]> {
|
||||
return await fetchClassStudents(classId).map((student) => student.username);
|
||||
const students: StudentDTO[] = await fetchClassStudents(classId);
|
||||
return students.map((student) => {
|
||||
return student.username;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getClassTeacherInvitations(
|
||||
classId: string,
|
||||
full: boolean
|
||||
): Promise<TeacherInvitationDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const invitations =
|
||||
await teacherInvitationRepository.findAllInvitationsForClass(cls);
|
||||
|
||||
console.log(invitations);
|
||||
|
||||
if (!invitations) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return invitations.map(mapToTeacherInvitationDTO);
|
||||
}
|
||||
|
||||
return invitations.map(mapToTeacherInvitationDTOIds);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,19 @@
|
|||
import { getAssignmentRepository, getClassRepository, getGroupRepository } from "../data/repositories.js";
|
||||
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from "../interfaces/groups.js";
|
||||
import {
|
||||
getAssignmentRepository,
|
||||
getClassRepository,
|
||||
getGroupRepository,
|
||||
} from '../data/repositories.js';
|
||||
import {
|
||||
GroupDTO,
|
||||
mapToGroupDTO,
|
||||
mapToGroupDTOId,
|
||||
} from '../interfaces/group.js';
|
||||
|
||||
export async function getGroup(
|
||||
classId: string,
|
||||
assignmentNumber: number,
|
||||
groupNumber: number,
|
||||
full: boolean,
|
||||
full: boolean
|
||||
): Promise<GroupDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
|
@ -15,14 +23,20 @@ export async function getGroup(
|
|||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
const assignment = await assignmentRepository.findByClassAndId(
|
||||
cls,
|
||||
assignmentNumber
|
||||
);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
const group = await groupRepository.findByAssignmentAndGroupNumber(
|
||||
assignment,
|
||||
groupNumber
|
||||
);
|
||||
|
||||
if (!group) {
|
||||
return null;
|
||||
|
|
@ -38,7 +52,7 @@ export async function getGroup(
|
|||
export async function getAllGroups(
|
||||
classId: string,
|
||||
assignmentNumber: number,
|
||||
full: boolean,
|
||||
full: boolean
|
||||
): Promise<GroupDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
|
@ -48,7 +62,10 @@ export async function getAllGroups(
|
|||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
const assignment = await assignmentRepository.findByClassAndId(
|
||||
cls,
|
||||
assignmentNumber
|
||||
);
|
||||
|
||||
if (!assignment) {
|
||||
return [];
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
import { getClassRepository, getStudentRepository } from "../data/repositories.js";
|
||||
import { Class } from "../entities/classes/class.entity.js";
|
||||
import { Student } from "../entities/users/student.entity.js";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes.js";
|
||||
import {UserService} from "./users.js";
|
||||
import {
|
||||
getClassRepository,
|
||||
getStudentRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { Student } from '../entities/users/student.entity.js';
|
||||
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
|
||||
import { UserService } from './users.js';
|
||||
|
||||
export class StudentService extends UserService<Student> {
|
||||
constructor() {
|
||||
|
|
@ -14,12 +17,16 @@ async function fetchStudentClasses(username: string): Promise<Class[]> {
|
|||
const studentRepository = getStudentRepository();
|
||||
const student = await studentRepository.findByUsername(username);
|
||||
|
||||
if (!student) return [];
|
||||
if (!student) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.findByStudent(student);
|
||||
|
||||
if (!classes) return [];
|
||||
if (!classes) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return classes;
|
||||
}
|
||||
|
|
@ -31,6 +38,7 @@ export async function getStudentClasses(username: string): Promise<ClassDTO[]> {
|
|||
|
||||
export async function getStudentClassIds(username: string): Promise<string[]> {
|
||||
const classes = await fetchStudentClasses(username);
|
||||
return classes.map(cls => cls.classId); // 'class' is a native keyword
|
||||
return classes.map((cls) => {
|
||||
return cls.classId;
|
||||
}); // 'class' is a native keyword
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,130 +2,108 @@ import {
|
|||
getClassRepository,
|
||||
getLearningObjectRepository,
|
||||
getQuestionRepository,
|
||||
getTeacherRepository
|
||||
} from "../data/repositories.js";
|
||||
import {mapToTeacher, mapToTeacherDTO, TeacherDTO} from "../interfaces/teacher.js";
|
||||
import { Teacher } from "../entities/users/teacher.entity";
|
||||
import {ClassDTO, mapToClassDTO} from "../interfaces/class";
|
||||
import {getClassStudents, getClassStudentsIds} from "./class";
|
||||
import {StudentDTO} from "../interfaces/student";
|
||||
import {mapToQuestionDTO, QuestionDTO, QuestionId} from "../interfaces/question";
|
||||
getStudentRepository,
|
||||
getTeacherRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { Teacher } from '../entities/users/teacher.entity.js';
|
||||
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
|
||||
import { getClassStudents } from './class.js';
|
||||
import { StudentDTO } from '../interfaces/student.js';
|
||||
import {
|
||||
mapToQuestionDTO,
|
||||
mapToQuestionId,
|
||||
QuestionDTO,
|
||||
QuestionId,
|
||||
} from '../interfaces/question.js';
|
||||
import { UserService } from './users.js';
|
||||
import { mapToUser } from '../interfaces/user.js';
|
||||
|
||||
|
||||
async function fetchAllTeachers(): Promise<TeacherDTO[]> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teachers = await teacherRepository.find({});
|
||||
|
||||
return teachers.map(mapToTeacherDTO);
|
||||
export class TeacherUserService extends UserService<Teacher> {
|
||||
constructor() {
|
||||
super(getTeacherRepository());
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllTeachers(): Promise<TeacherDTO[]> {
|
||||
return await fetchAllTeachers();
|
||||
}
|
||||
export class TeacherService {
|
||||
protected teacherService = new TeacherUserService();
|
||||
protected teacherRepository = getTeacherRepository();
|
||||
protected classRepository = getClassRepository();
|
||||
protected learningObjectRepository = getLearningObjectRepository();
|
||||
protected questionRepository = getQuestionRepository();
|
||||
|
||||
export async function getAllTeachersIds(): Promise<string[]> {
|
||||
return await fetchAllTeachers().map((teacher) => teacher.username)
|
||||
}
|
||||
async fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
|
||||
const teacher = await this.teacherRepository.findByUsername(username);
|
||||
if (!teacher) {
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function createTeacher(teacherData: TeacherDTO): Promise<Teacher> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const newTeacher = mapToTeacher(teacherData);
|
||||
|
||||
await teacherRepository.addTeacher(newTeacher);
|
||||
return newTeacher;
|
||||
}
|
||||
|
||||
export async function getTeacherByUsername(username: string): Promise<TeacherDTO | null> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teacher = await teacherRepository.findByUsername(username);
|
||||
|
||||
return teacher ? mapToTeacherDTO(teacher) : null;
|
||||
}
|
||||
|
||||
export async function deleteTeacher(username: string): Promise<TeacherDTO | null> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teacher = await teacherRepository.findByUsername(username);
|
||||
|
||||
if (!teacher)
|
||||
return null;
|
||||
|
||||
await teacherRepository.deleteByUsername(username);
|
||||
return teacher;
|
||||
}
|
||||
|
||||
async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const classRepository = getClassRepository();
|
||||
|
||||
const teacher = await teacherRepository.findByUsername(username);
|
||||
if (!teacher) {
|
||||
return [];
|
||||
const classes = await this.classRepository.findByTeacher(teacher);
|
||||
return classes.map(mapToClassDTO);
|
||||
}
|
||||
|
||||
const classes = await classRepository.findByTeacher(teacher);
|
||||
return classes.map(mapToClassDTO);
|
||||
}
|
||||
|
||||
export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> {
|
||||
return await fetchClassesByTeacher(username)
|
||||
}
|
||||
|
||||
export async function getClassIdsByTeacher(): Promise<string[]> {
|
||||
return await fetchClassesByTeacher(username).map((cls) => cls.id);
|
||||
}
|
||||
|
||||
async function fetchStudentsByTeacher(username: string) {
|
||||
const classes = await getClassIdsByTeacher();
|
||||
|
||||
return Promise.all(
|
||||
classes.map( async (id) => getClassStudents(id))
|
||||
);
|
||||
}
|
||||
|
||||
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
|
||||
return await fetchStudentsByTeacher(username);
|
||||
}
|
||||
|
||||
export async function getStudentIdsByTeacher(): Promise<string[]> {
|
||||
return await fetchStudentsByTeacher(username).map((student) => student.username);
|
||||
}
|
||||
|
||||
async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> {
|
||||
const learningObjectRepository = getLearningObjectRepository();
|
||||
const questionRepository = getQuestionRepository();
|
||||
|
||||
const teacher = getTeacherByUsername(username);
|
||||
if (!teacher) {
|
||||
throw new Error(`Teacher with username '${username}' not found.`);
|
||||
async getClassesByTeacher(username: string): Promise<ClassDTO[]> {
|
||||
return await this.fetchClassesByTeacher(username);
|
||||
}
|
||||
|
||||
// Find all learning objects that this teacher manages
|
||||
const learningObjects = await learningObjectRepository.findAllByTeacher(teacher);
|
||||
async getClassIdsByTeacher(username: string): Promise<string[]> {
|
||||
const classes = await this.fetchClassesByTeacher(username);
|
||||
return classes.map((cls) => {
|
||||
return cls.id;
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch all questions related to these learning objects
|
||||
const questions = await questionRepository.findAllByLearningObjects(learningObjects);
|
||||
async fetchStudentsByTeacher(username: string) {
|
||||
const classes = await this.getClassIdsByTeacher(username);
|
||||
|
||||
return questions.map(mapToQuestionDTO);
|
||||
return (
|
||||
await Promise.all(
|
||||
classes.map(async (id) => {
|
||||
return getClassStudents(id);
|
||||
})
|
||||
)
|
||||
).flat();
|
||||
}
|
||||
|
||||
async getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
|
||||
return await this.fetchStudentsByTeacher(username);
|
||||
}
|
||||
|
||||
async getStudentIdsByTeacher(username: string): Promise<string[]> {
|
||||
const students = await this.fetchStudentsByTeacher(username);
|
||||
return students.map((student) => {
|
||||
return student.username;
|
||||
});
|
||||
}
|
||||
|
||||
async fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> {
|
||||
const teacherDTO =
|
||||
await this.teacherService.getUserByUsername(username);
|
||||
if (!teacherDTO) {
|
||||
throw new Error(`Teacher with username '${username}' not found.`);
|
||||
}
|
||||
|
||||
const teacher = mapToUser<Teacher>(teacherDTO, new Teacher());
|
||||
|
||||
// Find all learning objects that this teacher manages
|
||||
const learningObjects =
|
||||
await this.learningObjectRepository.findAllByTeacher(teacher);
|
||||
|
||||
// Fetch all questions related to these learning objects
|
||||
const questions =
|
||||
await this.questionRepository.findAllByLearningObjects(
|
||||
learningObjects
|
||||
);
|
||||
|
||||
return questions.map(mapToQuestionDTO);
|
||||
}
|
||||
|
||||
async getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
|
||||
return await this.fetchTeacherQuestions(username);
|
||||
}
|
||||
|
||||
async getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
|
||||
const questions = await this.fetchTeacherQuestions(username);
|
||||
|
||||
return questions.map(mapToQuestionId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
|
||||
return await fetchTeacherQuestions(username);
|
||||
}
|
||||
|
||||
export async function getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
|
||||
const questions = await fetchTeacherQuestions(username);
|
||||
|
||||
return questions.map((question) => ({
|
||||
learningObjectHruid: question.learningObjectHruid,
|
||||
learningObjectLanguage: question.learningObjectLanguage,
|
||||
learningObjectVersion: question.learningObjectVersion,
|
||||
sequenceNumber: question.sequenceNumber
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { UserRepository } from "../data/users/user-repository.js";
|
||||
import { UserDTO, mapToUser, mapToUserDTO } from "../interfaces/user.js";
|
||||
import {User} from "../entities/users/user.entity.js";
|
||||
import { UserRepository } from '../data/users/user-repository.js';
|
||||
import { UserDTO, mapToUser, mapToUserDTO } from '../interfaces/user.js';
|
||||
import { User } from '../entities/users/user.entity.js';
|
||||
|
||||
export class UserService<T extends User> {
|
||||
protected repository: UserRepository<T>;
|
||||
|
|
@ -16,11 +16,13 @@ export class UserService<T extends User> {
|
|||
|
||||
async getAllUserIds(): Promise<string[]> {
|
||||
const users = await this.getAllUsers();
|
||||
return users.map((user) => user.username);
|
||||
return users.map((user) => {
|
||||
return user.username;
|
||||
});
|
||||
}
|
||||
|
||||
async getUserByUsername(username: string): Promise<UserDTO | null> {
|
||||
const user = await this.repository.findByUsername(username)
|
||||
const user = await this.repository.findByUsername(username);
|
||||
return user ? mapToUserDTO(user) : null;
|
||||
}
|
||||
|
||||
|
|
@ -32,8 +34,10 @@ export class UserService<T extends User> {
|
|||
|
||||
async deleteUser(username: string): Promise<UserDTO | null> {
|
||||
const user = await this.getUserByUsername(username);
|
||||
if (!user) return null;
|
||||
await this.repository.deleteByUsername(username)
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
await this.repository.deleteByUsername(username);
|
||||
return mapToUserDTO(user);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue