From bdf7cad95251619d1e65ee669c1827bbfe875d5c Mon Sep 17 00:00:00 2001 From: danis Date: Tue, 3 Jan 2023 22:40:55 +0300 Subject: [PATCH] create route + added to swagger --- back/src/app.module.ts | 5 ++- back/src/search/dto/search-song.dto.ts | 13 ++++++ back/src/search/search.controller.ts | 59 ++++++++++++++++++++++++++ back/src/search/search.module.ts | 13 ++++++ back/src/search/search.service.ts | 31 ++++++++++++++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 back/src/search/dto/search-song.dto.ts create mode 100644 back/src/search/search.controller.ts create mode 100644 back/src/search/search.module.ts create mode 100644 back/src/search/search.service.ts diff --git a/back/src/app.module.ts b/back/src/app.module.ts index 74a9262..cab6384 100644 --- a/back/src/app.module.ts +++ b/back/src/app.module.ts @@ -12,9 +12,12 @@ import { ArtistService } from './artist/artist.service'; import { GenreModule } from './genre/genre.module'; import { ArtistModule } from './artist/artist.module'; import { AlbumModule } from './album/album.module'; +import { SearchController } from './search/search.controller'; +import { SearchService } from './search/search.service'; +import { SearchModule } from './search/search.module'; @Module({ - imports: [UsersModule, PrismaModule, AuthModule, SongModule, LessonModule, GenreModule, ArtistModule, AlbumModule], + imports: [UsersModule, PrismaModule, AuthModule, SongModule, LessonModule, GenreModule, ArtistModule, AlbumModule, SearchModule], controllers: [AppController], providers: [AppService, PrismaService, ArtistService], }) diff --git a/back/src/search/dto/search-song.dto.ts b/back/src/search/dto/search-song.dto.ts new file mode 100644 index 0000000..d3bd2bf --- /dev/null +++ b/back/src/search/dto/search-song.dto.ts @@ -0,0 +1,13 @@ +import { ApiProperty } from "@nestjs/swagger"; + + +export class SearchSongDto { + @ApiProperty() + artist?: number; + + @ApiProperty() + album?: number; + + @ApiProperty() + genre?: number; +} diff --git a/back/src/search/search.controller.ts b/back/src/search/search.controller.ts new file mode 100644 index 0000000..59cf603 --- /dev/null +++ b/back/src/search/search.controller.ts @@ -0,0 +1,59 @@ +import { BadRequestException, Body, Controller, DefaultValuePipe, Get, NotFoundException, Param, ParseIntPipe, Query, Req } from '@nestjs/common'; +import { 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'; + +@ApiTags('search') +@Controller('search') +export class SearchController { + constructor(private readonly searchService: SearchService, private readonly songService: SongService) {} + @Get() + getHello(): string { + return this.searchService.getHello(); + } + + // @Get('/all') + // findall(): Promise { + // return this.songService.songs(); + // } + + @Get() + async findAll( + @Req() req: Request, + @Query() filter: Prisma.SongWhereInput, + @Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, + @Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, + ): Promise> { + try { + const ret = await this.songService.songs({ + skip, + take, + where: { + ...filter, + id: filter.id ? +filter.id : undefined, + }, + }); + return new Plage(ret, req); + } catch (e) { + console.log(e); + throw new BadRequestException(null, e?.toString()); + } + } + + @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(); + // return ret; + } + + @Get('/advanced') + async findAdvanced(@Body() searchSongDto: SearchSongDto): Promise { + throw new NotFoundException; + } +} diff --git a/back/src/search/search.module.ts b/back/src/search/search.module.ts new file mode 100644 index 0000000..cd3e557 --- /dev/null +++ b/back/src/search/search.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { SearchService } from './search.service'; +import { SearchController } from './search.controller'; +import { PrismaModule } from 'src/prisma/prisma.module'; +import { SongService } from 'src/song/song.service'; + +@Module({ + imports: [PrismaModule], + controllers: [SearchController], + providers: [SearchService, SongService], + exports: [SearchService], +}) +export class SearchModule {} diff --git a/back/src/search/search.service.ts b/back/src/search/search.service.ts new file mode 100644 index 0000000..8c9b92c --- /dev/null +++ b/back/src/search/search.service.ts @@ -0,0 +1,31 @@ +import { DefaultValuePipe, Injectable, ParseIntPipe, Query, Req } from '@nestjs/common'; +import { 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 { + // // return this.prisma.song.findUnique({ + // // where: songWhereUniqueInput, + // // }); + // return this.prisma.song.findMany({ + // where: + // }) + // } + + // async all(): Promise { + // return this.prisma.song.findMany + // } + + getHello(): string { + return 'Hello World!'; + } + + // async songAdvanced( + // SongWhereInput: Prisma.SongWhereInput): Promise { + // return this.prisma.song.findMany({ + // where: SongWhereInput, + // }); + // } +}