#71 - clean code
This commit is contained in:
@@ -1,12 +1,9 @@
|
|||||||
import { BadRequestException, Body, Controller, DefaultValuePipe, Get, HttpCode, HttpStatus, NotFoundException, Param, ParseIntPipe, Post, Query, Req } from '@nestjs/common';
|
import { BadRequestException, Body, Controller, DefaultValuePipe, Get, HttpCode, HttpStatus, InternalServerErrorException, NotFoundException, Param, ParseIntPipe, Post, Query, Req } from '@nestjs/common';
|
||||||
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
|
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
|
||||||
import { Prisma, Song } from '@prisma/client';
|
import { Song } from '@prisma/client';
|
||||||
import { Plage } from 'src/models/plage';
|
|
||||||
import { SongService } from 'src/song/song.service';
|
import { SongService } from 'src/song/song.service';
|
||||||
import { SearchSongDto } from './dto/search-song.dto';
|
import { SearchSongDto } from './dto/search-song.dto';
|
||||||
import { SearchService } from './search.service';
|
import { SearchService } from './search.service';
|
||||||
import { Request } from 'express';
|
|
||||||
import { type } from 'os';
|
|
||||||
|
|
||||||
@ApiTags('search')
|
@ApiTags('search')
|
||||||
@Controller('search')
|
@Controller('search')
|
||||||
@@ -31,13 +28,18 @@ export class SearchController {
|
|||||||
@Post('song/advanced')
|
@Post('song/advanced')
|
||||||
@HttpCode(200) // change from '201 created' to '200 OK' http default response code
|
@HttpCode(200) // change from '201 created' to '200 OK' http default response code
|
||||||
async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise<Song[] | null> {
|
async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise<Song[] | null> {
|
||||||
const ret = await this.searchService.findAdvanced({
|
try {
|
||||||
albumId: searchSongDto.album ? + searchSongDto.album : undefined,
|
const ret = await this.searchService.findAdvanced({
|
||||||
artistId: searchSongDto.artist ? + searchSongDto.artist : undefined,
|
albumId: searchSongDto.album ? + searchSongDto.album : undefined,
|
||||||
genreId: searchSongDto.genre ? + searchSongDto.genre: undefined
|
artistId: searchSongDto.artist ? + searchSongDto.artist : undefined,
|
||||||
});
|
genreId: searchSongDto.genre ? + searchSongDto.genre: undefined
|
||||||
if (!ret) throw new NotFoundException;
|
});
|
||||||
else return ret;
|
if (!ret.length) throw new NotFoundException;
|
||||||
|
else return ret;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
throw new BadRequestException(null, error?.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
@@ -47,7 +49,7 @@ export class SearchController {
|
|||||||
@Get('song/artist/:artistId')
|
@Get('song/artist/:artistId')
|
||||||
async findByArtist(@Param('artistId', ParseIntPipe) artistId: number): Promise<Song[] | null> {
|
async findByArtist(@Param('artistId', ParseIntPipe) artistId: number): Promise<Song[] | null> {
|
||||||
const ret = await this.searchService.songsByArtist(artistId);
|
const ret = await this.searchService.songsByArtist(artistId);
|
||||||
if (!ret || !ret.length) throw new NotFoundException;
|
if (!ret.length) throw new NotFoundException;
|
||||||
else return ret;
|
else return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ export class SearchController {
|
|||||||
@Get('song/genre/:genreId')
|
@Get('song/genre/:genreId')
|
||||||
async findByGenre(@Param('genreId', ParseIntPipe) genreId: number): Promise<Song[] | null> {
|
async findByGenre(@Param('genreId', ParseIntPipe) genreId: number): Promise<Song[] | null> {
|
||||||
const ret = await this.searchService.songsByGenre(genreId);
|
const ret = await this.searchService.songsByGenre(genreId);
|
||||||
if (!ret) throw new NotFoundException;
|
if (!ret.length) throw new NotFoundException;
|
||||||
else return ret;
|
else return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +71,7 @@ export class SearchController {
|
|||||||
@Get('song/album/:albumId')
|
@Get('song/album/:albumId')
|
||||||
async findByAlbum(@Param('albumId', ParseIntPipe) albumId: number): Promise<Song[] | null> {
|
async findByAlbum(@Param('albumId', ParseIntPipe) albumId: number): Promise<Song[] | null> {
|
||||||
const ret = await this.searchService.songsByAlbum(albumId);
|
const ret = await this.searchService.songsByAlbum(albumId);
|
||||||
if (!ret) throw new NotFoundException;
|
if (ret.length) throw new NotFoundException;
|
||||||
else return ret;
|
else return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,21 +83,26 @@ export class SearchController {
|
|||||||
@ApiParam({name: 'word', type: 'string', required: true, example: 'Yoko Shimomura'})
|
@ApiParam({name: 'word', type: 'string', required: true, example: 'Yoko Shimomura'})
|
||||||
@ApiParam({name: 'type', type: 'string', required: true, example: 'artist'})
|
@ApiParam({name: 'type', type: 'string', required: true, example: 'artist'})
|
||||||
async guess(@Param() params: {'type': string, 'word': string}): Promise<any[] | null> {
|
async guess(@Param() params: {'type': string, 'word': string}): Promise<any[] | null> {
|
||||||
let ret: any[];
|
try {
|
||||||
switch (params.type) {
|
let ret: any[];
|
||||||
case 'artist':
|
switch (params.type) {
|
||||||
ret = await this.searchService.guessArtist(params.word);
|
case 'artist':
|
||||||
break;
|
ret = await this.searchService.guessArtist(params.word);
|
||||||
case 'album':
|
break;
|
||||||
ret = await this.searchService.guessAlbum(params.word);
|
case 'album':
|
||||||
break;
|
ret = await this.searchService.guessAlbum(params.word);
|
||||||
case 'song':
|
break;
|
||||||
ret = await this.searchService.guessSong(params.word);
|
case 'song':
|
||||||
break;
|
ret = await this.searchService.guessSong(params.word);
|
||||||
default:
|
break;
|
||||||
throw new BadRequestException;
|
default:
|
||||||
|
throw new BadRequestException;
|
||||||
|
}
|
||||||
|
if (!ret.length) throw new NotFoundException;
|
||||||
|
else return ret;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
throw new InternalServerErrorException(null, error?.toString());
|
||||||
}
|
}
|
||||||
if (!ret || ret.length == 0) throw new NotFoundException;
|
|
||||||
else return ret;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ export class SearchService {
|
|||||||
return this.prisma.song.findMany({
|
return this.prisma.song.findMany({
|
||||||
where: {
|
where: {
|
||||||
artistId: artistId
|
artistId: artistId
|
||||||
}
|
},
|
||||||
|
orderBy: [
|
||||||
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user