Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Implement Progress tracking: User progress will be tracked and saved in the database.…

  1. 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

  2. Assistant

    To implement progress tracking, we need to create a new word service, controller, and word progress entity.

    1. Word Progress Entity

    Create a new entity file named word-progress.entity.ts under the entities folder 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 WordProgress entity has five fields:

    • id: primary key generated by the database
    • userId: the ID of the user who is learning the word
    • wordId: the ID of the word being learned
    • known: a boolean value to indicate whether the user knows the word
    • createdAt: the timestamp when the progress record is created
    1. Word Service

    Create a new service file named word.service.ts under the services folder 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 WordService has four methods:

    • getUnknownWords: returns a list of words that the user hasn't marked as known yet
    • getWellKnownWords: returns a list of words that the user has marked as known
    • getLearnedWords: 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
    1. Word Progress Controller

    Create a new controller file named word-progress.controller.ts under the controllers folder 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 WordProgressController has one route:

    • mark-as-known: marks a word as known for the authenticated user
    1. Word Progress Module

    Create a new module file named word-progress.module.ts under the modules folder 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 WordProgressModule imports the WordProgress entity, WordProgressController, and WordService, and sets up the dependency injection for them.

    1. Update Game Controller

    Import the WordService and update the GameController to 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 getWord method, we call wordService.getUnknownWords to get a list of unknown words for the authenticated user, and return a random word from the list.

    In the getTranslation method, we call wordService.getWellKnownWords to 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-known route in the WordProgressController to insert a new progress record for the authenticated user and the word they marked as known.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.