feat: group's questions route controller en service geimplementeerd
This commit is contained in:
parent
9a7a193af7
commit
074c5e510a
3 changed files with 43 additions and 2 deletions
|
@ -84,7 +84,7 @@ export async function createGroupHandler(req: Request, res: Response): Promise<v
|
||||||
res.status(201).json({ group });
|
res.status(201).json({ group });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
function getGroupParams(req: Request) {
|
||||||
const classId = req.params.classid;
|
const classId = req.params.classid;
|
||||||
const assignmentId = Number(req.params.assignmentid);
|
const assignmentId = Number(req.params.assignmentid);
|
||||||
const groupId = Number(req.params.groupid);
|
const groupId = Number(req.params.groupid);
|
||||||
|
@ -100,7 +100,21 @@ export async function getGroupSubmissionsHandler(req: Request, res: Response): P
|
||||||
throw new BadRequestException('Group id must be a number');
|
throw new BadRequestException('Group id must be a number');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { classId, assignmentId, groupId, full };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||||
|
const { classId, assignmentId, groupId, full } = getGroupParams(req);
|
||||||
|
|
||||||
const submissions = await getGroupSubmissions(classId, assignmentId, groupId, full);
|
const submissions = await getGroupSubmissions(classId, assignmentId, groupId, full);
|
||||||
|
|
||||||
res.json({ submissions });
|
res.json({ submissions });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getGroupQuestionsHandler(req: Request, res: Response): Promise<void> {
|
||||||
|
const { classId, assignmentId, groupId, full } = getGroupParams(req);
|
||||||
|
|
||||||
|
const questions = await getGroupQuestions(classId, assignmentId, groupId, full);
|
||||||
|
|
||||||
|
res.json({ questions });
|
||||||
|
}
|
||||||
|
|
|
@ -77,6 +77,13 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async findAllByGroup(inGroup: Group): Promise<Question[]> {
|
||||||
|
return this.findAll({
|
||||||
|
where: { inGroup },
|
||||||
|
orderBy: { timestamp: 'DESC' },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Looks up all questions for the given learning object which were asked as part of the given assignment.
|
* Looks up all questions for the given learning object which were asked as part of the given assignment.
|
||||||
* When forStudentUsername is set, only the questions within the given user's group are shown.
|
* When forStudentUsername is set, only the questions within the given user's group are shown.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { EntityDTO } from '@mikro-orm/core';
|
import { EntityDTO } from '@mikro-orm/core';
|
||||||
import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
|
import { getGroupRepository, getQuestionRepository, getSubmissionRepository } from '../data/repositories.js';
|
||||||
import { Group } from '../entities/assignments/group.entity.js';
|
import { Group } from '../entities/assignments/group.entity.js';
|
||||||
import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
|
import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
|
||||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||||
|
@ -12,6 +12,8 @@ import { fetchClass } from './classes.js';
|
||||||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||||
import { Student } from '../entities/users/student.entity.js';
|
import { Student } from '../entities/users/student.entity.js';
|
||||||
import { Class } from '../entities/classes/class.entity.js';
|
import { Class } from '../entities/classes/class.entity.js';
|
||||||
|
import { QuestionDTO, QuestionId } from 'dwengo-1-common/interfaces/question';
|
||||||
|
import { mapToQuestionDTO, mapToQuestionDTOId } from '../interfaces/question.js';
|
||||||
|
|
||||||
async function assertMembersInClass(members: Student[], cls: Class): Promise<void> {
|
async function assertMembersInClass(members: Student[], cls: Class): Promise<void> {
|
||||||
if (!members.every((student) => cls.students.contains(student))) {
|
if (!members.every((student) => cls.students.contains(student))) {
|
||||||
|
@ -121,3 +123,21 @@ export async function getGroupSubmissions(
|
||||||
|
|
||||||
return submissions.map(mapToSubmissionDTOId);
|
return submissions.map(mapToSubmissionDTOId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getGroupQuestions(
|
||||||
|
classId: string,
|
||||||
|
assignmentNumber: number,
|
||||||
|
groupNumber: number,
|
||||||
|
full: boolean
|
||||||
|
): Promise<QuestionDTO[] | QuestionId[]> {
|
||||||
|
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||||
|
|
||||||
|
const questionRepository = getQuestionRepository();
|
||||||
|
const questions = await questionRepository.findAllByGroup(group);
|
||||||
|
|
||||||
|
if (full) {
|
||||||
|
return questions.map(mapToQuestionDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return questions.map(mapToQuestionDTOId);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue