Skip to content

Commit

Permalink
[#122]feat: 좋아요 기능 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Daseul1 committed Apr 3, 2022
1 parent 7d4fbbb commit f2c4822
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions ars/src/apis/likeBoard/likeBoard.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Connection, getRepository, Repository } from 'typeorm';
import { Board } from '../board/entities/board.entity';
import { LikeBoard } from '../board/entities/likeBoard.entity';
import { User } from '../user/entities/user.entity';

@Injectable()
export class LikeBoardService {
Expand All @@ -17,12 +15,14 @@ export class LikeBoardService {
async find(userId) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
await queryRunner.startTransaction('SERIALIZABLE');
try {
const boards = await queryRunner.manager.find(LikeBoard, {
where: { userId },
relations: ['board'],
lock: { mode: 'pessimistic_write' },
});
await queryRunner.commitTransaction();
return boards.map((ele) => ele.board);
} catch (error) {
await queryRunner.rollbackTransaction();
Expand All @@ -35,9 +35,8 @@ export class LikeBoardService {
async like(boardId, userId) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
await queryRunner.startTransaction('SERIALIZABLE');
try {
const board = await queryRunner.manager.findOne(Board, { id: boardId });
const prevLike = await queryRunner.manager.findOne(LikeBoard, {
where: {
userId: userId,
Expand All @@ -47,7 +46,7 @@ export class LikeBoardService {
if (!prevLike) {
await queryRunner.manager.save(LikeBoard, {
userId: userId,
board: board,
board: boardId,
});
await queryRunner.commitTransaction();
return true;
Expand Down

0 comments on commit f2c4822

Please sign in to comment.