Add song history
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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],
|
||||
})
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user