From 712c08303aeaa042ac230733a1e717e518115d22 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 23 May 2023 16:00:59 +0900 Subject: [PATCH] Add create date on history items --- .../migrations/20230523065302_/migration.sql | 5 ++ back/prisma/schema.prisma | 60 ++++++++++--------- back/src/history/history.controller.ts | 19 ++++-- back/src/history/history.service.ts | 4 +- back/test/robot/history/history.robot | 12 +++- 5 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 back/prisma/migrations/20230523065302_/migration.sql diff --git a/back/prisma/migrations/20230523065302_/migration.sql b/back/prisma/migrations/20230523065302_/migration.sql new file mode 100644 index 0000000..8e87d03 --- /dev/null +++ b/back/prisma/migrations/20230523065302_/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "SearchHistory" ADD COLUMN "searchDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; + +-- AlterTable +ALTER TABLE "SongHistory" ADD COLUMN "playDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; diff --git a/back/prisma/schema.prisma b/back/prisma/schema.prisma index 81a6526..f1a7b30 100644 --- a/back/prisma/schema.prisma +++ b/back/prisma/schema.prisma @@ -10,38 +10,39 @@ datasource db { } model User { - id Int @id @default(autoincrement()) - username String @unique - password String - email String - isGuest Boolean @default(false) - partyPlayed Int @default(0) - LessonHistory LessonHistory[] - SongHistory SongHistory[] - searchHistory SearchHistory[] - settings UserSettings? + id Int @id @default(autoincrement()) + username String @unique + password String + email String + isGuest Boolean @default(false) + partyPlayed Int @default(0) + LessonHistory LessonHistory[] + SongHistory SongHistory[] + searchHistory SearchHistory[] + settings UserSettings? } model UserSettings { - id Int @id @default(autoincrement()) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - userId Int @unique - pushNotification Boolean @default(true) - emailNotification Boolean @default(true) - trainingNotification Boolean @default(true) - newSongNotification Boolean @default(true) - recommendations Boolean @default(true) - weeklyReport Boolean @default(true) - leaderBoard Boolean @default(true) - showActivity Boolean @default(true) + id Int @id @default(autoincrement()) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + userId Int @unique + pushNotification Boolean @default(true) + emailNotification Boolean @default(true) + trainingNotification Boolean @default(true) + newSongNotification Boolean @default(true) + recommendations Boolean @default(true) + weeklyReport Boolean @default(true) + leaderBoard Boolean @default(true) + showActivity Boolean @default(true) } model SearchHistory { - id Int @id @default(autoincrement()) - query String - type String - userId Int? - user User? @relation(fields: [userId], references: [id]) + id Int @id @default(autoincrement()) + query String + type String + userId Int? + user User? @relation(fields: [userId], references: [id]) + searchDate DateTime @default(now()) } model Song { @@ -60,13 +61,14 @@ model Song { } model SongHistory { - id Int @id @default(autoincrement()) - song Song @relation(fields: [songID], references: [id], onDelete: Cascade, onUpdate: Cascade) + id Int @id @default(autoincrement()) + song Song @relation(fields: [songID], references: [id], onDelete: Cascade, onUpdate: Cascade) songID Int - user User @relation(fields: [userID], references: [id], onDelete: Cascade, onUpdate: Cascade) + user User @relation(fields: [userID], references: [id], onDelete: Cascade, onUpdate: Cascade) userID Int score Int difficulties Json + playDate DateTime @default(now()) } model Genre { diff --git a/back/src/history/history.controller.ts b/back/src/history/history.controller.ts index 750911d..646a7b6 100644 --- a/back/src/history/history.controller.ts +++ b/back/src/history/history.controller.ts @@ -1,4 +1,15 @@ -import { Body, Controller, DefaultValuePipe, Get, HttpCode, ParseIntPipe, Post, Query, Request, UseGuards } from '@nestjs/common'; +import { + Body, + Controller, + DefaultValuePipe, + Get, + HttpCode, + ParseIntPipe, + Post, + Query, + Request, + UseGuards, +} from '@nestjs/common'; import { ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { SearchHistory, SongHistory } from '@prisma/client'; import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; @@ -6,9 +17,9 @@ import { SongHistoryDto } from './dto/SongHistoryDto'; import { HistoryService } from './history.service'; @Controller('history') -@ApiTags("history") +@ApiTags('history') export class HistoryController { - constructor(private readonly historyService: HistoryService) {} + constructor(private readonly historyService: HistoryService) { } @Get() @HttpCode(200) @@ -22,7 +33,7 @@ export class HistoryController { return this.historyService.getHistory(req.user.id, { skip, take }); } - @Get("search") + @Get('search') @HttpCode(200) @UseGuards(JwtAuthGuard) @ApiUnauthorizedResponse({ description: 'Invalid token' }) diff --git a/back/src/history/history.service.ts b/back/src/history/history.service.ts index 6404acd..cae6034 100644 --- a/back/src/history/history.service.ts +++ b/back/src/history/history.service.ts @@ -6,7 +6,7 @@ import { SongHistoryDto } from './dto/SongHistoryDto'; @Injectable() export class HistoryService { - constructor(private prisma: PrismaService) {} + constructor(private prisma: PrismaService) { } async createSongHistoryRecord({ songID, @@ -46,6 +46,7 @@ export class HistoryService { ): Promise { return this.prisma.songHistory.findMany({ where: { user: { id: playerId } }, + orderBy: { playDate: 'asc' }, skip, take, }); @@ -75,6 +76,7 @@ export class HistoryService { ): Promise { return this.prisma.searchHistory.findMany({ where: { user: { id: playerId } }, + orderBy: { searchDate: 'asc' }, skip, take, }); diff --git a/back/test/robot/history/history.robot b/back/test/robot/history/history.robot index b4aa6c8..d6b4785 100644 --- a/back/test/robot/history/history.robot +++ b/back/test/robot/history/history.robot @@ -63,10 +63,10 @@ Create and get a duplicated history record Array response body Integer $[0].userID ${userID} Integer $[0].songID ${song.body.id} - Integer $[0].score 55 + Integer $[0].score 65 Integer $[1].userID ${userID} Integer $[1].songID ${song.body.id} - Integer $[1].score 65 + Integer $[1].score 55 [Teardown] Run Keywords DELETE /users/${userID} ... AND DELETE /song/${song.body.id} @@ -79,11 +79,17 @@ Create and get a search history record Output Integer response status 404 + GET /search/song/tata + Output + Integer response status 404 + &{res}= GET /history/search Output Integer response status 200 Array response body String $[0].type "song" - String $[0].query "toto" + String $[0].query "tata" + String $[1].type "song" + String $[1].query "toto" [Teardown] DELETE /users/${userID}