feat: getDailyWords filter by difficulty param
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 34s

This commit is contained in:
deploy 2026-05-18 01:11:48 +00:00
parent 2c4fd80773
commit 16c47f8692
2 changed files with 15 additions and 9 deletions

View File

@ -15,8 +15,8 @@ export class WordsController {
} }
@Get('daily') @Get('daily')
getDaily() { getDaily(@Query('difficulty') difficulty?: string) {
return this.service.getDailyWords(); return this.service.getDailyWords(difficulty);
} }
@Get('quiz') @Get('quiz')

View File

@ -23,27 +23,33 @@ export class WordsService {
return this.repo.find({ where: { id: In(ids) } }); return this.repo.find({ where: { id: In(ids) } });
} }
async getDailyWords(): Promise<Word[]> { async getDailyWords(difficulty?: string): Promise<Word[]> {
const today = new Date().toISOString().split('T')[0]; 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); if (existing.length >= 10) return existing.slice(0, 10);
await this.repo await this.repo
.createQueryBuilder() .createQueryBuilder()
.update(Word) .update(Word)
.set({ is_daily: false, daily_date: null }) .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(); .execute();
const shuffled = await this.repo const newWordsQuery = this.repo
.createQueryBuilder('w') .createQueryBuilder('w')
.orderBy('RAND()') .orderBy('RAND()')
.limit(10) .limit(10);
.getMany(); if (difficulty) newWordsQuery.where('w.difficulty = :difficulty', { difficulty });
const shuffled = await newWordsQuery.getMany();
for (const w of shuffled) { for (const w of shuffled) {
w.is_daily = true; w.is_daily = true;
w.daily_date = today; w.daily_date = dailyKey;
} }
return this.repo.save(shuffled); return this.repo.save(shuffled);
} }