Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playlist 쿼리문 entity 로 이동 #382

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/src/config/errorCode.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum ERROR_CODE {
'MUSIC_ENCODE_ERROR' = 5002,
'ENCODED_MUSIC_UPLOAD_ERROR' = 5003,
'QUERY_ERROR' = 5004,
'ENTITY_ERROR' = 5005,
'NOT_EXIST_PLAYLIST_ON_USER' = 4001,
'NOT_EXIST_MUSIC' = 4002,
'ALREADY_ADDED' = 4003,
Expand Down
49 changes: 49 additions & 0 deletions server/src/entity/music_playlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import {
BaseEntity,
Column,
Entity,
EntityManager,
Index,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Music } from './music.entity';
import { Playlist } from './playlist.entity';
import { Logger } from '@nestjs/common';
import { CatchyException } from 'src/config/catchyException';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { ERROR_CODE } from 'src/config/errorCode.enum';

@Entity({ name: 'music_playlist' })
export class Music_Playlist extends BaseEntity {
private static readonly logger: Logger = new Logger('MusicPlaylistEntity');

@PrimaryGeneratedColumn()
music_playlist_id: number;

Expand Down Expand Up @@ -79,4 +86,46 @@ export class Music_Playlist extends BaseEntity {
order: { created_at: 'DESC' },
});
}

static async addMusicToPlaylist(
musicId: string,
playlistId: number,
): Promise<number> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

// 관계테이블에 추가
try {
const new_music_playlist: Music_Playlist = this.create({
music: { music_id: musicId },
playlist: { playlist_id: playlistId },
created_at: new Date(),
});

await queryRunner.manager.save(new_music_playlist);
await queryRunner.manager.update(
Playlist,
{ playlist_id: playlistId },
{ updated_at: new Date() },
);

await queryRunner.commitTransaction();

return new_music_playlist.music_playlist_id;
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(
`playlist.entity - addMusicToPlaylist : ENTITY_ERROR`,
);
throw new CatchyException(
'ENTITY_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.ENTITY_ERROR,
);
} finally {
await queryRunner.release();
}
}
}
45 changes: 45 additions & 0 deletions server/src/entity/playlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Column,
CreateDateColumn,
Entity,
EntityManager,
Index,
JoinColumn,
ManyToOne,
Expand All @@ -11,9 +12,16 @@ import {
} from 'typeorm';
import { User } from './user.entity';
import { Music_Playlist } from './music_playlist.entity';
import { PlaylistCreateDto } from 'src/dto/playlistCreate.dto';
import { CatchyException } from 'src/config/catchyException';
import { Logger } from '@nestjs/common';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { ERROR_CODE } from 'src/config/errorCode.enum';

@Entity({ name: 'playlist' })
export class Playlist extends BaseEntity {
private static readonly logger: Logger = new Logger('PlaylistEntity');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

무언가 이 로그를 보니 repository 계층을 추가하는 패턴이 더 괜찮을 수도 있다는 생각이 드네요..!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동의합니다,,


@PrimaryGeneratedColumn()
playlist_id: number;

Expand Down Expand Up @@ -55,4 +63,41 @@ export class Playlist extends BaseEntity {
},
});
}

static async createPlaylist(
userId: string,
playlistCreateDto: PlaylistCreateDto,
): Promise<number> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
const title: string = playlistCreateDto.title;
const newPlaylist: Playlist = this.create({
playlist_title: title,
created_at: new Date(),
updated_at: new Date(),
user: { user_id: userId },
});

const result: Playlist = await queryRunner.manager.save(newPlaylist);
await queryRunner.commitTransaction();

const playlistId: number = result.playlist_id;

return playlistId;
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(`playlist.entity - createPlaylist : ENTITY_ERROR`);
throw new CatchyException(
'ENTITY_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.ENTITY_ERROR,
);
} finally {
await queryRunner.release();
}
}
}
71 changes: 3 additions & 68 deletions server/src/playlist/playlist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DataSource, Repository } from 'typeorm';

@Injectable()
export class PlaylistService {
private readonly logger = new Logger('PlaylistService');
private readonly logger: Logger = new Logger('PlaylistService');
constructor(
@InjectRepository(Playlist)
private playlistRepository: Repository<Playlist>,
Expand All @@ -26,37 +26,7 @@ export class PlaylistService {
userId: string,
playlistCreateDto: PlaylistCreateDto,
): Promise<number> {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

try {
const title: string = playlistCreateDto.title;
const newPlaylist: Playlist = this.playlistRepository.create({
playlist_title: title,
created_at: new Date(),
updated_at: new Date(),
user: { user_id: userId },
});

const result: Playlist = await queryRunner.manager.save(newPlaylist);

await queryRunner.commitTransaction();

const playlistId: number = result.playlist_id;

return playlistId;
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(`playlist.service - createPlaylist : SERVICE_ERROR`);
throw new CatchyException(
'SERVER_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
} finally {
await queryRunner.release();
}
return Playlist.createPlaylist(userId, playlistCreateDto);
}

async addMusicToPlaylist(
Expand Down Expand Up @@ -99,42 +69,7 @@ export class PlaylistService {
);
}

const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

// 관계테이블에 추가
try {
const new_music_playlist: Music_Playlist =
this.music_playlistRepository.create({
music: { music_id: musicId },
playlist: { playlist_id: playlistId },
created_at: new Date(),
});

await queryRunner.manager.save(new_music_playlist);
await queryRunner.manager.update(
Playlist,
{ playlist_id: playlistId },
{ updated_at: new Date() },
);

await queryRunner.commitTransaction();

return new_music_playlist.music_playlist_id;
} catch {
await queryRunner.rollbackTransaction();

this.logger.error(
`playlist.service - addMusicToPlaylist : SERVICE_ERROR`,
);
throw new CatchyException(
'SERVER_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
} finally {
await queryRunner.release();
}
return Music_Playlist.addMusicToPlaylist(musicId, playlistId);
}

async isAlreadyAdded(playlistId: number, musicId: string): Promise<boolean> {
Expand Down
Loading