From e708d2a863b8e5c7395de7f155d91acea58ecb30 Mon Sep 17 00:00:00 2001 From: hyungun Date: Sun, 28 Jan 2024 16:23:10 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat=20:=20MusicRepository=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Music module 과 entity 를 위한 MusicRepository 추가 --- server/src/music/music.repository.ts | 166 +++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 server/src/music/music.repository.ts diff --git a/server/src/music/music.repository.ts b/server/src/music/music.repository.ts new file mode 100644 index 0000000..bd4d5ba --- /dev/null +++ b/server/src/music/music.repository.ts @@ -0,0 +1,166 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { CatchyException } from 'src/config/catchyException'; +import { ERROR_CODE } from 'src/config/errorCode.enum'; +import { MusicCreateDto } from 'src/dto/musicCreate.dto'; +import { Music } from 'src/entity/music.entity'; +import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; +import { DataSource, ILike, Repository } from 'typeorm'; + +@Injectable() +export class MusicRepository { + private musicRepository: Repository; + private readonly logger: Logger = new Logger('MusicRepository'); + + constructor(private readonly dataSource: DataSource) { + this.musicRepository = this.dataSource.getRepository(Music); + } + + async getMusicListByUserId(userId: string, count: number): Promise { + return this.musicRepository.find({ + relations: { + user: true, + }, + where: { + user: { user_id: userId }, + }, + select: { + music_id: true, + title: true, + lyrics: true, + cover: true, + music_file: true, + genre: true, + created_at: true, + user: { user_id: true, nickname: true }, + }, + order: { + created_at: 'DESC', + }, + take: count, + }); + } + + async countMusicById(musicId: string): Promise { + return this.musicRepository.countBy({ music_id: musicId }); + } + + async getRecentMusic(): Promise { + return this.musicRepository.find({ + relations: { + user: true, + }, + select: { + music_id: true, + title: true, + lyrics: true, + cover: true, + music_file: true, + genre: true, + created_at: true, + user: { + user_id: true, + nickname: true, + }, + }, + order: { + created_at: 'DESC', + }, + take: 10, + }); + } + + async getMusicById(music_id: string): Promise { + return this.musicRepository.findOne({ + relations: { user: true }, + select: { user: { user_id: true, nickname: true } }, + where: { music_id }, + }); + } + + async getCertainMusicByTitle(keyword: string): Promise { + return await this.musicRepository.find({ + relations: { + user: true, + music_playlist: false, + }, + select: { + music_id: true, + lyrics: true, + title: true, + cover: true, + music_file: true, + genre: true, + user: { + user_id: true, + nickname: true, + }, + }, + where: { + title: ILike(`%${keyword}%`), + }, + order: { + created_at: 'DESC', + }, + }); + } + + async isExistMusicId(musicId: string): Promise { + const count: number = await this.musicRepository.count({ + where: { music_id: musicId }, + }); + if (count === 0) { + return false; + } + return true; + } + + async isMusicOwner(musicId: string, userId: string): Promise { + const count: number = await this.musicRepository.count({ + where: { music_id: musicId, user: { user_id: userId } }, + }); + if (count === 0) { + return false; + } + return true; + } + + async addMusic(musicCreateDto: MusicCreateDto, user_id): Promise { + const { music_id, title, cover, file: music_file, genre } = musicCreateDto; + + const queryRunner = this.dataSource.createQueryRunner(); + await queryRunner.startTransaction(); + + try { + const newMusic: Music = this.musicRepository.create({ + music_id, + title, + cover, + music_file, + created_at: new Date(), + genre, + user: { user_id }, + }); + + await queryRunner.manager.save(newMusic); + + await queryRunner.commitTransaction(); + + return music_id; + } catch (err) { + await queryRunner.rollbackTransaction(); + + if (err instanceof CatchyException) { + throw err; + } + + this.logger.error(`music.service - createMusic : SERVICE_ERROR`); + throw new CatchyException( + 'SERVER ERROR', + HTTP_STATUS_CODE.SERVER_ERROR, + ERROR_CODE.SERVICE_ERROR, + ); + } finally { + await queryRunner.release(); + } + } +} From 5739d5a3a571e5707afafb0a419bed6358d490e4 Mon Sep 17 00:00:00 2001 From: hyungun Date: Sun, 28 Jan 2024 16:23:55 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor=20:=20=EB=AA=A8=EB=93=A0=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=9D=B4=EB=8F=99,=20=ED=98=B8=EC=B6=9C?= =?UTF-8?q?=EB=B6=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * entity 에 있는 함수들 모두 repository 로 이동 * service 에 있는 쿼리 모두 repository 로 이동 * 기존 Music. 로 호출하는 부분 수정 --- server/src/entity/music.entity.ts | 220 ++++++++++----------- server/src/music/music.module.ts | 2 + server/src/music/music.service.ts | 88 +++++---- server/src/playlist/playlist.module.ts | 3 +- server/src/playlist/playlist.repository.ts | 2 +- server/src/playlist/playlist.service.ts | 8 +- server/src/user/user.module.ts | 3 +- server/src/user/user.service.ts | 4 +- 8 files changed, 172 insertions(+), 158 deletions(-) diff --git a/server/src/entity/music.entity.ts b/server/src/entity/music.entity.ts index 12bb91f..d5ed5fa 100644 --- a/server/src/entity/music.entity.ts +++ b/server/src/entity/music.entity.ts @@ -7,7 +7,7 @@ import { ManyToOne, OneToMany, PrimaryColumn, - ILike, + // ILike, Index, } from 'typeorm'; import { User } from './user.entity'; @@ -49,113 +49,113 @@ export class Music extends BaseEntity { @OneToMany(() => Recent_Played, (recent_played) => recent_played.music) recent_played: Recent_Played[]; - static async getMusicListByUserId( - userId: string, - count: number, - ): Promise { - return this.find({ - relations: { - user: true, - }, - where: { - user: { user_id: userId }, - }, - select: { - music_id: true, - title: true, - lyrics: true, - cover: true, - music_file: true, - genre: true, - created_at: true, - user: { user_id: true, nickname: true }, - }, - order: { - created_at: 'DESC', - }, - take: count, - }); - } - - static async countMusicById(musicId: string): Promise { - return this.countBy({ music_id: musicId }); - } - - static async getRecentMusic(): Promise { - return this.find({ - relations: { - user: true, - }, - select: { - music_id: true, - title: true, - lyrics: true, - cover: true, - music_file: true, - genre: true, - created_at: true, - user: { - user_id: true, - nickname: true, - }, - }, - order: { - created_at: 'DESC', - }, - take: 10, - }); - } - - static async getMusicById(music_id: string): Promise { - return this.findOne({ - relations: { user: true }, - select: { user: { user_id: true, nickname: true } }, - where: { music_id }, - }); - } - - static async getCertainMusicByTitle(keyword: string): Promise { - return await this.find({ - relations: { - user: true, - music_playlist: false, - }, - select: { - music_id: true, - lyrics: true, - title: true, - cover: true, - music_file: true, - genre: true, - user: { - user_id: true, - nickname: true, - }, - }, - where: { - title: ILike(`%${keyword}%`), - }, - order: { - created_at: 'DESC', - }, - }); - } - - static async isExistMusicId(musicId: string): Promise { - const count: number = await this.count({ where: { music_id: musicId } }); - if (count === 0) { - return false; - } - return true; - } - - static async isMusicOwner(musicId: string, userId: string): Promise { - const count: number = await this.count({ - where: { music_id: musicId, user: { user_id: userId } }, - }); - if (count === 0) { - return false; - } - return true; - } + // static async getMusicListByUserId( + // userId: string, + // count: number, + // ): Promise { + // return this.find({ + // relations: { + // user: true, + // }, + // where: { + // user: { user_id: userId }, + // }, + // select: { + // music_id: true, + // title: true, + // lyrics: true, + // cover: true, + // music_file: true, + // genre: true, + // created_at: true, + // user: { user_id: true, nickname: true }, + // }, + // order: { + // created_at: 'DESC', + // }, + // take: count, + // }); + // } + + // static async countMusicById(musicId: string): Promise { + // return this.countBy({ music_id: musicId }); + // } + + // static async getRecentMusic(): Promise { + // return this.find({ + // relations: { + // user: true, + // }, + // select: { + // music_id: true, + // title: true, + // lyrics: true, + // cover: true, + // music_file: true, + // genre: true, + // created_at: true, + // user: { + // user_id: true, + // nickname: true, + // }, + // }, + // order: { + // created_at: 'DESC', + // }, + // take: 10, + // }); + // } + + // static async getMusicById(music_id: string): Promise { + // return this.findOne({ + // relations: { user: true }, + // select: { user: { user_id: true, nickname: true } }, + // where: { music_id }, + // }); + // } + + // static async getCertainMusicByTitle(keyword: string): Promise { + // return await this.find({ + // relations: { + // user: true, + // music_playlist: false, + // }, + // select: { + // music_id: true, + // lyrics: true, + // title: true, + // cover: true, + // music_file: true, + // genre: true, + // user: { + // user_id: true, + // nickname: true, + // }, + // }, + // where: { + // title: ILike(`%${keyword}%`), + // }, + // order: { + // created_at: 'DESC', + // }, + // }); + // } + + // static async isExistMusicId(musicId: string): Promise { + // const count: number = await this.count({ where: { music_id: musicId } }); + // if (count === 0) { + // return false; + // } + // return true; + // } + + // static async isMusicOwner(musicId: string, userId: string): Promise { + // const count: number = await this.count({ + // where: { music_id: musicId, user: { user_id: userId } }, + // }); + // if (count === 0) { + // return false; + // } + // return true; + // } } diff --git a/server/src/music/music.module.ts b/server/src/music/music.module.ts index 2a27f84..8f60d0a 100644 --- a/server/src/music/music.module.ts +++ b/server/src/music/music.module.ts @@ -8,6 +8,7 @@ import { UploadService } from 'src/upload/upload.service'; import { NcloudConfigService } from 'src/config/ncloud.config'; import { Logger } from 'winston'; import { GreenEyeService } from 'src/config/greenEye.service'; +import { MusicRepository } from './music.repository'; @Module({ imports: [TypeOrmModule.forFeature([Music]), AuthModule], @@ -18,6 +19,7 @@ import { GreenEyeService } from 'src/config/greenEye.service'; NcloudConfigService, Logger, GreenEyeService, + MusicRepository, ], }) export class MusicModule {} diff --git a/server/src/music/music.service.ts b/server/src/music/music.service.ts index 5c2e072..d8c60b2 100644 --- a/server/src/music/music.service.ts +++ b/server/src/music/music.service.ts @@ -11,13 +11,15 @@ import { UploadService } from 'src/upload/upload.service'; import { NcloudConfigService } from 'src/config/ncloud.config'; import { AWSError } from 'aws-sdk'; import { DeleteObjectOutput } from 'aws-sdk/clients/s3'; +import { MusicRepository } from './music.repository'; @Injectable() export class MusicService { private readonly logger = new Logger('MusicService'); private objectStorage: AWS.S3; constructor( - @InjectRepository(Music) private musicRepository: Repository, + private musicRepository: MusicRepository, + // @InjectRepository(Music) private musicRepository: Repository, private uploadService: UploadService, private readonly ncloudConfigService: NcloudConfigService, private readonly dataSource: DataSource, @@ -48,46 +50,47 @@ export class MusicService { ); } - const queryRunner = this.dataSource.createQueryRunner(); - await queryRunner.startTransaction(); - - try { - const newMusic: Music = this.musicRepository.create({ - music_id, - title, - cover, - music_file, - created_at: new Date(), - genre, - user: { user_id }, - }); - - await queryRunner.manager.save(newMusic); - - await queryRunner.commitTransaction(); - - return music_id; - } catch (err) { - await queryRunner.rollbackTransaction(); - - if (err instanceof CatchyException) { - throw err; - } - - this.logger.error(`music.service - createMusic : SERVICE_ERROR`); - throw new CatchyException( - 'SERVER ERROR', - HTTP_STATUS_CODE.SERVER_ERROR, - ERROR_CODE.SERVICE_ERROR, - ); - } finally { - await queryRunner.release(); - } + return this.musicRepository.addMusic(musicCreateDto, user_id); + // const queryRunner = this.dataSource.createQueryRunner(); + // await queryRunner.startTransaction(); + + // try { + // const newMusic: Music = this.musicRepository.create({ + // music_id, + // title, + // cover, + // music_file, + // created_at: new Date(), + // genre, + // user: { user_id }, + // }); + + // await queryRunner.manager.save(newMusic); + + // await queryRunner.commitTransaction(); + + // return music_id; + // } catch (err) { + // await queryRunner.rollbackTransaction(); + + // if (err instanceof CatchyException) { + // throw err; + // } + + // this.logger.error(`music.service - createMusic : SERVICE_ERROR`); + // throw new CatchyException( + // 'SERVER ERROR', + // HTTP_STATUS_CODE.SERVER_ERROR, + // ERROR_CODE.SERVICE_ERROR, + // ); + // } finally { + // await queryRunner.release(); + // } } async getRecentMusic(): Promise { try { - return Music.getRecentMusic(); + return this.musicRepository.getRecentMusic(); } catch { this.logger.error(`music.service - getRecentMusic : SERVICE_ERROR`); throw new CatchyException( @@ -100,7 +103,7 @@ export class MusicService { async getMyUploads(userId: string, count: number): Promise { try { - return Music.getMusicListByUserId(userId, count); + return this.musicRepository.getMusicListByUserId(userId, count); } catch { this.logger.error(`music.service - getMyUploads : SERVICE_ERROR`); throw new CatchyException( @@ -113,7 +116,8 @@ export class MusicService { async getMusicInfo(music_id: string): Promise { try { - const targetMusic: Music = await Music.getMusicById(music_id); + const targetMusic: Music = + await this.musicRepository.getMusicById(music_id); if (!targetMusic) { this.logger.error(`music.service - getMusicInfo : NOT_EXIST_MUSIC`); @@ -176,7 +180,7 @@ export class MusicService { async getCertainKeywordNicknameUser(keyword: string): Promise { try { - return await Music.getCertainMusicByTitle(keyword); + return await this.musicRepository.getCertainMusicByTitle(keyword); } catch { this.logger.error( `music.service - getCertainKeywordNicknameUser : QUERY_ERROR`, @@ -190,7 +194,7 @@ export class MusicService { } async deleteMusicById(musicId: string, userId: string): Promise { - if (!(await Music.isMusicOwner(musicId, userId))) { + if (!(await this.musicRepository.isMusicOwner(musicId, userId))) { this.logger.error(`music.service - deleteMusicById : NOT_EXIST_MUSIC`); throw new CatchyException( 'NOT_EXIST_MUSIC', @@ -203,7 +207,7 @@ export class MusicService { await queryRunner.startTransaction(); try { - const music: Music = await Music.getMusicById(musicId); + const music: Music = await this.musicRepository.getMusicById(musicId); await queryRunner.manager.remove(music); const musicFilePath: string = music.music_file.slice( diff --git a/server/src/playlist/playlist.module.ts b/server/src/playlist/playlist.module.ts index c8d8885..c191a83 100644 --- a/server/src/playlist/playlist.module.ts +++ b/server/src/playlist/playlist.module.ts @@ -8,6 +8,7 @@ import { Music_Playlist } from 'src/entity/music_playlist.entity'; import { Music } from 'src/entity/music.entity'; import { PlaylistRepository } from './playlist.repository'; import { Logger } from 'winston'; +import { MusicRepository } from 'src/music/music.repository'; @Module({ imports: [ @@ -15,7 +16,7 @@ import { Logger } from 'winston'; AuthModule, ], controllers: [PlaylistController], - providers: [PlaylistService, PlaylistRepository, Logger], + providers: [PlaylistService, PlaylistRepository, Logger, MusicRepository], exports: [PlaylistService], }) export class PlaylistModule {} diff --git a/server/src/playlist/playlist.repository.ts b/server/src/playlist/playlist.repository.ts index fbd3dee..e908a14 100644 --- a/server/src/playlist/playlist.repository.ts +++ b/server/src/playlist/playlist.repository.ts @@ -11,7 +11,7 @@ import { DataSource, QueryRunner, Repository } from 'typeorm'; export class PlaylistRepository { private playlistRepository: Repository; private music_playlistRepository: Repository; - private readonly logger: Logger = new Logger('PlaylistEntity'); + private readonly logger: Logger = new Logger('PlaylistRepository'); constructor(private readonly dataSource: DataSource) { this.playlistRepository = this.dataSource.getRepository(Playlist); diff --git a/server/src/playlist/playlist.service.ts b/server/src/playlist/playlist.service.ts index 9b88e88..e1d1572 100644 --- a/server/src/playlist/playlist.service.ts +++ b/server/src/playlist/playlist.service.ts @@ -7,11 +7,15 @@ import { Music_Playlist } from 'src/entity/music_playlist.entity'; import { Playlist } from 'src/entity/playlist.entity'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; import { PlaylistRepository } from './playlist.repository'; +import { MusicRepository } from 'src/music/music.repository'; @Injectable() export class PlaylistService { private readonly logger: Logger = new Logger('PlaylistService'); - constructor(private readonly playlistRepository: PlaylistRepository) {} + constructor( + private readonly playlistRepository: PlaylistRepository, + private musicRepository: MusicRepository, + ) {} async createPlaylist( userId: string, @@ -104,7 +108,7 @@ export class PlaylistService { async isExistMusic(musicId: string): Promise { try { - const musicCount: number = await Music.countMusicById(musicId); + const musicCount: number = await this.musicRepository.countMusicById(musicId); return musicCount !== 0; } catch { diff --git a/server/src/user/user.module.ts b/server/src/user/user.module.ts index ecaff11..3640731 100644 --- a/server/src/user/user.module.ts +++ b/server/src/user/user.module.ts @@ -6,10 +6,11 @@ import { User } from 'src/entity/user.entity'; import { AuthModule } from 'src/auth/auth.module'; import { Logger } from 'winston'; import { Recent_Played } from 'src/entity/recent_played.entity'; +import { MusicRepository } from 'src/music/music.repository'; @Module({ imports: [TypeOrmModule.forFeature([User, Recent_Played]), AuthModule], controllers: [UserController], - providers: [UserService, Logger], + providers: [UserService, Logger, MusicRepository], }) export class UserModule {} diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index 2d6ac13..4c799ec 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -8,11 +8,13 @@ import { CatchyException } from 'src/config/catchyException'; import { ERROR_CODE } from 'src/config/errorCode.enum'; import { Recent_Played } from 'src/entity/recent_played.entity'; import { UserUpdateDto } from './../dto/userUpdate.dto'; +import { MusicRepository } from 'src/music/music.repository'; @Injectable() export class UserService { private readonly logger = new Logger('UserService'); constructor( + private musicRepository: MusicRepository, @InjectRepository(User) private userRepository: Repository, @InjectRepository(Recent_Played) private recentPlayedRepository: Repository, @@ -162,7 +164,7 @@ export class UserService { const queryRunner = this.dataSource.createQueryRunner(); await queryRunner.startTransaction(); try { - if (!(await Music.getMusicById(music_id))) { + if (!(await this.musicRepository.getMusicById(music_id))) { this.logger.error( `user.service - updateRecentMusic : NOT_EXIST_MUSIC_ID`, ); From 13092f1a68649a65850fe7020857315979a55c45 Mon Sep 17 00:00:00 2001 From: hyungun Date: Sun, 28 Jan 2024 16:28:05 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor=20:=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=A3=BC=EC=84=9D=20=EC=82=AD=EC=A0=9C,=20?= =?UTF-8?q?=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/entity/music.entity.ts | 111 --------------------------- server/src/music/music.repository.ts | 2 +- server/src/music/music.service.ts | 40 +--------- server/src/user/user.service.ts | 3 +- 4 files changed, 3 insertions(+), 153 deletions(-) diff --git a/server/src/entity/music.entity.ts b/server/src/entity/music.entity.ts index d5ed5fa..75c7984 100644 --- a/server/src/entity/music.entity.ts +++ b/server/src/entity/music.entity.ts @@ -7,7 +7,6 @@ import { ManyToOne, OneToMany, PrimaryColumn, - // ILike, Index, } from 'typeorm'; import { User } from './user.entity'; @@ -48,114 +47,4 @@ export class Music extends BaseEntity { @OneToMany(() => Recent_Played, (recent_played) => recent_played.music) recent_played: Recent_Played[]; - - // static async getMusicListByUserId( - // userId: string, - // count: number, - // ): Promise { - // return this.find({ - // relations: { - // user: true, - // }, - // where: { - // user: { user_id: userId }, - // }, - // select: { - // music_id: true, - // title: true, - // lyrics: true, - // cover: true, - // music_file: true, - // genre: true, - // created_at: true, - // user: { user_id: true, nickname: true }, - // }, - // order: { - // created_at: 'DESC', - // }, - // take: count, - // }); - // } - - // static async countMusicById(musicId: string): Promise { - // return this.countBy({ music_id: musicId }); - // } - - // static async getRecentMusic(): Promise { - // return this.find({ - // relations: { - // user: true, - // }, - // select: { - // music_id: true, - // title: true, - // lyrics: true, - // cover: true, - // music_file: true, - // genre: true, - // created_at: true, - // user: { - // user_id: true, - // nickname: true, - // }, - // }, - // order: { - // created_at: 'DESC', - // }, - // take: 10, - // }); - // } - - // static async getMusicById(music_id: string): Promise { - // return this.findOne({ - // relations: { user: true }, - // select: { user: { user_id: true, nickname: true } }, - // where: { music_id }, - // }); - // } - - // static async getCertainMusicByTitle(keyword: string): Promise { - // return await this.find({ - // relations: { - // user: true, - // music_playlist: false, - // }, - // select: { - // music_id: true, - // lyrics: true, - // title: true, - // cover: true, - // music_file: true, - // genre: true, - // user: { - // user_id: true, - // nickname: true, - // }, - // }, - // where: { - // title: ILike(`%${keyword}%`), - // }, - // order: { - // created_at: 'DESC', - // }, - // }); - // } - - // static async isExistMusicId(musicId: string): Promise { - // const count: number = await this.count({ where: { music_id: musicId } }); - // if (count === 0) { - // return false; - // } - // return true; - // } - - // static async isMusicOwner(musicId: string, userId: string): Promise { - // const count: number = await this.count({ - // where: { music_id: musicId, user: { user_id: userId } }, - // }); - // if (count === 0) { - // return false; - // } - // return true; - // } } diff --git a/server/src/music/music.repository.ts b/server/src/music/music.repository.ts index bd4d5ba..9b957cc 100644 --- a/server/src/music/music.repository.ts +++ b/server/src/music/music.repository.ts @@ -153,7 +153,7 @@ export class MusicRepository { throw err; } - this.logger.error(`music.service - createMusic : SERVICE_ERROR`); + this.logger.error(`music.repository - addMusic : SERVICE_ERROR`); throw new CatchyException( 'SERVER ERROR', HTTP_STATUS_CODE.SERVER_ERROR, diff --git a/server/src/music/music.service.ts b/server/src/music/music.service.ts index d8c60b2..4e3fc20 100644 --- a/server/src/music/music.service.ts +++ b/server/src/music/music.service.ts @@ -1,7 +1,6 @@ import { Injectable, Logger } from '@nestjs/common'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; -import { Repository, DataSource } from 'typeorm'; -import { InjectRepository } from '@nestjs/typeorm'; +import { DataSource } from 'typeorm'; import { MusicCreateDto } from 'src/dto/musicCreate.dto'; import { Music } from 'src/entity/music.entity'; import { Genres, SLICE_COUNT } from 'src/constants'; @@ -10,7 +9,6 @@ import { ERROR_CODE } from 'src/config/errorCode.enum'; import { UploadService } from 'src/upload/upload.service'; import { NcloudConfigService } from 'src/config/ncloud.config'; import { AWSError } from 'aws-sdk'; -import { DeleteObjectOutput } from 'aws-sdk/clients/s3'; import { MusicRepository } from './music.repository'; @Injectable() @@ -19,7 +17,6 @@ export class MusicService { private objectStorage: AWS.S3; constructor( private musicRepository: MusicRepository, - // @InjectRepository(Music) private musicRepository: Repository, private uploadService: UploadService, private readonly ncloudConfigService: NcloudConfigService, private readonly dataSource: DataSource, @@ -51,41 +48,6 @@ export class MusicService { } return this.musicRepository.addMusic(musicCreateDto, user_id); - // const queryRunner = this.dataSource.createQueryRunner(); - // await queryRunner.startTransaction(); - - // try { - // const newMusic: Music = this.musicRepository.create({ - // music_id, - // title, - // cover, - // music_file, - // created_at: new Date(), - // genre, - // user: { user_id }, - // }); - - // await queryRunner.manager.save(newMusic); - - // await queryRunner.commitTransaction(); - - // return music_id; - // } catch (err) { - // await queryRunner.rollbackTransaction(); - - // if (err instanceof CatchyException) { - // throw err; - // } - - // this.logger.error(`music.service - createMusic : SERVICE_ERROR`); - // throw new CatchyException( - // 'SERVER ERROR', - // HTTP_STATUS_CODE.SERVER_ERROR, - // ERROR_CODE.SERVICE_ERROR, - // ); - // } finally { - // await queryRunner.release(); - // } } async getRecentMusic(): Promise { diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index 4c799ec..4270b69 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; import { User } from 'src/entity/user.entity'; import { Music } from 'src/entity/music.entity'; -import { DataSource, Repository, UpdateResult } from 'typeorm'; +import { DataSource, Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { CatchyException } from 'src/config/catchyException'; import { ERROR_CODE } from 'src/config/errorCode.enum'; @@ -15,7 +15,6 @@ export class UserService { private readonly logger = new Logger('UserService'); constructor( private musicRepository: MusicRepository, - @InjectRepository(User) private userRepository: Repository, @InjectRepository(Recent_Played) private recentPlayedRepository: Repository, private readonly dataSource: DataSource, From 8eaa8572f13783a7d3dc70b5857fb7f8863f7763 Mon Sep 17 00:00:00 2001 From: hyungun Date: Sun, 28 Jan 2024 16:37:51 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactor=20:=20test=20code=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * MusicRepository 추가로 인한 테스트코드 변경 --- server/src/auth/auth.service.spec.ts | 3 ++- server/src/music/music.controller.spec.ts | 7 ++++--- server/src/music/music.service.spec.ts | 8 +++++--- server/src/user/user.controller.spec.ts | 5 +++++ server/src/user/user.service.spec.ts | 5 +++++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/server/src/auth/auth.service.spec.ts b/server/src/auth/auth.service.spec.ts index dabbebf..f1065d5 100644 --- a/server/src/auth/auth.service.spec.ts +++ b/server/src/auth/auth.service.spec.ts @@ -10,6 +10,7 @@ import { Music } from 'src/entity/music.entity'; import { Music_Playlist } from 'src/entity/music_playlist.entity'; import { PassportModule } from '@nestjs/passport'; import { PlaylistRepository } from 'src/playlist/playlist.repository'; +import { MusicRepository } from 'src/music/music.repository'; describe('AuthService', () => { let service: AuthService; @@ -32,7 +33,7 @@ describe('AuthService', () => { useClass: Repository, }, { - provide: getRepositoryToken(Music), + provide: MusicRepository, useClass: Repository, }, { diff --git a/server/src/music/music.controller.spec.ts b/server/src/music/music.controller.spec.ts index cf2fe9a..466dcd0 100644 --- a/server/src/music/music.controller.spec.ts +++ b/server/src/music/music.controller.spec.ts @@ -16,6 +16,7 @@ import { JwtService } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { GreenEyeService } from 'src/config/greenEye.service'; import { JwtStrategy } from 'src/auth/jwt.strategy'; +import { MusicRepository } from './music.repository'; describe('UploadController', () => { let app: INestApplication; @@ -25,7 +26,7 @@ describe('UploadController', () => { let cloudService: NcloudConfigService; let configService: ConfigService; let authService: AuthService; - let musicRepository: Repository; + let musicRepository: MusicRepository; let mockJwtStrategy = { validate: () => user }; let mockDataSource: jest.Mocked; @@ -43,7 +44,7 @@ describe('UploadController', () => { JwtService, JwtStrategy, { - provide: getRepositoryToken(Music), + provide: MusicRepository, useClass: Repository, }, { @@ -66,7 +67,7 @@ describe('UploadController', () => { authService = module.get(AuthService); musicController = module.get(MusicController); musicService = module.get(MusicService); - musicRepository = module.get(getRepositoryToken(Music)); + musicRepository = module.get(MusicRepository); mockDataSource = { createQueryRunner: jest.fn(), } as unknown as jest.Mocked; diff --git a/server/src/music/music.service.spec.ts b/server/src/music/music.service.spec.ts index a3ff10a..58ff066 100644 --- a/server/src/music/music.service.spec.ts +++ b/server/src/music/music.service.spec.ts @@ -16,6 +16,7 @@ import { } from 'test/constants/music.mockData'; import { GreenEyeService } from 'src/config/greenEye.service'; import { Recent_Played } from 'src/entity/recent_played.entity'; +import { MusicRepository } from './music.repository'; describe('UploadController', () => { let app: INestApplication; @@ -23,7 +24,8 @@ describe('UploadController', () => { let uploadService: UploadService; let cloudService: NcloudConfigService; let configService: ConfigService; - let mockRepository: jest.Mocked>; + // let mockRepository: jest.Mocked>; + let mockRepository: jest.Mocked; let mockDataSource: jest.Mocked; beforeEach(async () => { @@ -37,7 +39,7 @@ describe('UploadController', () => { ConfigService, GreenEyeService, { - provide: getRepositoryToken(Music), + provide: MusicRepository, useClass: Repository, }, { @@ -65,7 +67,7 @@ describe('UploadController', () => { mockRepository = { create: jest.fn(), save: jest.fn(), - } as unknown as jest.Mocked>; + } as unknown as jest.Mocked; musicService = new MusicService( mockRepository, uploadService, diff --git a/server/src/user/user.controller.spec.ts b/server/src/user/user.controller.spec.ts index 06f2112..1a61fd6 100644 --- a/server/src/user/user.controller.spec.ts +++ b/server/src/user/user.controller.spec.ts @@ -5,6 +5,7 @@ import { getRepositoryToken } from '@nestjs/typeorm'; import { User } from 'src/entity/user.entity'; import { DataSource, Repository } from 'typeorm'; import { Recent_Played } from 'src/entity/recent_played.entity'; +import { MusicRepository } from 'src/music/music.repository'; describe('UserController', () => { let controller: UserController; @@ -25,6 +26,10 @@ describe('UserController', () => { provide: getRepositoryToken(Recent_Played), useClass: Repository, }, + { + provide: MusicRepository, + useClass: Repository, + }, { provide: DataSource, useValue: mockDataSource, diff --git a/server/src/user/user.service.spec.ts b/server/src/user/user.service.spec.ts index d4ebe8f..b72cfab 100644 --- a/server/src/user/user.service.spec.ts +++ b/server/src/user/user.service.spec.ts @@ -5,6 +5,7 @@ import { DataSource, Repository } from 'typeorm'; import { User } from '../entity/user.entity'; import { Recent_Played } from 'src/entity/recent_played.entity'; import { PassportModule } from '@nestjs/passport'; +import { MusicRepository } from 'src/music/music.repository'; describe('UserService', () => { let service: UserService; @@ -24,6 +25,10 @@ describe('UserService', () => { provide: getRepositoryToken(Recent_Played), useClass: Repository, }, + { + provide: MusicRepository, + useClass: Repository, + }, { provide: DataSource, useValue: mockDataSource, From bac74c671412a1a9a717d047473ef1b6164b7edf Mon Sep 17 00:00:00 2001 From: Kim Hyung Un Date: Sun, 28 Jan 2024 17:08:11 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/music/music.service.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/src/music/music.service.ts b/server/src/music/music.service.ts index 9e68bc0..82e59e4 100644 --- a/server/src/music/music.service.ts +++ b/server/src/music/music.service.ts @@ -165,6 +165,9 @@ export class MusicService { ); } + const queryRunner = this.dataSource.createQueryRunner(); + await queryRunner.startTransaction(); + try { const music: Music = await this.musicRepository.getMusicById(musicId); await queryRunner.manager.remove(music);