This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const questionGenerationPolicy = new RandomNextQuestionPolicy(); | |
const answerValidationPolicy = new TextEqualityAnswerValidationPolicy(); | |
const questionRepository = new QuestionStoreRepository(); | |
const getNextQuestionUseCase = new GetNextQuestion( | |
questionRepository, questionGenerationPolicy | |
); | |
const answerQuestionUseCase = new AnswerQuestion( | |
questionRepository, answerValidationPolicy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getQuestions, getQuestion } from '../../../question-store'; | |
export default class QuestionStoreRepository implements QuestionRepository { | |
async getAvailableQuestions(categoryId: string): Promise<Question[]> { | |
const rawQuestions = await getQuestions(categoryId); | |
return rawQuestions.map(rawQuestion => new Question( | |
rawQuestion.id, | |
rawQuestion.question, | |
rawQuestion.answer | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
export function getQuestions(categoryId: string): Promise<{ id: string, question: string, answer: string}[]> | |
export function getQuestion(questionId: string): Promise<{ id: string, question: string, answer: string}> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextEqualityAnswerValidationPolicy implements AnswerValidationPolicy { | |
isAnswerValid(userAnswer: string, correctAnswer: string): Promise<boolean> { | |
return Promise.resolve( | |
userAnswer.trim().toLowerCase() === correctAnswer.toLowerCase() | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RandomNextQuestionPolicy implements NextQuestionPolicy { | |
async getNextQuestion(availableQuestions: Question[]): Promise<Question> { | |
if (availableQuestions.length === 0) | |
throw new Error('Next question is not available'); | |
const nextRandomIndex = RandomNextQuestionPolicy.getRandomInt( | |
0, availableQuestions.length - 1 | |
); | |
return Promise.resolve(availableQuestions[nextRandomIndex]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnswerResult { | |
userAnswer: string; | |
isValid: boolean; | |
correctAnswer: string; | |
} | |
class AnswerQuestion { | |
questionRepository: QuestionRepository; | |
answerValidationPolicy: AnswerValidationPolicy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetNextQuestion { | |
questionRepository: QuestionRepository; | |
nextQuestionPolicy: NextQuestionPolicy; | |
async invoke(categoryId: string): Promise<Question> { | |
const availableQuestions = await this.questionRepository.getAvailableQuestions( | |
categoryId | |
); | |
return this.nextQuestionPolicy.getNextQuestion(availableQuestions); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface QuestionRepository { | |
getAvailableQuestions(categoryId: string): Promise<Question[]>; | |
getById(questionId: string): Promise<Question>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface NextQuestionPolicy { | |
getNextQuestion(availableQuestions: Question[]): Promise<Question>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface AnswerValidationPolicy { | |
isAnswerValid(userAnswer: string, correctAnswer: string): Promise<boolean>; | |
} |
NewerOlder