Handle includes in the home page

This commit is contained in:
2023-11-29 19:57:39 +01:00
parent 59a48ad060
commit eff5eae706
14 changed files with 149 additions and 238 deletions

View File

@@ -9,68 +9,75 @@ import {
Query,
Request,
UseGuards,
} from '@nestjs/common';
} from "@nestjs/common";
import {
ApiCreatedResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { SearchHistory, SongHistory } from '@prisma/client';
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
import { SongHistoryDto } from './dto/SongHistoryDto';
import { HistoryService } from './history.service';
import { SearchHistoryDto } from './dto/SearchHistoryDto';
import { SongHistory as _SongHistory } from 'src/_gen/prisma-class/song_history';
import { SearchHistory as _SearchHistory } from 'src/_gen/prisma-class/search_history';
} from "@nestjs/swagger";
import { SearchHistory, SongHistory } from "@prisma/client";
import { JwtAuthGuard } from "src/auth/jwt-auth.guard";
import { SongHistoryDto } from "./dto/SongHistoryDto";
import { HistoryService } from "./history.service";
import { SearchHistoryDto } from "./dto/SearchHistoryDto";
import { SongHistory as _SongHistory } from "src/_gen/prisma-class/song_history";
import { SearchHistory as _SearchHistory } from "src/_gen/prisma-class/search_history";
import { SongController } from "src/song/song.controller";
import { mapInclude } from "src/utils/include";
@Controller('history')
@ApiTags('history')
@Controller("history")
@ApiTags("history")
export class HistoryController {
constructor(private readonly historyService: HistoryService) {}
constructor(private readonly historyService: HistoryService) { }
@Get()
@HttpCode(200)
@ApiOperation({ description: 'Get song history of connected user' })
@ApiOperation({ description: "Get song history of connected user" })
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: _SongHistory, isArray: true })
@ApiUnauthorizedResponse({ description: 'Invalid token' })
@ApiUnauthorizedResponse({ description: "Invalid token" })
async getHistory(
@Request() req: any,
@Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number,
@Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number,
@Query("skip", new DefaultValuePipe(0), ParseIntPipe) skip: number,
@Query("take", new DefaultValuePipe(20), ParseIntPipe) take: number,
@Query("include") include: string,
): Promise<SongHistory[]> {
return this.historyService.getHistory(req.user.id, { skip, take });
return this.historyService.getHistory(
req.user.id,
{ skip, take },
mapInclude(include, req, SongController.includableFields),
);
}
@Get('search')
@Get("search")
@HttpCode(200)
@ApiOperation({ description: 'Get search history of connected user' })
@ApiOperation({ description: "Get search history of connected user" })
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: _SearchHistory, isArray: true })
@ApiUnauthorizedResponse({ description: 'Invalid token' })
@ApiUnauthorizedResponse({ description: "Invalid token" })
async getSearchHistory(
@Request() req: any,
@Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number,
@Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number,
@Query("skip", new DefaultValuePipe(0), ParseIntPipe) skip: number,
@Query("take", new DefaultValuePipe(20), ParseIntPipe) take: number,
): Promise<SearchHistory[]> {
return this.historyService.getSearchHistory(req.user.id, { skip, take });
}
@Post()
@HttpCode(201)
@ApiOperation({ description: 'Create a record of a song played by a user' })
@ApiCreatedResponse({ description: 'Succesfully created a record' })
@ApiOperation({ description: "Create a record of a song played by a user" })
@ApiCreatedResponse({ description: "Succesfully created a record" })
async create(@Body() record: SongHistoryDto): Promise<SongHistory> {
return this.historyService.createSongHistoryRecord(record);
}
@Post('search')
@Post("search")
@HttpCode(201)
@ApiOperation({ description: 'Creates a search record in the users history' })
@ApiOperation({ description: "Creates a search record in the users history" })
@UseGuards(JwtAuthGuard)
@ApiUnauthorizedResponse({ description: 'Invalid token' })
@ApiUnauthorizedResponse({ description: "Invalid token" })
async createSearchHistory(
@Request() req: any,
@Body() record: SearchHistoryDto,

View File

@@ -1,12 +1,12 @@
import { Injectable } from '@nestjs/common';
import { SearchHistory, SongHistory } from '@prisma/client';
import { PrismaService } from 'src/prisma/prisma.service';
import { SearchHistoryDto } from './dto/SearchHistoryDto';
import { SongHistoryDto } from './dto/SongHistoryDto';
import { Injectable } from "@nestjs/common";
import { Prisma, SearchHistory, SongHistory } from "@prisma/client";
import { PrismaService } from "src/prisma/prisma.service";
import { SearchHistoryDto } from "./dto/SearchHistoryDto";
import { SongHistoryDto } from "./dto/SongHistoryDto";
@Injectable()
export class HistoryService {
constructor(private prisma: PrismaService) {}
constructor(private prisma: PrismaService) { }
async createSongHistoryRecord({
songID,
@@ -45,13 +45,14 @@ export class HistoryService {
async getHistory(
playerId: number,
{ skip, take }: { skip?: number; take?: number },
include?: Prisma.SongInclude,
): Promise<SongHistory[]> {
return this.prisma.songHistory.findMany({
where: { user: { id: playerId } },
orderBy: { playDate: 'desc' },
orderBy: { playDate: "desc" },
skip,
take,
include: { song: true }
include: { song: include ? { include } : true },
});
}
@@ -64,7 +65,7 @@ export class HistoryService {
}): Promise<{ best: number; history: SongHistory[] }> {
const history = await this.prisma.songHistory.findMany({
where: { user: { id: playerId }, song: { id: songId } },
orderBy: { playDate: 'desc' },
orderBy: { playDate: "desc" },
});
return {
@@ -96,7 +97,7 @@ export class HistoryService {
): Promise<SearchHistory[]> {
return this.prisma.searchHistory.findMany({
where: { user: { id: playerId } },
orderBy: { searchDate: 'desc' },
orderBy: { searchDate: "desc" },
skip,
take,
});