style: fix linting issues met Prettier

This commit is contained in:
Lint Action 2025-03-13 17:45:32 +00:00
parent e78849f568
commit 400a955850
40 changed files with 321 additions and 700 deletions

View file

@ -1,5 +1,5 @@
import { EnvVars, getEnvVar } from './util/envvars.js';
import {Language} from "./entities/content/language.js";
import { Language } from './entities/content/language.js';
// API
export const DWENGO_API_BASE = getEnvVar(EnvVars.LearningContentRepoApiBaseUrl);

View file

@ -9,10 +9,7 @@ interface AssignmentParams {
id: string;
}
export async function getAllAssignmentsHandler(
req: Request<AssignmentParams>,
res: Response
): Promise<void> {
export async function getAllAssignmentsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid;
const full = req.query.full === 'true';
@ -23,18 +20,11 @@ export async function getAllAssignmentsHandler(
});
}
export async function createAssignmentHandler(
req: Request<AssignmentParams>,
res: Response,
): Promise<void> {
export async function createAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid;
const assignmentData = req.body as AssignmentDTO;
if (!assignmentData.description
|| !assignmentData.language
|| !assignmentData.learningPath
|| !assignmentData.title
) {
if (!assignmentData.description || !assignmentData.language || !assignmentData.learningPath || !assignmentData.title) {
res.status(400).json({
error: 'Missing one or more required fields: title, description, learningPath, title',
});
@ -44,17 +34,14 @@ export async function createAssignmentHandler(
const assignment = createAssignment(classid, assignmentData);
if (!assignment) {
res.status(500).json({ error: "Could not create assignment "});
res.status(500).json({ error: 'Could not create assignment ' });
return;
}
res.status(201).json({ assignment: assignment });
}
export async function getAssignmentHandler(
req: Request<AssignmentParams>,
res: Response
): Promise<void> {
export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const id = +req.params.id;
const classid = req.params.classid;
@ -73,10 +60,7 @@ export async function getAssignmentHandler(
res.json(assignment);
}
export async function getAssignmentsSubmissionsHandler(
req: Request<AssignmentParams>,
res: Response,
): Promise<void> {
export async function getAssignmentsSubmissionsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid;
const assignmentNumber = +req.params.id;

View file

@ -1,19 +1,9 @@
import { Request, Response } from 'express';
import {
createClass,
getAllClasses,
getClass,
getClassStudents,
getClassStudentsIds,
getClassTeacherInvitations,
} from '../services/class.js';
import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/class.js';
import { ClassDTO, mapToClass } from '../interfaces/class.js';
import { getClassRepository, getStudentRepository, getTeacherRepository } from '../data/repositories.js';
export async function getAllClassesHandler(
req: Request,
res: Response
): Promise<void> {
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
const classes = await getAllClasses(full);
@ -22,10 +12,7 @@ export async function getAllClassesHandler(
});
}
export async function createClassHandler(
req: Request,
res: Response,
): Promise<void> {
export async function createClassHandler(req: Request, res: Response): Promise<void> {
const classData = req.body as ClassDTO;
if (!classData.displayName) {
@ -38,17 +25,14 @@ export async function createClassHandler(
const cls = await createClass(classData);
if (!cls) {
res.status(500).json({ error: "Something went wrong while creating class" });
return
res.status(500).json({ error: 'Something went wrong while creating class' });
return;
}
res.status(201).json({ class: cls });
}
export async function getClassHandler(
req: Request,
res: Response
): Promise<void> {
export async function getClassHandler(req: Request, res: Response): Promise<void> {
try {
const classId = req.params.id;
const cls = await getClass(classId);
@ -71,26 +55,18 @@ export async function getClassHandler(
}
}
export async function getClassStudentsHandler(
req: Request,
res: Response
): Promise<void> {
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {
const classId = req.params.id;
const full = req.query.full === 'true';
const students = full
? await getClassStudents(classId)
: await getClassStudentsIds(classId);
const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId);
res.json({
students: students,
});
}
export async function getTeacherInvitationsHandler(
req: Request,
res: Response
): Promise<void> {
export async function getTeacherInvitationsHandler(req: Request, res: Response): Promise<void> {
const classId = req.params.id;
const full = req.query.full === 'true'; // TODO: not implemented yet

View file

@ -9,10 +9,7 @@ interface GroupParams {
groupid?: string;
}
export async function getGroupHandler(
req: Request<GroupParams>,
res: Response
): Promise<void> {
export async function getGroupHandler(req: Request<GroupParams>, res: Response): Promise<void> {
const classId = req.params.classid;
const full = req.query.full === 'true';
const assignmentId = +req.params.assignmentid;
@ -34,10 +31,7 @@ export async function getGroupHandler(
res.json(group);
}
export async function getAllGroupsHandler(
req: Request,
res: Response
): Promise<void> {
export async function getAllGroupsHandler(req: Request, res: Response): Promise<void> {
const classId = req.params.classid;
const full = req.query.full === 'true';
@ -55,10 +49,7 @@ export async function getAllGroupsHandler(
});
}
export async function createGroupHandler(
req: Request,
res: Response,
): Promise<void> {
export async function createGroupHandler(req: Request, res: Response): Promise<void> {
const classid = req.params.classid;
const assignmentId = +req.params.assignmentid;
@ -71,17 +62,14 @@ export async function createGroupHandler(
const group = createGroup(groupData, classid, assignmentId);
if (!group) {
res.status(500).json({ error: "Something went wrong while creating group" });
return
res.status(500).json({ error: 'Something went wrong while creating group' });
return;
}
res.status(201).json({ group: group });
}
export async function getGroupSubmissionsHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> {
const classId = req.params.classid;
// Const full = req.query.full === 'true';
@ -104,4 +92,4 @@ export async function getGroupSubmissionsHandler(
res.json({
submissions: submissions,
});
}
}

View file

@ -1,121 +1,119 @@
import {Request, Response} from "express";
import {
createQuestion,
deleteQuestion,
getAllQuestions,
getAnswersByQuestion,
getQuestion
} from "../services/questions.js";
import {QuestionDTO, QuestionId} from "../interfaces/question.js";
import {FALLBACK_LANG, FALLBACK_SEQ_NUM} from "../config.js";
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier.js";
import {Language} from "../entities/content/language.js";
import { Request, Response } from 'express';
import { createQuestion, deleteQuestion, getAllQuestions, getAnswersByQuestion, getQuestion } from '../services/questions.js';
import { QuestionDTO, QuestionId } from '../interfaces/question.js';
import { FALLBACK_LANG, FALLBACK_SEQ_NUM } from '../config.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { Language } from '../entities/content/language.js';
function getObjectId(req: Request, res: Response): LearningObjectIdentifier | null {
const { hruid, version } = req.params
const lang = req.query.lang
const { hruid, version } = req.params;
const lang = req.query.lang;
if (!hruid || !version ) {
res.status(400).json({ error: "Missing required parameters." });
if (!hruid || !version) {
res.status(400).json({ error: 'Missing required parameters.' });
return null;
}
return {
hruid,
language: lang as Language || FALLBACK_LANG,
version: +version
}
language: (lang as Language) || FALLBACK_LANG,
version: +version,
};
}
function getQuestionId(req: Request, res: Response): QuestionId | null {
const seq = req.params.seq
const learningObjectIdentifier = getObjectId(req,res);
const seq = req.params.seq;
const learningObjectIdentifier = getObjectId(req, res);
if (!learningObjectIdentifier)
{return null}
if (!learningObjectIdentifier) {
return null;
}
return {
learningObjectIdentifier,
sequenceNumber: seq ? Number(seq) : FALLBACK_SEQ_NUM
}
sequenceNumber: seq ? Number(seq) : FALLBACK_SEQ_NUM,
};
}
export async function getAllQuestionsHandler(
req: Request,
res: Response
): Promise<void> {
export async function getAllQuestionsHandler(req: Request, res: Response): Promise<void> {
const objectId = getObjectId(req, res);
const full = req.query.full === 'true';
if (!objectId)
{return}
if (!objectId) {
return;
}
const questions = await getAllQuestions(objectId, full);
if (!questions)
{res.status(404).json({ error: `Questions not found.` });}
else
{res.json(questions);}
if (!questions) {
res.status(404).json({ error: `Questions not found.` });
} else {
res.json(questions);
}
}
export async function getQuestionHandler(req: Request, res: Response): Promise<void> {
const questionId = getQuestionId(req, res);
if (!questionId)
{return}
if (!questionId) {
return;
}
const question = await getQuestion(questionId);
if (!question)
{res.status(404).json({ error: `Question not found.` });}
else
{res.json(question)}
if (!question) {
res.status(404).json({ error: `Question not found.` });
} else {
res.json(question);
}
}
export async function getQuestionAnswersHandler(req: Request, res: Response): Promise<void> {
const questionId = getQuestionId(req, res);
const full = req.query.full === 'true';
if (!questionId)
{return}
if (!questionId) {
return;
}
const answers = getAnswersByQuestion(questionId, full);
if (!answers)
{res.status(404).json({ error: `Questions not found.` });}
else
{res.json(answers)}
if (!answers) {
res.status(404).json({ error: `Questions not found.` });
} else {
res.json(answers);
}
}
export async function createQuestionHandler(req: Request, res: Response): Promise<void> {
const questionDTO = req.body as QuestionDTO;
if (!questionDTO.learningObjectIdentifier || !questionDTO.author || !questionDTO.content) {
res.status(400).json({error: 'Missing required fields: identifier and content'});
res.status(400).json({ error: 'Missing required fields: identifier and content' });
return;
}
const question = await createQuestion(questionDTO);
if (!question)
{res.status(400).json({error: 'Could not add question'});}
else
{res.json(question)}
if (!question) {
res.status(400).json({ error: 'Could not add question' });
} else {
res.json(question);
}
}
export async function deleteQuestionHandler(req: Request, res: Response): Promise<void> {
const questionId = getQuestionId(req, res);
if (!questionId)
{return}
if (!questionId) {
return;
}
const question = await deleteQuestion(questionId);
if (!question)
{res.status(400).json({error: 'Could not find nor delete question'});}
else
{res.json(question)}
if (!question) {
res.status(400).json({ error: 'Could not find nor delete question' });
} else {
res.json(question);
}
}

View file

@ -10,9 +10,7 @@ import {
} from '../services/students.js';
import { ClassDTO } from '../interfaces/class.js';
import { getAllAssignments } from '../services/assignments.js';
import {
getUserHandler,
} from './users.js';
import { getUserHandler } from './users.js';
import { Student } from '../entities/users/student.entity.js';
import { StudentDTO } from '../interfaces/student.js';
import { getStudentRepository } from '../data/repositories.js';
@ -20,17 +18,12 @@ import { UserDTO } from '../interfaces/user.js';
// TODO: accept arguments (full, ...)
// TODO: endpoints
export async function getAllStudentsHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getAllStudentsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
const studentRepository = getStudentRepository();
const students: StudentDTO[] | string[] = full
? await getAllStudents()
: await getAllStudents();
const students: StudentDTO[] | string[] = full ? await getAllStudents() : await getAllStudents();
if (!students) {
res.status(404).json({ error: `Student not found.` });
@ -40,11 +33,7 @@ export async function getAllStudentsHandler(
res.status(201).json(students);
}
export async function getStudentHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getStudentHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username;
if (!username) {
@ -64,10 +53,7 @@ export async function getStudentHandler(
res.status(201).json(user);
}
export async function createStudentHandler(
req: Request,
res: Response,
) {
export async function createStudentHandler(req: Request, res: Response) {
const userData = req.body as StudentDTO;
if (!userData.username || !userData.firstName || !userData.lastName) {
@ -81,10 +67,7 @@ export async function createStudentHandler(
res.status(201).json(newUser);
}
export async function deleteStudentHandler(
req: Request,
res: Response,
) {
export async function deleteStudentHandler(req: Request, res: Response) {
const username = req.params.username;
if (!username) {
@ -103,10 +86,7 @@ export async function deleteStudentHandler(
res.status(200).json(deletedUser);
}
export async function getStudentClassesHandler(
req: Request,
res: Response
): Promise<void> {
export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> {
try {
const full = req.query.full === 'true';
const username = req.params.id;
@ -132,10 +112,7 @@ export async function getStudentClassesHandler(
// Might not be fully correct depending on if
// A class has an assignment, that all students
// Have this assignment.
export async function getStudentAssignmentsHandler(
req: Request,
res: Response
): Promise<void> {
export async function getStudentAssignmentsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
const username = req.params.id;
@ -146,24 +123,18 @@ export async function getStudentAssignmentsHandler(
});
}
export async function getStudentGroupsHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getStudentGroupsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
const username = req.params.id;
const groups = await getStudentGroups(username, full);
res.json({
groups: groups,
});
}
export async function getStudentSubmissionsHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getStudentSubmissionsHandler(req: Request, res: Response): Promise<void> {
const username = req.params.id;
const submissions = await getStudentSubmissions(username);
@ -175,4 +146,3 @@ export async function getStudentSubmissionsHandler(
function getAllStudents(): StudentDTO[] | string[] | PromiseLike<StudentDTO[] | string[]> {
throw new Error('Function not implemented.');
}

View file

@ -1,17 +1,14 @@
import { Request, Response } from "express";
import {createSubmission, deleteSubmission, getSubmission} from "../services/submissions.js";
import { Language, languageMap } from "../entities/content/language.js";
import {SubmissionDTO} from "../interfaces/submission";
import { Request, Response } from 'express';
import { createSubmission, deleteSubmission, getSubmission } from '../services/submissions.js';
import { Language, languageMap } from '../entities/content/language.js';
import { SubmissionDTO } from '../interfaces/submission';
interface SubmissionParams {
hruid: string,
hruid: string;
id: number;
}
export async function getSubmissionHandler(
req: Request<SubmissionParams>,
res: Response,
): Promise<void> {
export async function getSubmissionHandler(req: Request<SubmissionParams>, res: Response): Promise<void> {
const lohruid = req.params.hruid;
const submissionNumber = +req.params.id;
@ -33,18 +30,19 @@ export async function getSubmissionHandler(
res.json(submission);
}
export async function createSubmissionHandler(req: Request, res: Response){
export async function createSubmissionHandler(req: Request, res: Response) {
const submissionDTO = req.body as SubmissionDTO;
const submission = await createSubmission(submissionDTO);
if (!submission)
{res.status(404).json({ error: 'Submission not added' });}
else
{res.json(submission)}
if (!submission) {
res.status(404).json({ error: 'Submission not added' });
} else {
res.json(submission);
}
}
export async function deleteSubmissionHandler(req: Request, res: Response){
export async function deleteSubmissionHandler(req: Request, res: Response) {
const hruid = req.params.hruid;
const submissionNumber = +req.params.id;
@ -53,8 +51,9 @@ export async function deleteSubmissionHandler(req: Request, res: Response){
const submission = await deleteSubmission(hruid, lang, version, submissionNumber);
if (!submission)
{res.status(404).json({ error: 'Submission not found' });}
else
{res.json(submission)}
if (!submission) {
res.status(404).json({ error: 'Submission not found' });
} else {
res.json(submission);
}
}

View file

@ -1,5 +1,16 @@
import { Request, Response } from 'express';
import { createTeacher, deleteTeacher, getAllTeachers, getClassesByTeacher, getClassIdsByTeacher, getQuestionIdsByTeacher, getQuestionsByTeacher, getStudentIdsByTeacher, getStudentsByTeacher, getTeacher } from '../services/teachers.js';
import {
createTeacher,
deleteTeacher,
getAllTeachers,
getClassesByTeacher,
getClassIdsByTeacher,
getQuestionIdsByTeacher,
getQuestionsByTeacher,
getStudentIdsByTeacher,
getStudentsByTeacher,
getTeacher,
} from '../services/teachers.js';
import { ClassDTO } from '../interfaces/class.js';
import { StudentDTO } from '../interfaces/student.js';
import { QuestionDTO, QuestionId } from '../interfaces/question.js';
@ -7,17 +18,12 @@ import { Teacher } from '../entities/users/teacher.entity.js';
import { TeacherDTO } from '../interfaces/teacher.js';
import { getTeacherRepository } from '../data/repositories.js';
export async function getAllTeachersHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getAllTeachersHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
const teacherRepository = getTeacherRepository();
const teachers: TeacherDTO[] | string[] = full
? await getAllTeachers()
: await getAllTeachers();
const teachers: TeacherDTO[] | string[] = full ? await getAllTeachers() : await getAllTeachers();
if (!teachers) {
res.status(404).json({ error: `Teacher not found.` });
@ -27,11 +33,7 @@ export async function getAllTeachersHandler(
res.status(201).json(teachers);
}
export async function getTeacherHandler(
req: Request,
res: Response,
): Promise<void> {
export async function getTeacherHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username;
if (!username) {
@ -51,10 +53,7 @@ export async function getTeacherHandler(
res.status(201).json(user);
}
export async function createTeacherHandler(
req: Request,
res: Response,
) {
export async function createTeacherHandler(req: Request, res: Response) {
const userData = req.body as TeacherDTO;
if (!userData.username || !userData.firstName || !userData.lastName) {
@ -68,10 +67,7 @@ export async function createTeacherHandler(
res.status(201).json(newUser);
}
export async function deleteTeacherHandler(
req: Request,
res: Response,
) {
export async function deleteTeacherHandler(req: Request, res: Response) {
const username = req.params.username;
if (!username) {
@ -90,11 +86,7 @@ export async function deleteTeacherHandler(
res.status(200).json(deletedUser);
}
export async function getTeacherClassHandler(
req: Request,
res: Response
): Promise<void> {
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
@ -104,9 +96,7 @@ export async function getTeacherClassHandler(
return;
}
const classes: ClassDTO[] | string[] = full
? await getClassesByTeacher(username)
: await getClassIdsByTeacher(username);
const classes: ClassDTO[] | string[] = full ? await getClassesByTeacher(username) : await getClassIdsByTeacher(username);
res.status(201).json(classes);
} catch (error) {
@ -115,10 +105,7 @@ export async function getTeacherClassHandler(
}
}
export async function getTeacherStudentHandler(
req: Request,
res: Response
): Promise<void> {
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
@ -128,9 +115,7 @@ export async function getTeacherStudentHandler(
return;
}
const students: StudentDTO[] | string[] = full
? await getStudentsByTeacher(username)
: await getStudentIdsByTeacher(username);
const students: StudentDTO[] | string[] = full ? await getStudentsByTeacher(username) : await getStudentIdsByTeacher(username);
res.status(201).json(students);
} catch (error) {
@ -139,10 +124,7 @@ export async function getTeacherStudentHandler(
}
}
export async function getTeacherQuestionHandler(
req: Request,
res: Response
): Promise<void> {
export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
@ -152,9 +134,7 @@ export async function getTeacherQuestionHandler(
return;
}
const questions: QuestionDTO[] | QuestionId[] = full
? await getQuestionsByTeacher(username)
: await getQuestionIdsByTeacher(username);
const questions: QuestionDTO[] | QuestionId[] = full ? await getQuestionsByTeacher(username) : await getQuestionIdsByTeacher(username);
res.status(201).json(questions);
} catch (error) {

View file

@ -3,17 +3,11 @@ import { UserService } from '../services/users.js';
import { UserDTO } from '../interfaces/user.js';
import { User } from '../entities/users/user.entity.js';
export async function getAllUsersHandler<T extends User>(
req: Request,
res: Response,
service: UserService<T>
): Promise<void> {
export async function getAllUsersHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
try {
const full = req.query.full === 'true';
const users: UserDTO[] | string[] = full
? await service.getAllUsers()
: await service.getAllUserIds();
const users: UserDTO[] | string[] = full ? await service.getAllUsers() : await service.getAllUserIds();
if (!users) {
res.status(404).json({ error: `Users not found.` });
@ -27,11 +21,7 @@ export async function getAllUsersHandler<T extends User>(
}
}
export async function getUserHandler<T extends User>(
req: Request,
res: Response,
service: UserService<T>
): Promise<void> {
export async function getUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
try {
const username = req.params.username as string;
@ -56,12 +46,7 @@ export async function getUserHandler<T extends User>(
}
}
export async function createUserHandler<T extends User>(
req: Request,
res: Response,
service: UserService<T>,
UserClass: new () => T
) {
export async function createUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>, UserClass: new () => T) {
try {
console.log('req', req);
const userData = req.body as UserDTO;
@ -81,11 +66,7 @@ export async function createUserHandler<T extends User>(
}
}
export async function deleteUserHandler<T extends User>(
req: Request,
res: Response,
service: UserService<T>
) {
export async function deleteUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>) {
try {
const username = req.params.username;

View file

@ -4,10 +4,7 @@ import { Assignment } from '../../entities/assignments/assignment.entity.js';
import { Student } from '../../entities/users/student.entity.js';
export class GroupRepository extends DwengoEntityRepository<Group> {
public findByAssignmentAndGroupNumber(
assignment: Assignment,
groupNumber: number
): Promise<Group | null> {
public findByAssignmentAndGroupNumber(assignment: Assignment, groupNumber: number): Promise<Group | null> {
return this.findOne(
{
assignment: assignment,
@ -16,26 +13,16 @@ export class GroupRepository extends DwengoEntityRepository<Group> {
{ populate: ['members'] }
);
}
public findAllGroupsForAssignment(
assignment: Assignment
): Promise<Group[]> {
public findAllGroupsForAssignment(assignment: Assignment): Promise<Group[]> {
return this.findAll({
where: { assignment: assignment },
populate: ['members'],
});
}
public findAllGroupsWithStudent(
student: Student
): Promise<Group[]> {
return this.find(
{ members: student },
{ populate: ['members'] }
)
public findAllGroupsWithStudent(student: Student): Promise<Group[]> {
return this.find({ members: student }, { populate: ['members'] });
}
public deleteByAssignmentAndGroupNumber(
assignment: Assignment,
groupNumber: number
) {
public deleteByAssignmentAndGroupNumber(assignment: Assignment, groupNumber: number) {
return this.deleteWhere({
assignment: assignment,
groupNumber: groupNumber,

View file

@ -5,10 +5,7 @@ import { LearningObjectIdentifier } from '../../entities/content/learning-object
import { Student } from '../../entities/users/student.entity.js';
export class SubmissionRepository extends DwengoEntityRepository<Submission> {
public findSubmissionByLearningObjectAndSubmissionNumber(
loId: LearningObjectIdentifier,
submissionNumber: number
): Promise<Submission | null> {
public findSubmissionByLearningObjectAndSubmissionNumber(loId: LearningObjectIdentifier, submissionNumber: number): Promise<Submission | null> {
return this.findOne({
learningObjectHruid: loId.hruid,
learningObjectLanguage: loId.language,
@ -17,10 +14,7 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> {
});
}
public findMostRecentSubmissionForStudent(
loId: LearningObjectIdentifier,
submitter: Student
): Promise<Submission | null> {
public findMostRecentSubmissionForStudent(loId: LearningObjectIdentifier, submitter: Student): Promise<Submission | null> {
return this.findOne(
{
learningObjectHruid: loId.hruid,
@ -32,10 +26,7 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> {
);
}
public findMostRecentSubmissionForGroup(
loId: LearningObjectIdentifier,
group: Group
): Promise<Submission | null> {
public findMostRecentSubmissionForGroup(loId: LearningObjectIdentifier, group: Group): Promise<Submission | null> {
return this.findOne(
{
learningObjectHruid: loId.hruid,
@ -47,26 +38,15 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> {
);
}
public findAllSubmissionsForGroup(
group: Group,
): Promise<Submission[]> {
return this.find(
{ onBehalfOf: group },
);
public findAllSubmissionsForGroup(group: Group): Promise<Submission[]> {
return this.find({ onBehalfOf: group });
}
public findAllSubmissionsForStudent(
student: Student,
): Promise<Submission[]> {
return this.find(
{ submitter: student },
);
public findAllSubmissionsForStudent(student: Student): Promise<Submission[]> {
return this.find({ submitter: student });
}
public deleteSubmissionByLearningObjectAndSubmissionNumber(
loId: LearningObjectIdentifier,
submissionNumber: number
): Promise<void> {
public deleteSubmissionByLearningObjectAndSubmissionNumber(loId: LearningObjectIdentifier, submissionNumber: number): Promise<void> {
return this.deleteWhere({
learningObjectHruid: loId.hruid,
learningObjectLanguage: loId.language,

View file

@ -5,10 +5,7 @@ import { Teacher } from '../../entities/users/teacher.entity';
export class ClassRepository extends DwengoEntityRepository<Class> {
public findById(id: string): Promise<Class | null> {
return this.findOne(
{ classId: id },
{ populate: ['students', 'teachers'] }
);
return this.findOne({ classId: id }, { populate: ['students', 'teachers'] });
}
public deleteById(id: string): Promise<void> {
return this.deleteWhere({ classId: id });
@ -21,9 +18,6 @@ export class ClassRepository extends DwengoEntityRepository<Class> {
}
public findByTeacher(teacher: Teacher): Promise<Class[]> {
return this.find(
{ teachers: teacher },
{ populate: ['students', 'teachers'] }
);
return this.find({ teachers: teacher }, { populate: ['students', 'teachers'] });
}
}

View file

@ -19,10 +19,7 @@ export class AnswerRepository extends DwengoEntityRepository<Answer> {
orderBy: { sequenceNumber: 'ASC' },
});
}
public removeAnswerByQuestionAndSequenceNumber(
question: Question,
sequenceNumber: number
): Promise<void> {
public removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise<void> {
return this.deleteWhere({
toQuestion: question,
sequenceNumber: sequenceNumber,

View file

@ -21,9 +21,7 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
questionEntity.content = question.content;
return this.insert(questionEntity);
}
public findAllQuestionsAboutLearningObject(
loId: LearningObjectIdentifier
): Promise<Question[]> {
public findAllQuestionsAboutLearningObject(loId: LearningObjectIdentifier): Promise<Question[]> {
return this.findAll({
where: {
learningObjectHruid: loId.hruid,
@ -35,10 +33,7 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
},
});
}
public removeQuestionByLearningObjectAndSequenceNumber(
loId: LearningObjectIdentifier,
sequenceNumber: number
): Promise<void> {
public removeQuestionByLearningObjectAndSequenceNumber(loId: LearningObjectIdentifier, sequenceNumber: number): Promise<void> {
return this.deleteWhere({
learningObjectHruid: loId.hruid,
learningObjectLanguage: loId.language,
@ -47,14 +42,12 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
});
}
public async findAllByLearningObjects(
learningObjects: LearningObject[]
): Promise<Question[]> {
public async findAllByLearningObjects(learningObjects: LearningObject[]): Promise<Question[]> {
const objectIdentifiers = learningObjects.map((lo) => ({
learningObjectHruid: lo.hruid,
learningObjectLanguage: lo.language,
learningObjectVersion: lo.version,
}));
learningObjectHruid: lo.hruid,
learningObjectLanguage: lo.language,
learningObjectVersion: lo.version,
}));
return this.findAll({
where: { $or: objectIdentifiers },

View file

@ -40,9 +40,7 @@ export function transactional<T>(f: () => Promise<T>) {
entityManager?.transactional(f);
}
function repositoryGetter<T extends AnyEntity, R extends EntityRepository<T>>(
entity: EntityName<T>
): () => R {
function repositoryGetter<T extends AnyEntity, R extends EntityRepository<T>>(entity: EntityName<T>): () => R {
let cachedRepo: R | undefined;
return (): R => {
if (!cachedRepo) {

View file

@ -12,4 +12,4 @@ export class StudentRepository extends DwengoEntityRepository<Student> {
public deleteByUsername(username: string): Promise<void> {
return this.deleteWhere({ username: username });
}
}
}

View file

@ -9,4 +9,4 @@ export class TeacherRepository extends DwengoEntityRepository<Teacher> {
public deleteByUsername(username: string): Promise<void> {
return this.deleteWhere({ username: username });
}
}
}

View file

@ -1,12 +1,4 @@
import {
Collection,
Entity,
Enum,
ManyToOne,
OneToMany,
PrimaryKey,
Property,
} from '@mikro-orm/core';
import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Class } from '../classes/class.entity.js';
import { Group } from './group.entity.js';
import { Language } from '../content/language.js';

View file

@ -1,10 +1,4 @@
import {
Collection,
Entity,
ManyToMany,
PrimaryKey,
Property,
} from '@mikro-orm/core';
import { Collection, Entity, ManyToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { v4 } from 'uuid';
import { Teacher } from '../users/teacher.entity.js';
import { Student } from '../users/student.entity.js';

View file

@ -190,4 +190,4 @@ export const languageMap: Record<string, Language> = {
fr: Language.French,
en: Language.English,
de: Language.German,
};
};

View file

@ -1,13 +1,4 @@
import {
Embeddable,
Embedded,
Entity,
Enum,
ManyToMany,
OneToMany,
PrimaryKey,
Property,
} from '@mikro-orm/core';
import { Embeddable, Embedded, Entity, Enum, ManyToMany, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Language } from './language.js';
import { Attachment } from './attachment.entity.js';
import { Teacher } from '../users/teacher.entity.js';

View file

@ -1,6 +1,6 @@
import {mapToUserDTO, UserDTO} from "./user.js";
import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from "./question.js";
import {Answer} from "../entities/questions/answer.entity.js";
import { mapToUserDTO, UserDTO } from './user.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from './question.js';
import { Answer } from '../entities/questions/answer.entity.js';
export interface AnswerDTO {
author: UserDTO;
@ -33,6 +33,6 @@ export function mapToAnswerId(answer: AnswerDTO): AnswerId {
return {
author: answer.author.username,
toQuestion: mapToQuestionId(answer.toQuestion),
sequenceNumber: answer.sequenceNumber
}
sequenceNumber: answer.sequenceNumber,
};
}

View file

@ -27,15 +27,11 @@ export function mapToClassDTO(cls: Class): ClassDTO {
};
}
export function mapToClass(
classData: ClassDTO,
students: Collection<Student>,
teachers: Collection<Teacher>
): Class {
export function mapToClass(classData: ClassDTO, students: Collection<Student>, teachers: Collection<Teacher>): Class {
const cls = new Class();
cls.displayName = classData.displayName;
cls.students = students;
cls.teachers = teachers;
return cls;
}

View file

@ -1,6 +1,6 @@
import { Question } from '../entities/questions/question.entity.js';
import {UserDTO} from "./user.js";
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier.js";
import { UserDTO } from './user.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { mapToStudentDTO, StudentDTO } from './student.js';
import { TeacherDTO } from './teacher.js';
@ -19,8 +19,8 @@ export function mapToQuestionDTO(question: Question): QuestionDTO {
const learningObjectIdentifier = {
hruid: question.learningObjectHruid,
language: question.learningObjectLanguage,
version: question.learningObjectVersion
}
version: question.learningObjectVersion,
};
return {
learningObjectIdentifier,
@ -42,4 +42,3 @@ export function mapToQuestionId(question: QuestionDTO): QuestionId {
sequenceNumber: question.sequenceNumber!,
};
}

View file

@ -23,11 +23,7 @@ export function mapToStudentDTO(student: Student): StudentDTO {
}
export function mapToStudent(studentData: StudentDTO): Student {
const student = new Student(
studentData.username,
studentData.firstName,
studentData.lastName,
);
const student = new Student(studentData.username, studentData.firstName, studentData.lastName);
return student;
}

View file

@ -1,20 +1,20 @@
import { Submission } from "../entities/assignments/submission.entity.js";
import { Language } from "../entities/content/language.js";
import { GroupDTO, mapToGroupDTO } from "./group.js";
import {mapToStudent, mapToStudentDTO, StudentDTO} from "./student.js";
import {mapToUser} from "./user";
import {Student} from "../entities/users/student.entity";
import { Submission } from '../entities/assignments/submission.entity.js';
import { Language } from '../entities/content/language.js';
import { GroupDTO, mapToGroupDTO } from './group.js';
import { mapToStudent, mapToStudentDTO, StudentDTO } from './student.js';
import { mapToUser } from './user';
import { Student } from '../entities/users/student.entity';
export interface SubmissionDTO {
learningObjectHruid: string,
learningObjectLanguage: Language,
learningObjectVersion: number,
learningObjectHruid: string;
learningObjectLanguage: Language;
learningObjectVersion: number;
submissionNumber?: number,
submitter: StudentDTO,
time?: Date,
group?: GroupDTO,
content: string,
submissionNumber?: number;
submitter: StudentDTO;
time?: Date;
group?: GroupDTO;
content: string;
}
export function mapToSubmissionDTO(submission: Submission): SubmissionDTO {
@ -28,7 +28,7 @@ export function mapToSubmissionDTO(submission: Submission): SubmissionDTO {
time: submission.submissionTime,
group: submission.onBehalfOf ? mapToGroupDTO(submission.onBehalfOf) : undefined,
content: submission.content,
}
};
}
export function mapToSubmission(submissionDTO: SubmissionDTO): Submission {
@ -37,7 +37,7 @@ export function mapToSubmission(submissionDTO: SubmissionDTO): Submission {
submission.learningObjectLanguage = submissionDTO.learningObjectLanguage;
submission.learningObjectVersion = submissionDTO.learningObjectVersion;
// Submission.submissionNumber = submissionDTO.submissionNumber;
submission.submitter = mapToStudent(submissionDTO.submitter) ;
submission.submitter = mapToStudent(submissionDTO.submitter);
// Submission.submissionTime = submissionDTO.time;
// Submission.onBehalfOf = submissionDTO.group!;
// TODO fix group

View file

@ -8,9 +8,7 @@ export interface TeacherInvitationDTO {
class: string | ClassDTO;
}
export function mapToTeacherInvitationDTO(
invitation: TeacherInvitation
): TeacherInvitationDTO {
export function mapToTeacherInvitationDTO(invitation: TeacherInvitation): TeacherInvitationDTO {
return {
sender: mapToUserDTO(invitation.sender),
receiver: mapToUserDTO(invitation.receiver),
@ -18,9 +16,7 @@ export function mapToTeacherInvitationDTO(
};
}
export function mapToTeacherInvitationDTOIds(
invitation: TeacherInvitation
): TeacherInvitationDTO {
export function mapToTeacherInvitationDTOIds(invitation: TeacherInvitation): TeacherInvitationDTO {
return {
sender: invitation.sender.username,
receiver: invitation.receiver.username,

View file

@ -23,11 +23,7 @@ export function mapToTeacherDTO(teacher: Teacher): TeacherDTO {
}
export function mapToTeacher(TeacherData: TeacherDTO): Teacher {
const teacher = new Teacher(
TeacherData.username,
TeacherData.firstName,
TeacherData.lastName,
);
const teacher = new Teacher(TeacherData.username, TeacherData.firstName, TeacherData.lastName);
return teacher;
}

View file

@ -22,10 +22,7 @@ export function mapToUserDTO(user: User): UserDTO {
};
}
export function mapToUser<T extends User>(
userData: UserDTO,
userInstance: T
): T {
export function mapToUser<T extends User>(userData: UserDTO, userInstance: T): T {
userInstance.username = userData.username;
userInstance.firstName = userData.firstName;
userInstance.lastName = userData.lastName;

View file

@ -1,15 +1,9 @@
import express from 'express';
import {
getAllLearningObjects,
getAttachment,
getLearningObject,
getLearningObjectHTML,
} from '../controllers/learning-objects.js';
import { getAllLearningObjects, getAttachment, getLearningObject, getLearningObjectHTML } from '../controllers/learning-objects.js';
import submissionRoutes from './submissions.js';
import questionRoutes from './questions.js';
const router = express.Router();
// DWENGO learning objects
@ -32,7 +26,7 @@ router.get('/:hruid', getLearningObject);
router.use('/:hruid/submissions', submissionRoutes);
router.use('/:hruid/:version/questions', questionRoutes)
router.use('/:hruid/:version/questions', questionRoutes);
// Parameter: hruid of learning object
// Query: language, version (optional)

View file

@ -1,10 +1,11 @@
import express from 'express';
import {
createQuestionHandler, deleteQuestionHandler,
createQuestionHandler,
deleteQuestionHandler,
getAllQuestionsHandler,
getQuestionAnswersHandler,
getQuestionHandler
} from "../controllers/questions.js";
getQuestionHandler,
} from '../controllers/questions.js';
const router = express.Router({ mergeParams: true });
// Query language

View file

@ -1,9 +1,7 @@
import express from 'express';
import {createSubmissionHandler, deleteSubmissionHandler, getSubmissionHandler} from '../controllers/submissions.js';
import { createSubmissionHandler, deleteSubmissionHandler, getSubmissionHandler } from '../controllers/submissions.js';
const router = express.Router({ mergeParams: true });
// Root endpoint used to search objects
router.get('/', (req, res) => {
res.json({

View file

@ -1,22 +1,9 @@
import {
getAssignmentRepository,
getClassRepository,
getGroupRepository,
getSubmissionRepository,
} from '../data/repositories.js';
import { getAssignmentRepository, getClassRepository, getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
import { Assignment } from '../entities/assignments/assignment.entity.js';
import {
AssignmentDTO,
mapToAssignment,
mapToAssignmentDTO,
mapToAssignmentDTOId,
} from '../interfaces/assignment.js';
import { AssignmentDTO, mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js';
import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.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);
@ -25,8 +12,7 @@ export async function getAllAssignments(
}
const assignmentRepository = getAssignmentRepository();
const assignments =
await assignmentRepository.findAllAssignmentsInClass(cls);
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
if (full) {
return assignments.map(mapToAssignmentDTO);
@ -35,14 +21,11 @@ export async function getAllAssignments(
return assignments.map(mapToAssignmentDTOId);
}
export async function createAssignment(
classid: string,
assignmentData: AssignmentDTO,
): Promise<Assignment | null> {
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<Assignment | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classid);
if (!cls) {
if (!cls) {
return null;
}
@ -54,16 +37,12 @@ export async function createAssignment(
await assignmentRepository.save(newAssignment);
return newAssignment;
} catch(e) {
} catch (e) {
return null;
}
}
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);
@ -81,10 +60,7 @@ export async function getAssignment(
return mapToAssignmentDTO(assignment);
}
export async function getAssignmentsSubmissions(
classid: string,
assignmentNumber: number,
): Promise<SubmissionDTO[]> {
export async function getAssignmentsSubmissions(classid: string, assignmentNumber: number): Promise<SubmissionDTO[]> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classid);
@ -103,8 +79,7 @@ export async function getAssignmentsSubmissions(
const groups = await groupRepository.findAllGroupsForAssignment(assignment);
const submissionRepository = getSubmissionRepository();
const submissions =
(await Promise.all(groups.map(group => submissionRepository.findAllSubmissionsForGroup(group)))).flat();
const submissions = (await Promise.all(groups.map((group) => submissionRepository.findAllSubmissionsForGroup(group)))).flat();
return submissions.map(mapToSubmissionDTO);
}
}

View file

@ -1,26 +1,12 @@
import {
getClassRepository,
getStudentRepository,
getTeacherInvitationRepository,
getTeacherRepository,
} from '../data/repositories.js';
import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js';
import { Class } from '../entities/classes/class.entity.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import {
mapToTeacherInvitationDTO,
mapToTeacherInvitationDTOIds,
TeacherInvitationDTO,
} from '../interfaces/teacher-invitation.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 [];
@ -35,13 +21,11 @@ export async function getAllClasses(
export async function createClass(classData: ClassDTO): Promise<Class | null> {
const teacherRepository = getTeacherRepository();
const teacherUsernames = classData.teachers || [];
const teachers = (await Promise.all(teacherUsernames.map(id => teacherRepository.findByUsername(id))))
.filter(teacher => teacher != null);
const teachers = (await Promise.all(teacherUsernames.map((id) => teacherRepository.findByUsername(id)))).filter((teacher) => teacher != null);
const studentRepository = getStudentRepository();
const studentUsernames = classData.students || [];
const students = (await Promise.all(studentUsernames.map(id => studentRepository.findByUsername(id))))
.filter(student => student != null);
const students = (await Promise.all(studentUsernames.map((id) => studentRepository.findByUsername(id)))).filter((student) => student != null);
//Const cls = mapToClass(classData, teachers, students);
@ -56,7 +40,7 @@ export async function createClass(classData: ClassDTO): Promise<Class | null> {
await classRepository.save(newClass);
return newClass;
} catch(e) {
} catch (e) {
return null;
}
}
@ -92,10 +76,7 @@ export async function getClassStudentsIds(classId: string): Promise<string[]> {
return students.map((student) => student.username);
}
export async function getClassTeacherInvitations(
classId: string,
full: boolean
): Promise<TeacherInvitationDTO[]> {
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
@ -104,8 +85,7 @@ export async function getClassTeacherInvitations(
}
const teacherInvitationRepository = getTeacherInvitationRepository();
const invitations =
await teacherInvitationRepository.findAllInvitationsForClass(cls);
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls);
if (full) {
return invitations.map(mapToTeacherInvitationDTO);

View file

@ -7,19 +7,10 @@ import {
getSubmissionRepository,
} from '../data/repositories.js';
import { Group } from '../entities/assignments/group.entity.js';
import {
GroupDTO,
mapToGroupDTO,
mapToGroupDTOId,
} from '../interfaces/group.js';
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js';
export async function getGroup(
classId: string,
assignmentNumber: number,
groupNumber: number,
full: boolean
): Promise<GroupDTO | null> {
export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number, full: boolean): Promise<GroupDTO | null> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
@ -28,20 +19,14 @@ 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;
@ -54,16 +39,11 @@ export async function getGroup(
return mapToGroupDTOId(group);
}
export async function createGroup(
groupData: GroupDTO,
classid: string,
assignmentNumber: number,
): Promise<Group | null> {
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<Group | null> {
const studentRepository = getStudentRepository();
const memberUsernames = groupData.members as string[] || []; // TODO check if groupdata.members is a list
const members = (await Promise.all([...memberUsernames].map(id => studentRepository.findByUsername(id))))
.filter(student => student != null);
const memberUsernames = (groupData.members as string[]) || []; // TODO check if groupdata.members is a list
const members = (await Promise.all([...memberUsernames].map((id) => studentRepository.findByUsername(id)))).filter((student) => student != null);
console.log(members);
@ -90,17 +70,13 @@ export async function createGroup(
await groupRepository.save(newGroup);
return newGroup;
} catch(e) {
} catch (e) {
console.log(e);
return null;
}
}
export async function getAllGroups(
classId: string,
assignmentNumber: number,
full: boolean
): Promise<GroupDTO[]> {
export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[]> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
@ -109,10 +85,7 @@ export async function getAllGroups(
}
const assignmentRepository = getAssignmentRepository();
const assignment = await assignmentRepository.findByClassAndId(
cls,
assignmentNumber
);
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
if (!assignment) {
return [];
@ -130,11 +103,7 @@ export async function getAllGroups(
return groups.map(mapToGroupDTOId);
}
export async function getGroupSubmissions(
classId: string,
assignmentNumber: number,
groupNumber: number,
): Promise<SubmissionDTO[]> {
export async function getGroupSubmissions(classId: string, assignmentNumber: number, groupNumber: number): Promise<SubmissionDTO[]> {
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
@ -143,20 +112,14 @@ export async function getGroupSubmissions(
}
const assignmentRepository = getAssignmentRepository();
const assignment = await assignmentRepository.findByClassAndId(
cls,
assignmentNumber
);
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
if (!assignment) {
return [];
}
const groupRepository = getGroupRepository();
const group = await groupRepository.findByAssignmentAndGroupNumber(
assignment,
groupNumber
);
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber);
if (!group) {
return [];

View file

@ -1,16 +1,8 @@
import { DWENGO_API_BASE } from '../config.js';
import { fetchWithLogging } from '../util/api-helper.js';
import {
FilteredLearningObject,
LearningObjectMetadata,
LearningObjectNode,
LearningPathResponse,
} from '../interfaces/learning-content.js';
import { FilteredLearningObject, LearningObjectMetadata, LearningObjectNode, LearningPathResponse } from '../interfaces/learning-content.js';
function filterData(
data: LearningObjectMetadata,
htmlUrl: string
): FilteredLearningObject {
function filterData(data: LearningObjectMetadata, htmlUrl: string): FilteredLearningObject {
return {
key: data.hruid, // Hruid learningObject (not path)
_id: data._id,
@ -37,10 +29,7 @@ function filterData(
/**
* Fetches a single learning object by its HRUID
*/
export async function getLearningObjectById(
hruid: string,
language: string
): Promise<FilteredLearningObject | null> {
export async function getLearningObjectById(hruid: string, language: string): Promise<FilteredLearningObject | null> {
const metadataUrl = `${DWENGO_API_BASE}/learningObject/getMetadata?hruid=${hruid}&language=${language}`;
const metadata = await fetchWithLogging<LearningObjectMetadata>(
metadataUrl,
@ -59,26 +48,12 @@ export async function getLearningObjectById(
/**
* Generic function to fetch learning objects (full data or just HRUIDs)
*/
async function fetchLearningObjects(
hruid: string,
full: boolean,
language: string
): Promise<FilteredLearningObject[] | string[]> {
async function fetchLearningObjects(hruid: string, full: boolean, language: string): Promise<FilteredLearningObject[] | string[]> {
try {
const learningPathResponse: LearningPathResponse =
await fetchLearningPaths(
[hruid],
language,
`Learning path for HRUID "${hruid}"`
);
const learningPathResponse: LearningPathResponse = await fetchLearningPaths([hruid], language, `Learning path for HRUID "${hruid}"`);
if (
!learningPathResponse.success ||
!learningPathResponse.data?.length
) {
console.error(
`⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.`
);
if (!learningPathResponse.success || !learningPathResponse.data?.length) {
console.error(`⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.`);
return [];
}
@ -88,12 +63,9 @@ async function fetchLearningObjects(
return nodes.map((node) => node.learningobject_hruid);
}
return await Promise.all(
nodes.map(async (node) => getLearningObjectById(
node.learningobject_hruid,
language
))
).then((objects) => objects.filter((obj): obj is FilteredLearningObject => obj !== null));
return await Promise.all(nodes.map(async (node) => getLearningObjectById(node.learningobject_hruid, language))).then((objects) =>
objects.filter((obj): obj is FilteredLearningObject => obj !== null)
);
} catch (error) {
console.error('❌ Error fetching learning objects:', error);
return [];
@ -103,27 +75,16 @@ async function fetchLearningObjects(
/**
* Fetch full learning object data (metadata)
*/
export async function getLearningObjectsFromPath(
hruid: string,
language: string
): Promise<FilteredLearningObject[]> {
return (await fetchLearningObjects(
hruid,
true,
language
)) as FilteredLearningObject[];
export async function getLearningObjectsFromPath(hruid: string, language: string): Promise<FilteredLearningObject[]> {
return (await fetchLearningObjects(hruid, true, language)) as FilteredLearningObject[];
}
/**
* Fetch only learning object HRUIDs
*/
export async function getLearningObjectIdsFromPath(
hruid: string,
language: string
): Promise<string[]> {
export async function getLearningObjectIdsFromPath(hruid: string, language: string): Promise<string[]> {
return (await fetchLearningObjects(hruid, false, language)) as string[];
}
function fetchLearningPaths(arg0: string[], language: string, arg2: string): LearningPathResponse | PromiseLike<LearningPathResponse> {
throw new Error('Function not implemented.');
}

View file

@ -1,17 +1,15 @@
import {getAnswerRepository, getQuestionRepository} from "../data/repositories.js";
import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from "../interfaces/question.js";
import {Question} from "../entities/questions/question.entity.js";
import {Answer} from "../entities/questions/answer.entity.js";
import {mapToAnswerDTO, mapToAnswerId} from "../interfaces/answer.js";
import {QuestionRepository} from "../data/questions/question-repository.js";
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier.js";
import {mapToUser} from "../interfaces/user.js";
import {Student} from "../entities/users/student.entity.js";
import { mapToStudent } from "../interfaces/student.js";
import { getAnswerRepository, getQuestionRepository } from '../data/repositories.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { Question } from '../entities/questions/question.entity.js';
import { Answer } from '../entities/questions/answer.entity.js';
import { mapToAnswerDTO, mapToAnswerId } from '../interfaces/answer.js';
import { QuestionRepository } from '../data/questions/question-repository.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { mapToUser } from '../interfaces/user.js';
import { Student } from '../entities/users/student.entity.js';
import { mapToStudent } from '../interfaces/student.js';
export async function getAllQuestions(
id: LearningObjectIdentifier, full: boolean
): Promise<QuestionDTO[] | QuestionId[]> {
export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const questionRepository: QuestionRepository = getQuestionRepository();
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id);
@ -19,7 +17,7 @@ export async function getAllQuestions(
return [];
}
const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO);
const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO);
if (full) {
return questionsDTO;
@ -31,21 +29,20 @@ export async function getAllQuestions(
async function fetchQuestion(questionId: QuestionId): Promise<Question | null> {
const questionRepository = getQuestionRepository();
return await questionRepository.findOne(
{
learningObjectHruid: questionId.learningObjectIdentifier.hruid,
learningObjectLanguage: questionId.learningObjectIdentifier.language,
learningObjectVersion: questionId.learningObjectIdentifier.version,
sequenceNumber: questionId.sequenceNumber
}
)
return await questionRepository.findOne({
learningObjectHruid: questionId.learningObjectIdentifier.hruid,
learningObjectLanguage: questionId.learningObjectIdentifier.language,
learningObjectVersion: questionId.learningObjectIdentifier.version,
sequenceNumber: questionId.sequenceNumber,
});
}
export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO | null> {
const question = await fetchQuestion(questionId);
if (!question)
{return null}
if (!question) {
return null;
}
return mapToQuestionDTO(question);
}
@ -54,18 +51,21 @@ export async function getAnswersByQuestion(questionId: QuestionId, full: boolean
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
if (!question)
{return [];}
if (!question) {
return [];
}
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
if (!answers)
{return []}
if (!answers) {
return [];
}
const answersDTO = answers.map(mapToAnswerDTO);
if (full)
{return answersDTO}
if (full) {
return answersDTO;
}
return answersDTO.map(mapToAnswerId);
}
@ -79,10 +79,10 @@ export async function createQuestion(questionDTO: QuestionDTO) {
await questionRepository.createQuestion({
loId: questionDTO.learningObjectIdentifier,
author,
content: questionDTO.content }
);
content: questionDTO.content,
});
} catch (e) {
return null
return null;
}
return questionDTO;
@ -93,18 +93,15 @@ export async function deleteQuestion(questionId: QuestionId) {
const question = await fetchQuestion(questionId);
if (!question)
{return null}
try {
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(
questionId.learningObjectIdentifier, questionId.sequenceNumber
);
} catch (e) {
return null
if (!question) {
return null;
}
return question
try {
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(questionId.learningObjectIdentifier, questionId.sequenceNumber);
} catch (e) {
return null;
}
return question;
}

View file

@ -1,9 +1,4 @@
import {
getClassRepository,
getGroupRepository,
getStudentRepository,
getSubmissionRepository,
} from '../data/repositories.js';
import { getClassRepository, getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js';
import { Class } from '../entities/classes/class.entity.js';
import { Student } from '../entities/users/student.entity.js';
import { AssignmentDTO } from '../interfaces/assignment.js';
@ -14,7 +9,6 @@ import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js';
import { getAllAssignments } from './assignments.js';
import { UserService } from './users.js';
export async function getAllStudents(): Promise<StudentDTO[]> {
const studentRepository = getStudentRepository();
const users = await studentRepository.findAll();
@ -38,9 +32,9 @@ export async function createStudent(userData: StudentDTO): Promise<StudentDTO |
try {
const newStudent = studentRepository.create(mapToStudent(userData));
await studentRepository.save(newStudent);
return mapToStudentDTO(newStudent);
} catch(e) {
} catch (e) {
console.log(e);
return null;
}
@ -57,9 +51,9 @@ export async function deleteStudent(username: string): Promise<StudentDTO | null
try {
await studentRepository.deleteByUsername(username);
return mapToStudentDTO(user);
} catch(e) {
} catch (e) {
console.log(e);
return null;
}
@ -94,11 +88,7 @@ export async function getStudentAssignments(username: string, full: boolean): Pr
const classRepository = getClassRepository();
const classes = await classRepository.findByStudent(student);
const assignments = (
await Promise.all(
classes.map(async (cls) => await getAllAssignments(cls.classId!, full))
)
).flat();
const assignments = (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat();
return assignments;
}
@ -121,9 +111,7 @@ export async function getStudentGroups(username: string, full: boolean): Promise
return groups.map(mapToGroupDTOId);
}
export async function getStudentSubmissions(
username: string
): Promise<SubmissionDTO[]> {
export async function getStudentSubmissions(username: string): Promise<SubmissionDTO[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
@ -135,4 +123,4 @@ export async function getStudentSubmissions(
const submissions = await submissionRepository.findAllSubmissionsForStudent(student);
return submissions.map(mapToSubmissionDTO);
}
}

View file

@ -1,13 +1,13 @@
import {getGroupRepository, getSubmissionRepository} from "../data/repositories.js";
import { Language } from "../entities/content/language.js";
import { LearningObjectIdentifier } from "../entities/content/learning-object-identifier.js";
import {mapToSubmission, mapToSubmissionDTO, SubmissionDTO} from "../interfaces/submission.js";
import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
import { Language } from '../entities/content/language.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { mapToSubmission, mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js';
export async function getSubmission(
learningObjectHruid: string,
language: Language,
version: number,
submissionNumber: number,
submissionNumber: number
): Promise<SubmissionDTO | null> {
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
@ -29,29 +29,23 @@ export async function createSubmission(submissionDTO: SubmissionDTO) {
const newSubmission = await submissionRepository.create(submission);
await submissionRepository.save(newSubmission);
} catch (e) {
return null
return null;
}
return submission;
}
export async function deleteSubmission(
learningObjectHruid: string,
language: Language,
version: number,
submissionNumber: number
) {
export async function deleteSubmission(learningObjectHruid: string, language: Language, version: number, submissionNumber: number) {
const submissionRepository = getSubmissionRepository();
const submission = getSubmission(learningObjectHruid, language, version, submissionNumber);
if (!submission)
{return null}
if (!submission) {
return null;
}
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
return submission;
}

View file

@ -9,12 +9,7 @@ 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 { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { UserService } from './users.js';
import { mapToUser } from '../interfaces/user.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
@ -42,9 +37,9 @@ export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO |
try {
const newTeacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher);
return mapToTeacherDTO(newTeacher);
} catch(e) {
} catch (e) {
console.log(e);
return null;
}
@ -61,9 +56,9 @@ export async function deleteTeacher(username: string): Promise<TeacherDTO | null
try {
await teacherRepository.deleteByUsername(username);
return mapToTeacherDTO(user);
} catch(e) {
} catch (e) {
console.log(e);
return null;
}
@ -93,11 +88,7 @@ export async function getClassIdsByTeacher(username: string): Promise<string[]>
export async function fetchStudentsByTeacher(username: string) {
const classes = await getClassIdsByTeacher(username);
return (
await Promise.all(
classes.map(async (id) => getClassStudents(id))
)
).flat();
return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat();
}
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
@ -122,10 +113,7 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD
// Fetch all questions related to these learning objects
const questionRepository = getQuestionRepository();
const questions =
await questionRepository.findAllByLearningObjects(
learningObjects
);
const questions = await questionRepository.findAllByLearningObjects(learningObjects);
return questions.map(mapToQuestionDTO);
}