diff --git a/src/words/words.controller.ts b/src/words/words.controller.ts index 5cad236..2ba4517 100644 --- a/src/words/words.controller.ts +++ b/src/words/words.controller.ts @@ -15,8 +15,8 @@ export class WordsController { } @Get('daily') - getDaily() { - return this.service.getDailyWords(); + getDaily(@Query('difficulty') difficulty?: string) { + return this.service.getDailyWords(difficulty); } @Get('quiz') diff --git a/src/words/words.service.ts b/src/words/words.service.ts index aa4e395..b215b7b 100644 --- a/src/words/words.service.ts +++ b/src/words/words.service.ts @@ -23,27 +23,33 @@ export class WordsService { return this.repo.find({ where: { id: In(ids) } }); } - async getDailyWords(): Promise { + async getDailyWords(difficulty?: string): Promise { const today = new Date().toISOString().split('T')[0]; - const existing = await this.repo.find({ where: { daily_date: today, is_daily: true } }); + const dailyKey = difficulty ? `${today}_${difficulty}` : today; + + const existingQuery = this.repo + .createQueryBuilder('w') + .where('w.daily_date = :dailyKey AND w.is_daily = true', { dailyKey }); + const existing = await existingQuery.getMany(); if (existing.length >= 10) return existing.slice(0, 10); await this.repo .createQueryBuilder() .update(Word) .set({ is_daily: false, daily_date: null }) - .where('daily_date = :today AND is_daily = true', { today }) + .where('daily_date = :dailyKey AND is_daily = true', { dailyKey }) .execute(); - const shuffled = await this.repo + const newWordsQuery = this.repo .createQueryBuilder('w') .orderBy('RAND()') - .limit(10) - .getMany(); + .limit(10); + if (difficulty) newWordsQuery.where('w.difficulty = :difficulty', { difficulty }); + const shuffled = await newWordsQuery.getMany(); for (const w of shuffled) { w.is_daily = true; - w.daily_date = today; + w.daily_date = dailyKey; } return this.repo.save(shuffled); }