78 lines
2.0 KiB
TypeScript

export interface QuestionData {
questionId: number;
title: string;
content: string;
userName: string;
created: Date;
answers: AnswerData[];
}
export interface AnswerData {
answertId: number;
content: string;
userName: string;
created: Date;
}
const questions: QuestionData[] = [
{
questionId: 1,
title: 'Why should I learn TypeScript?',
content:
'TypeScript seems to be getting popular so I wonder whether it is worth my time learning it? What benefits does it give over JavaScript?',
userName: 'Bob',
created: new Date(),
answers: [
{
answertId: 1,
content: 'To catch problems earlier speeding up your development',
userName: 'Jane',
created: new Date(),
},
{
answertId: 2,
content: 'So, that you can use the JS features of tomorrow, today',
userName: 'Fred',
created: new Date(),
},
],
},
{
questionId: 2,
title: 'Which state management tool should I use?',
content:
'There seem to be a fair few state management tools around for React - React, Unstated, ... Which one should I use?',
userName: 'Bob',
created: new Date(),
answers: [],
},
];
export const getUnansweredQuestions = async (): Promise<QuestionData[]> => {
await wait(500);
return questions.filter((q) => q.answers.length === 0);
};
const wait = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const getQuestion = async (
questionId: number,
): Promise<QuestionData | null> => {
await wait(500);
const results = questions.filter((q) => q.questionId === questionId);
return results.length === 0 ? null : results[0];
};
export const searchQuestions = async (
criteria: string,
): Promise<QuestionData[]> => {
await wait(500);
return questions.filter(
(q) =>
q.title.toLowerCase().indexOf(criteria.toLowerCase()) >= 0 ||
q.content.toLowerCase().indexOf(criteria.toLowerCase()) >= 0,
);
};