Implement Progress tracking: User progress will be tracked and saved in the database.…
- User
Implement Progress tracking: User progress will be tracked and saved in the database. Authenticated Users will be able to see their progress in a list of unknown words, well-known words, and words that need to be learned based on the below game controller import { Controller, Get, Req } from ‘@nestjs/common’; import { InjectRepository } from ‘@nestjs/typeorm’; import { Repository } from ‘typeorm’; import { MasculineNoun } from ‘./entities/masculine-noun.entity’; import { FeminineNoun } from ‘./entities/feminine-noun.entity’; import { Word } from ‘./entities/word.entity’;
@Controller(‘game’) export class GameController { constructor( @InjectRepository(MasculineNoun) private masculineNounRepository: Repository, @InjectRepository(FeminineNoun) private feminineNounRepository: Repository, @InjectRepository(Word) private wordRepository: Repository, ) {}
@Get(‘word’) async getWord(@Req() req) { const randomWord = await this.wordRepository .createQueryBuilder(‘word’) .orderBy(‘RANDOM()’) .getOne(); const wordWithTranslation = { …randomWord }; delete wordWithTranslation.translation_fr; return JSON.stringify(wordWithTranslation); }
@Get(‘translation’) async getTranslation(@Req() req) { const randomWord = await this.wordRepository .createQueryBuilder(‘word’) .orderBy(‘RANDOM()’) .getOne(); return JSON.stringify({ en: randomWord.translation_en }); }
@Get(‘masculine-nouns’) async getMasculineNouns(@Req() req) { const nouns = await this.masculineNounRepository.find(); return JSON.stringify(nouns); }
@Get(‘feminine-nouns’) async getFeminineNouns(@Req() req) { const nouns = await this.feminineNounRepository.find(); return JSON.stringify(nouns); } } Create a new word service, controller and word progress entity to handle the progress tracking
- Assistant
To implement progress tracking, we need to create a new word service, controller, and word progress entity.
- Word Progress Entity
Create a new entity file named
word-progress.entity.tsunder theentitiesfolder with the following code:import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class WordProgress { @PrimaryGeneratedColumn() id: number; @Column() userId: number; @Column() wordId: number; @Column() known: boolean; @Column() createdAt: Date; }The
WordProgressentity has five fields:id: primary key generated by the databaseuserId: the ID of the user who is learning the wordwordId: the ID of the word being learnedknown: a boolean value to indicate whether the user knows the wordcreatedAt: the timestamp when the progress record is created
- Word Service
Create a new service file named
word.service.tsunder theservicesfolder with the following code:import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Word } from '../entities/word.entity'; @Injectable() export class WordService { constructor(@InjectRepository(Word) private wordRepository: Repository<Word>) {} async getUnknownWords(userId: number): Promise<Word[]> { // Get all words that the user hasn't marked as known const query = this.wordRepository.createQueryBuilder('word') .leftJoin('word.progress', 'progress', 'progress.userId = :userId', { userId }) .where('progress.known IS NULL') .orWhere('progress.known = false'); return await query.getMany(); } async getWellKnownWords(userId: number): Promise<Word[]> { // Get all words that the user has marked as known const query = this.wordRepository.createQueryBuilder('word') .leftJoin('word.progress', 'progress', 'progress.userId = :userId', { userId }) .where('progress.known = true'); return await query.getMany(); } async getLearnedWords(userId: number): Promise<Word[]> { // Get all words that the user has marked as known const query = this.wordRepository.createQueryBuilder('word') .leftJoin('word.progress', 'progress', 'progress.userId = :userId', { userId }) .where('progress.known = true'); return await query.getMany(); } async markWordAsKnown(userId: number, wordId: number): Promise<void> { // Mark a word as known by inserting a new progress record await this.wordRepository .createQueryBuilder() .insert() .into('word_progress') .values({ userId, wordId, known: true, createdAt: new Date() }) .execute(); } }The
WordServicehas four methods:getUnknownWords: returns a list of words that the user hasn't marked as known yetgetWellKnownWords: returns a list of words that the user has marked as knowngetLearnedWords: returns a list of all words that the user has learned (marked as known)markWordAsKnown: inserts a new progress record to mark a word as known by the user
- Word Progress Controller
Create a new controller file named
word-progress.controller.tsunder thecontrollersfolder with the following code:import { Body, Controller, Post, Req } from '@nestjs/common'; import { WordService } from '../services/word.service'; @Controller('word-progress') export class WordProgressController { constructor(private readonly wordService: WordService) {} @Post('mark-as-known') async markWordAsKnown(@Req() req, @Body() body) { const userId = req.user.id; const wordId = body.wordId; await this.wordService.markWordAsKnown(userId, wordId); } }The
WordProgressControllerhas one route:mark-as-known: marks a word as known for the authenticated user
- Word Progress Module
Create a new module file named
word-progress.module.tsunder themodulesfolder with the following code:import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { WordProgress } from '../entities/word-progress.entity'; import { WordProgressController } from '../controllers/word-progress.controller'; import { WordService } from '../services/word.service'; @Module({ imports: [TypeOrmModule.forFeature([WordProgress])], controllers: [WordProgressController], providers: [WordService], }) export class WordProgressModule {}The
WordProgressModuleimports theWordProgressentity,WordProgressController, andWordService, and sets up the dependency injection for them.- Update Game Controller
Import the
WordServiceand update theGameControllerto track user progress:import { Controller, Get, Req } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { MasculineNoun } from './entities/masculine-noun.entity'; import { FeminineNoun } from './entities/feminine-noun.entity'; import { Word } from './entities/word.entity'; import { WordService } from './services/word.service'; @Controller('game') export class GameController { constructor( @InjectRepository(MasculineNoun) private masculineNounRepository: Repository<MasculineNoun>, @InjectRepository(FeminineNoun) private feminineNounRepository: Repository<FeminineNoun>, @InjectRepository(Word) private wordRepository: Repository<Word>, private readonly wordService: WordService, ) {} @Get('word') async getWord(@Req() req) { const userId = req.user.id; const unknownWords = await this.wordService.getUnknownWords(userId); const randomWord = unknownWords[Math.floor(Math.random() * unknownWords.length)]; const wordWithTranslation = { ...randomWord }; delete wordWithTranslation.translation_fr; return JSON.stringify(wordWithTranslation); } @Get('translation') async getTranslation(@Req() req) { const userId = req.user.id; const wellKnownWords = await this.wordService.getWellKnownWords(userId); const randomWord = wellKnownWords[Math.floor(Math.random() * wellKnownWords.length)]; return JSON.stringify({ en: randomWord.translation_en }); } @Get('masculine-nouns') async getMasculineNouns(@Req() req) { const nouns = await this.masculineNounRepository.find(); return JSON.stringify(nouns); } @Get('feminine-nouns') async getFeminineNouns(@Req() req) { const nouns = await this.feminineNounRepository.find(); return JSON.stringify(nouns); } }In the
getWordmethod, we callwordService.getUnknownWordsto get a list of unknown words for the authenticated user, and return a random word from the list.In the
getTranslationmethod, we callwordService.getWellKnownWordsto get a list of well-known words for the authenticated user, and return a random translation from the list.Finally, we update the
mark-as-knownroute in theWordProgressControllerto insert a new progress record for the authenticated user and the word they marked as known.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.