From 91bf8c934b77d25bfc962eea53e6cd9d4232257e Mon Sep 17 00:00:00 2001 From: danis Date: Thu, 19 Jan 2023 18:17:27 +0300 Subject: [PATCH] #71 - clean code --- back/src/search/search.controller.ts | 67 +++++++++++++++------------- back/src/search/search.service.ts | 4 +- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/back/src/search/search.controller.ts b/back/src/search/search.controller.ts index 45b4701..f91eb35 100644 --- a/back/src/search/search.controller.ts +++ b/back/src/search/search.controller.ts @@ -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 { Prisma, Song } from '@prisma/client'; -import { Plage } from 'src/models/plage'; +import { Song } from '@prisma/client'; 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') @@ -31,13 +28,18 @@ export class SearchController { @Post('song/advanced') @HttpCode(200) // change from '201 created' to '200 OK' http default response code 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; + try { + 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.length) throw new NotFoundException; + else return ret; + } catch (error) { + console.log(error); + throw new BadRequestException(null, error?.toString()); + } } @ApiOperation({ @@ -47,7 +49,7 @@ export class SearchController { @Get('song/artist/:artistId') async findByArtist(@Param('artistId', ParseIntPipe) artistId: number): Promise { const ret = await this.searchService.songsByArtist(artistId); - if (!ret || !ret.length) throw new NotFoundException; + if (!ret.length) throw new NotFoundException; else return ret; } @@ -58,7 +60,7 @@ export class SearchController { @Get('song/genre/:genreId') async findByGenre(@Param('genreId', ParseIntPipe) genreId: number): Promise { const ret = await this.searchService.songsByGenre(genreId); - if (!ret) throw new NotFoundException; + if (!ret.length) throw new NotFoundException; else return ret; } @@ -69,7 +71,7 @@ export class SearchController { @Get('song/album/:albumId') async findByAlbum(@Param('albumId', ParseIntPipe) albumId: number): Promise { const ret = await this.searchService.songsByAlbum(albumId); - if (!ret) throw new NotFoundException; + if (ret.length) throw new NotFoundException; else return ret; } @@ -81,21 +83,26 @@ export class SearchController { @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; + try { + 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.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; } } diff --git a/back/src/search/search.service.ts b/back/src/search/search.service.ts index 267a77e..584d387 100644 --- a/back/src/search/search.service.ts +++ b/back/src/search/search.service.ts @@ -16,7 +16,9 @@ export class SearchService { return this.prisma.song.findMany({ where: { artistId: artistId - } + }, + orderBy: [ + ] }); }