diff --git a/back/src/search/search.controller.ts b/back/src/search/search.controller.ts index 0008b57..17839f9 100644 --- a/back/src/search/search.controller.ts +++ b/back/src/search/search.controller.ts @@ -1,22 +1,23 @@ -import { BadRequestException, Body, Controller, DefaultValuePipe, Get, NotFoundException, Param, ParseIntPipe, Query, Req } from '@nestjs/common'; -import { ApiTags } from '@nestjs/swagger'; +import { BadRequestException, Body, Controller, DefaultValuePipe, Get, NotFoundException, Param, ParseIntPipe, Post, Query, Req } from '@nestjs/common'; +import { ApiParam, ApiTags } from '@nestjs/swagger'; import { Prisma, Song } from '@prisma/client'; import { Plage } from 'src/models/plage'; import { SongService } from 'src/song/song.service'; import { SearchSongDto } from './dto/search-song.dto'; import { SearchService } from './search.service'; import { Request } from 'express'; +import { type } from 'os'; @ApiTags('search') @Controller('search') export class SearchController { - constructor(private readonly searchService: SearchService, private readonly songService: SongService) {} - @Get() - getHello(): string { - return this.searchService.getHello(); - } + constructor(private readonly searchService: SearchService, private readonly songService: SongService) {} + @Get() + getHello(): string { + return this.searchService.getHello(); + } - @Get() + @Get() async findAll( @Req() req: Request, @Query() filter: Prisma.SongWhereInput, @@ -39,16 +40,65 @@ export class SearchController { } } - @Get('/title/:name') - async findByName(@Param('name') name: string): Promise { - const ret = await this.searchService.songByTitle({ name }) - if (!ret) throw new NotFoundException(); - throw new NotFoundException(); + @Get('/title/:name') + async findByName(@Param('name') name: string): Promise { + const ret = await this.searchService.songByTitle({ name }) + if (!ret) throw new NotFoundException; return ret; - } + } - @Get('/advanced') - async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise { - throw new NotFoundException; - } + @Post('/advanced/song') + async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise { + const ret = await this.searchService.findAdvanced({ + albumId: searchSongDto.album ? + searchSongDto.album : undefined, + artistId: searchSongDto.artist ? + searchSongDto.artist : undefined, + genreId: searchSongDto.genre ? + searchSongDto.genre: undefined + }); + if (!ret) throw new NotFoundException; + else return ret; + } + + @Get('/artist/:artistId') + async findByArtist(@Param('artistId', ParseIntPipe) artistId: number): Promise { + const ret = await this.searchService.songsByArtist(artistId); + if (!ret) throw new NotFoundException; + else return ret; + } + + @Get('genre/:genreId') + async findByGenre(@Param('genreId', ParseIntPipe) genreId: number): Promise { + const ret = await this.searchService.songsByGenre(genreId); + if (!ret) throw new NotFoundException; + else return ret; + } + + @Get('album/:albumId') + async findByAlbum(@Param('albumId', ParseIntPipe) albumId: number): Promise { + const ret = await this.searchService.songsByAlbum(albumId); + if (!ret) throw new NotFoundException; + else return ret; + } + + @Get('guess/:type/:word') + @ApiParam({name: 'word', type: 'string', required: true, example: 'Yoko Shimomura'}) + @ApiParam({name: 'type', type: 'string', required: true, example: 'artist'}) + async guess(@Param() params: {'type': string, 'word': string}): Promise { + let ret: any[]; + switch (params.type) { + case 'artist': + ret = await this.searchService.guessArtist(params.word); + break; + case 'album': + ret = await this.searchService.guessAlbum(params.word); + break; + case 'song': + ret = await this.searchService.guessSong(params.word); + break; + default: + throw new BadRequestException; + } + if (!ret || ret.length == 0) throw new NotFoundException; + else return ret; + console.log(params.type); + } } diff --git a/back/src/search/search.service.ts b/back/src/search/search.service.ts index 39465ad..39fa8e7 100644 --- a/back/src/search/search.service.ts +++ b/back/src/search/search.service.ts @@ -1,28 +1,96 @@ import { DefaultValuePipe, Injectable, ParseIntPipe, Query, Req } from '@nestjs/common'; -import { Prisma, Song } from '@prisma/client'; +import { Album, Artist, Prisma, Song } from '@prisma/client'; import { PrismaService } from 'src/prisma/prisma.service'; @Injectable() export class SearchService { - constructor(private prisma: PrismaService) {} - async songByTitle(songWhereUniqueInput: Prisma.SongWhereUniqueInput): Promise { + constructor(private prisma: PrismaService) {} + + getHello(): string { + return 'Hello World!'; + } + + async songByTitle(songWhereUniqueInput: Prisma.SongWhereUniqueInput): Promise { return this.prisma.song.findUnique({ where: songWhereUniqueInput, }); } - async all(): Promise { - return this.prisma.song.findMany - } - - getHello(): string { - return 'Hello World!'; + async songsByArtist(artistId: number): Promise { + return this.prisma.song.findMany({ + where: { + artistId: artistId + } + }); } - // async songAdvanced( - // SongWhereInput: Prisma.SongWhereInput): Promise { - // return this.prisma.song.findMany({ - // where: SongWhereInput, - // }); - // } + async songsByGenre(genreId: number): Promise { + return this.prisma.song.findMany({ + where: { + genreId: genreId + } + }); + } + + async songsByAlbum(albumId: number): Promise { + return this.prisma.song.findMany({ + where: { + albumId: albumId + } + }); + } + + async artistByName(artistName: string): Promise { + return this.prisma.artist.findUnique({ + where: { + name: artistName + } + }); + } + + async guessSong(word: string): Promise { + return this.prisma.song.findMany({ + where: { + name: {contains: word} + } + }); + } + + async guessArtist(word: string): Promise { + return this.prisma.artist.findMany({ + where: { + name: {contains: word}, + } + }); + + } + + async guessAlbum(word: string): Promise { + return this.prisma.album.findMany({ + where: { + name: {contains: word}, + } + }); + } + + async findAdvanced(params: { + albumId?: number; + genreId?: number; + artistId?: number; + orderBy?: Prisma.SongOrderByWithRelationInput; + }): Promise { + const { albumId: albumId, genreId: genreId, artistId: artistId, orderBy: orderBy } = params; + return this.prisma.song.findMany({ + where: { + OR:[ + { + albumId: { equals: albumId }, + genreId: { equals: genreId }, + artistId: { equals: artistId }, + } + ] + }, + orderBy + }); + } } diff --git a/back/test/robot/search/search.robot b/back/test/robot/search/search.robot new file mode 100644 index 0000000..2016c27 --- /dev/null +++ b/back/test/robot/search/search.robot @@ -0,0 +1,25 @@ +*** Settings *** +Documentation Tests of the /search route. +... Ensures that the search routes are working properly. + +Resource ../rest.resource + +*** Test Cases *** +Search for Artists + [Documentation] Create an artist + &{res}= POST + ... /artist + ... {"name": "Powerwolf"} + Output + Integer response status 201 + &{get}= GET /search/artist/${res.body.id} + Output + Integer response status 200 + Should Be Equal ${get.body.name} "Powerwolf" + &{get2}= GET /search/guess/artist/Powerwolf + Output + Integer response status 200 + Should Be Equal ${get2.body.id} ${res.body.id} + [Teardown] DELETE /artist/${res.body.id} + +