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

user, auth, music 트랜잭션 코드 entity로 이동 #381

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
52 changes: 22 additions & 30 deletions server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { UserCreateDto } from 'src/dto/userCreate.dto';
import { User } from 'src/entity/user.entity';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { Repository, DataSource } from 'typeorm';
import { v4 as uuid } from 'uuid';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -49,22 +48,9 @@ export class AuthService {
);
}

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

try {
if (email) {
const newUser: User = this.userRepository.create({
user_id: uuid(),
nickname,
photo: null,
user_email: email,
created_at: new Date(),
});

await queryRunner.manager.save(newUser);

await queryRunner.commitTransaction();
await User.saveUser(nickname, email);

return await this.login(email);
}
Expand All @@ -75,10 +61,16 @@ export class AuthService {
HTTP_STATUS_CODE.WRONG_TOKEN,
ERROR_CODE.WRONG_TOKEN,
);
} catch {
await queryRunner.rollbackTransaction();
} finally {
await queryRunner.release();
} catch (error) {
if (error instanceof CatchyException) {
throw error;
}

throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.BAD_REQUEST,
ERROR_CODE.SERVICE_ERROR,
);
}
}

Expand Down Expand Up @@ -112,20 +104,20 @@ export class AuthService {
}

async deleteUser(user: User): Promise<{ userId: string }> {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

const user_id: string = user.user_id;

try {
await queryRunner.manager.update(User, { user_id }, { is_deleted: true });
await queryRunner.commitTransaction();
await User.deleteUser(user.user_id);

return { userId: user.user_id };
} catch {
await queryRunner.rollbackTransaction();
} finally {
await queryRunner.release();
} catch (err) {
if (err instanceof CatchyException) {
throw err;
}

throw new CatchyException(
'SERVICE_ERROR',
HTTP_STATUS_CODE.BAD_REQUEST,
ERROR_CODE.SERVICE_ERROR,
);
}
}
}
68 changes: 68 additions & 0 deletions server/src/entity/music.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ import { User } from './user.entity';
import { Genres } from 'src/constants';
import { Music_Playlist } from './music_playlist.entity';
import { Recent_Played } from './recent_played.entity';
import { MusicCreateDto } from 'src/dto/musicCreate.dto';
import { CatchyException } from 'src/config/catchyException';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { ERROR_CODE } from 'src/config/errorCode.enum';
import { Logger } from '@nestjs/common';

@Entity({ name: 'music' })
export class Music extends BaseEntity {
private static readonly logger: Logger = new Logger('MusicEntity');

@PrimaryColumn()
music_id: string;

Expand Down Expand Up @@ -158,4 +165,65 @@ export class Music extends BaseEntity {
}
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();
}
}
}
123 changes: 123 additions & 0 deletions server/src/entity/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import {
PrimaryColumn,
OneToMany,
ILike,
DataSource,
EntityManager,
} from 'typeorm';
import { Playlist } from './playlist.entity';
import { Music } from './music.entity';
import { Recent_Played } from './recent_played.entity';
import { v4 as uuid } from 'uuid';
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: 'user' })
export class User extends BaseEntity {
private static readonly logger: Logger = new Logger('MusicEntity');

@PrimaryColumn()
user_id: string;

Expand Down Expand Up @@ -81,4 +90,118 @@ export class User extends BaseEntity {
},
});
}

static async updateRecentPlaylist(
music_id: string,
user_id: string,
): Promise<void> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
await queryRunner.manager.update(
Recent_Played,
{ music: { music_id }, user: { user_id } },
{ played_at: new Date() },
);

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

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

static async updateUserInformation(
user_id: string,
image_url: string,
nickname: string | null,
): Promise<void> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
await queryRunner.manager.update(
User,
{ user_id },
{ photo: image_url, nickname },
);
await queryRunner.commitTransaction();
} catch {
await queryRunner.rollbackTransaction();

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

static async saveUser(nickname: string, email: string): Promise<void> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
const newUser = this.create({
user_id: uuid(),
nickname,
photo: null,
user_email: email,
created_at: new Date(),
});

await queryRunner.manager.save(newUser);

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

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

static async deleteUser(user_id: string): Promise<void> {
const entityManager: EntityManager = this.getRepository().manager;
const queryRunner = entityManager.connection.createQueryRunner();
await queryRunner.startTransaction();

try {
await queryRunner.manager.update(User, { user_id }, { is_deleted: true });

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

this.logger.error(`user.entity - deleteUser : ENTITY_ERROR`);
throw new CatchyException(
'ENTITY_ERROR',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.ENTITY_ERROR,
);
} finally {
await queryRunner.release();
}
}
}
40 changes: 9 additions & 31 deletions server/src/music/music.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MusicService {
musicCreateDto: MusicCreateDto,
user_id: string,
): Promise<string> {
const { music_id, title, cover, file: music_file, genre } = musicCreateDto;
const { music_id, genre } = musicCreateDto;

if (!this.isValidGenre(genre)) {
this.logger.error(`music.service - createMusic : NOT_EXIST_GENRE`);
Expand All @@ -48,28 +48,11 @@ 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();
await Music.saveMusic(musicCreateDto, user_id);

return music_id;
} catch (err) {
await queryRunner.rollbackTransaction();

if (err instanceof CatchyException) {
throw err;
}
Expand All @@ -80,8 +63,6 @@ export class MusicService {
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SERVICE_ERROR,
);
} finally {
await queryRunner.release();
}
}

Expand Down Expand Up @@ -199,12 +180,10 @@ export class MusicService {
);
}

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

try {
const music: Music = await Music.getMusicById(musicId);
await queryRunner.manager.remove(music);

await Music.deleteMusic(music);

const musicFilePath: string = music.music_file.slice(
SLICE_COUNT,
Expand All @@ -218,19 +197,18 @@ export class MusicService {
if (musicFilePath) this.deleteFolder(musicFilePath);
if (coverFilePath) this.deleteFolder(coverFilePath);

await queryRunner.commitTransaction();
return music.music_id;
} catch {
await queryRunner.rollbackTransaction();
return musicId;
} catch (err) {
if (err instanceof CatchyException) {
throw err;
}

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

Expand Down
Loading
Loading