import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; import { Question } from '../../entities/questions/question.entity.js'; import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js'; import { Student } from '../../entities/users/student.entity.js'; import { LearningObject } from '../../entities/content/learning-object.entity.js'; export class QuestionRepository extends DwengoEntityRepository { public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; content: string }): Promise { const questionEntity = this.create({ learningObjectHruid: question.loId.hruid, learningObjectLanguage: question.loId.language, learningObjectVersion: question.loId.version, author: question.author, content: question.content, timestamp: new Date(), }); questionEntity.learningObjectHruid = question.loId.hruid; questionEntity.learningObjectLanguage = question.loId.language; questionEntity.learningObjectVersion = question.loId.version; questionEntity.author = question.author; questionEntity.content = question.content; return this.insert(questionEntity); } public async findAllQuestionsAboutLearningObject(loId: LearningObjectIdentifier): Promise { return this.findAll({ where: { learningObjectHruid: loId.hruid, learningObjectLanguage: loId.language, learningObjectVersion: loId.version, }, orderBy: { sequenceNumber: 'ASC', }, }); } public async removeQuestionByLearningObjectAndSequenceNumber(loId: LearningObjectIdentifier, sequenceNumber: number): Promise { return this.deleteWhere({ learningObjectHruid: loId.hruid, learningObjectLanguage: loId.language, learningObjectVersion: loId.version, sequenceNumber: sequenceNumber, }); } public async findAllByLearningObjects(learningObjects: LearningObject[]): Promise { const objectIdentifiers = learningObjects.map((lo) => ({ learningObjectHruid: lo.hruid, learningObjectLanguage: lo.language, learningObjectVersion: lo.version, })); return this.findAll({ where: { $or: objectIdentifiers }, orderBy: { timestamp: 'ASC' }, }); } }