Skip to content

Commit

Permalink
Merge pull request #387 from boostcampwm2023/server/feature/384
Browse files Browse the repository at this point in the history
MusicRepository 추가
  • Loading branch information
khw3754 authored Jan 29, 2024
2 parents dd801fd + bac74c6 commit 73da6e6
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 215 deletions.
3 changes: 2 additions & 1 deletion server/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,7 @@ describe('AuthService', () => {
useClass: Repository,
},
{
provide: getRepositoryToken(Music),
provide: MusicRepository,
useClass: Repository,
},
{
Expand Down
172 changes: 0 additions & 172 deletions server/src/entity/music.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ManyToOne,
OneToMany,
PrimaryColumn,
ILike,
Index,
} from 'typeorm';
import { User } from './user.entity';
Expand Down Expand Up @@ -55,175 +54,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<Music[]> {
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<number> {
return this.countBy({ music_id: musicId });
}

static async getRecentMusic(): Promise<Music[]> {
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<Music> {
return this.findOne({
relations: { user: true },
select: { user: { user_id: true, nickname: true } },
where: { music_id },
});
}

static async getCertainMusicByTitle(keyword: string): Promise<Music[]> {
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<boolean> {
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<boolean> {
const count: number = await this.count({
where: { music_id: musicId, user: { user_id: userId } },
});
if (count === 0) {
return false;
}
return true;
}

static async saveMusic(
musicCreateDto: MusicCreateDto,
user_id: string,
): Promise<void> {
const { music_id, title, cover, file: music_file, genre } = musicCreateDto;

const entityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
const newMusic: Music = this.create({
music_id,
title,
cover,
music_file,
created_at: new Date(),
genre,
user: { user_id },
});

await queryRunner.manager.save(newMusic);

await queryRunner.commitTransaction();
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(`music.entity - saveMusic : ENTITY_ERROR`);
throw new CatchyException(
'ENTITY_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.ENTITY_ERROR,
);
} finally {
await queryRunner.release();
}
}

static async deleteMusic(music: Music): Promise<void> {
const entityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
await this.remove(music);

await queryRunner.commitTransaction();
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(`music.entity - deleteMusic : ENTITY_ERROR`);
throw new CatchyException(
'ENTITY_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.ENTITY_ERROR,
);
} finally {
await queryRunner.release();
}
}
}
7 changes: 4 additions & 3 deletions server/src/music/music.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@ describe('UploadController', () => {
let cloudService: NcloudConfigService;
let configService: ConfigService;
let authService: AuthService;
let musicRepository: Repository<Music>;
let musicRepository: MusicRepository;
let mockJwtStrategy = { validate: () => user };
let mockDataSource: jest.Mocked<DataSource>;

Expand All @@ -43,7 +44,7 @@ describe('UploadController', () => {
JwtService,
JwtStrategy,
{
provide: getRepositoryToken(Music),
provide: MusicRepository,
useClass: Repository,
},
{
Expand All @@ -66,7 +67,7 @@ describe('UploadController', () => {
authService = module.get<AuthService>(AuthService);
musicController = module.get<MusicController>(MusicController);
musicService = module.get<MusicService>(MusicService);
musicRepository = module.get(getRepositoryToken(Music));
musicRepository = module.get<MusicRepository>(MusicRepository);
mockDataSource = {
createQueryRunner: jest.fn(),
} as unknown as jest.Mocked<DataSource>;
Expand Down
2 changes: 2 additions & 0 deletions server/src/music/music.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -18,6 +19,7 @@ import { GreenEyeService } from 'src/config/greenEye.service';
NcloudConfigService,
Logger,
GreenEyeService,
MusicRepository,
],
})
export class MusicModule {}
Loading

0 comments on commit 73da6e6

Please sign in to comment.