all basic routes
This commit is contained in:
@@ -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<Song> {
|
||||
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<Song | null> {
|
||||
const ret = await this.searchService.songByTitle({ name })
|
||||
if (!ret) throw new NotFoundException;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/advanced')
|
||||
async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise<Song[] | null> {
|
||||
throw new NotFoundException;
|
||||
}
|
||||
@Post('/advanced/song')
|
||||
async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise<Song[] | null> {
|
||||
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<Song[] | null> {
|
||||
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<Song[] | null> {
|
||||
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<Song[] | null> {
|
||||
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<any[] | null> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Song | null> {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
|
||||
async songByTitle(songWhereUniqueInput: Prisma.SongWhereUniqueInput): Promise<Song | null> {
|
||||
return this.prisma.song.findUnique({
|
||||
where: songWhereUniqueInput,
|
||||
});
|
||||
}
|
||||
|
||||
async all(): Promise<Song[] | null> {
|
||||
return this.prisma.song.findMany
|
||||
}
|
||||
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
async songsByArtist(artistId: number): Promise<Song[]> {
|
||||
return this.prisma.song.findMany({
|
||||
where: {
|
||||
artistId: artistId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// async songAdvanced(
|
||||
// SongWhereInput: Prisma.SongWhereInput): Promise<Song[] | null> {
|
||||
// return this.prisma.song.findMany({
|
||||
// where: SongWhereInput,
|
||||
// });
|
||||
// }
|
||||
async songsByGenre(genreId: number): Promise<Song[]> {
|
||||
return this.prisma.song.findMany({
|
||||
where: {
|
||||
genreId: genreId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async songsByAlbum(albumId: number): Promise<Song[]> {
|
||||
return this.prisma.song.findMany({
|
||||
where: {
|
||||
albumId: albumId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async artistByName(artistName: string): Promise<Artist | null> {
|
||||
return this.prisma.artist.findUnique({
|
||||
where: {
|
||||
name: artistName
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async guessSong(word: string): Promise<Song[]> {
|
||||
return this.prisma.song.findMany({
|
||||
where: {
|
||||
name: {contains: word}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async guessArtist(word: string): Promise<Artist[]> {
|
||||
return this.prisma.artist.findMany({
|
||||
where: {
|
||||
name: {contains: word},
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async guessAlbum(word: string): Promise<Album[]> {
|
||||
return this.prisma.album.findMany({
|
||||
where: {
|
||||
name: {contains: word},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async findAdvanced(params: {
|
||||
albumId?: number;
|
||||
genreId?: number;
|
||||
artistId?: number;
|
||||
orderBy?: Prisma.SongOrderByWithRelationInput;
|
||||
}): Promise<Song[]> {
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
25
back/test/robot/search/search.robot
Normal file
25
back/test/robot/search/search.robot
Normal file
@@ -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}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user