From 571b3b89e567d526965fc39d24235dbea75fa743 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 23 May 2023 16:21:04 +0900 Subject: [PATCH] Add song history --- back/src/history/history.service.ts | 18 +++++++++++ back/src/song/song.controller.ts | 24 +++++++++++++-- back/src/song/song.module.ts | 3 +- back/test/robot/history/history.robot | 44 +++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/back/src/history/history.service.ts b/back/src/history/history.service.ts index cae6034..6a9fd1d 100644 --- a/back/src/history/history.service.ts +++ b/back/src/history/history.service.ts @@ -52,6 +52,24 @@ export class HistoryService { }); } + async getForSong({ + playerId, + songId, + }: { + playerId: number; + songId: number; + }): Promise<{ best: number; history: SongHistory[] }> { + const history = await this.prisma.songHistory.findMany({ + where: { user: { id: playerId }, song: { id: songId } }, + orderBy: { playDate: 'asc' }, + }); + + return { + best: 0, + history, + }; + } + async createSearchHistoryRecord({ userID, query, diff --git a/back/src/song/song.controller.ts b/back/src/song/song.controller.ts index e76649a..441cb6e 100644 --- a/back/src/song/song.controller.ts +++ b/back/src/song/song.controller.ts @@ -6,6 +6,7 @@ import { DefaultValuePipe, Delete, Get, + HttpCode, InternalServerErrorException, NotFoundException, Param, @@ -14,19 +15,25 @@ import { Query, Req, StreamableFile, + UseGuards, } from '@nestjs/common'; import { Plage } from 'src/models/plage'; import { CreateSongDto } from './dto/create-song.dto'; import { SongService } from './song.service'; import { Request } from 'express'; import { Prisma, Song } from '@prisma/client'; -import { createReadStream, lstat, promises } from 'fs'; -import { ApiTags } from '@nestjs/swagger'; +import { createReadStream } from 'fs'; +import { ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; +import { HistoryService } from 'src/history/history.service'; +import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; @Controller('song') @ApiTags('song') export class SongController { - constructor(private readonly songService: SongService) {} + constructor( + private readonly songService: SongService, + private readonly historyService: HistoryService, + ) { } @Get(':id/midi') async getMidi(@Param('id', ParseIntPipe) id: number) { @@ -107,4 +114,15 @@ export class SongController { if (res === null) throw new NotFoundException('Song not found'); return res; } + + @Get(':id/history') + @HttpCode(200) + @UseGuards(JwtAuthGuard) + @ApiUnauthorizedResponse({ description: 'Invalid token' }) + async getHistory(@Req() req: any, @Param('id', ParseIntPipe) id: number) { + return this.historyService.getForSong({ + playerId: req.user.id, + songId: id, + }); + } } diff --git a/back/src/song/song.module.ts b/back/src/song/song.module.ts index 1cf9780..61a5118 100644 --- a/back/src/song/song.module.ts +++ b/back/src/song/song.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { SongService } from './song.service'; import { SongController } from './song.controller'; import { PrismaModule } from 'src/prisma/prisma.module'; +import { HistoryService } from 'src/history/history.service'; @Module({ - imports: [PrismaModule], + imports: [PrismaModule, HistoryService], providers: [SongService], controllers: [SongController], }) diff --git a/back/test/robot/history/history.robot b/back/test/robot/history/history.robot index d6b4785..cc4aa50 100644 --- a/back/test/robot/history/history.robot +++ b/back/test/robot/history/history.robot @@ -93,3 +93,47 @@ Create and get a search history record String $[1].query "toto" [Teardown] DELETE /users/${userID} + +Get the history of a single song + [Documentation] Create an history item + &{song}= POST + ... /song + ... {"name": "Mama mia", "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"} + Output + &{song2}= POST + ... /song + ... {"name": "Mama mia", "difficulties": {}, "midiPath": "/musics/Beethoven-125-4.midi", "musicXmlPath": "/musics/Beethoven-125-4.mxl"} + Output + ${userID}= RegisterLogin wowuser + + &{history}= POST + ... /history + ... { "userID": ${userID}, "songID": ${song.body.id}, "score": 55, "difficulties": {} } + Output + Integer response status 201 + &{history2}= POST + ... /history + ... { "userID": ${userID}, "songID": ${song.body.id}, "score": 65, "difficulties": {} } + Output + Integer response status 201 + &{history3}= POST + ... /history + ... { "userID": ${userID}, "songID": ${song2.body.id}, "score": 75, "difficulties": {} } + Output + Integer response status 201 + + &{res}= GET /song/${song.body.id}/history + Output + Integer response status 200 + Array response body history + Integer $history[0].userID ${userID} + Integer $history[0].songID ${song.body.id} + Integer $history[0].score 65 + Integer $history[1].userID ${userID} + Integer $history[1].songID ${song.body.id} + Integer $history[1].score 55 + Integer $best 0 + + [Teardown] Run Keywords DELETE /users/${userID} + ... AND DELETE /song/${song.body.id} + ... AND DELETE /song/${song2.body.id}