Skip to content

Commit 82ecb62

Browse files
authored
Merge pull request #123 from Daseul1/feat/#122
[#122]feat: 좋아요 기능 수정
2 parents 394c3c7 + 7d4fbbb commit 82ecb62

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

ars/src/apis/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AuthService {
2222
{ email: user.email, sub: user.id },
2323
{ secret: process.env.REFRESH_TOKEN_KEY, expiresIn: '8h' },
2424
);
25-
res.setHeader('Access-Control-Allow-Origin', 'https://artipul.shop');
25+
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
2626
res.setHeader(
2727
'Set-Cookie',
2828
`refreshToken=${refreshToken}; path=/; domain=.daseul.shop; SameSite=None; Secure;httpOnly;`,

ars/src/apis/board/board.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ export class BoardService {
9898
await queryRunner.startTransaction();
9999
try {
100100
const board = await queryRunner.manager.findOne(Board, { id: boardId });
101+
const user = await queryRunner.manager.findOne(User, {
102+
id: currentUser.id,
103+
});
101104
const result = await queryRunner.manager.save(Board, {
102105
...board,
103106
...rest,
104-
user: currentUser,
107+
user: user,
105108
thumbnail: image_urls[0],
106109
});
107110

ars/src/apis/comment/comment.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ export class CommentService {
2525
await queryRunner.startTransaction();
2626
try {
2727
const board = await queryRunner.manager.findOne(Board, { id: boardId });
28+
const user = await queryRunner.manager.findOne(User, {
29+
id: currentUser.id,
30+
});
2831
const comment = await queryRunner.manager.save(Comment, {
2932
content,
3033
board,
31-
user: currentUser,
34+
user: user,
3235
});
3336
await queryRunner.commitTransaction();
3437
return comment;

ars/src/apis/likeBoard/likeBoard.service.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,66 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3-
import { Repository } from 'typeorm';
3+
import { Connection, getRepository, Repository } from 'typeorm';
4+
import { Board } from '../board/entities/board.entity';
45
import { LikeBoard } from '../board/entities/likeBoard.entity';
6+
import { User } from '../user/entities/user.entity';
57

68
@Injectable()
79
export class LikeBoardService {
810
constructor(
911
@InjectRepository(LikeBoard)
1012
private readonly likeBoardRepository: Repository<LikeBoard>,
13+
14+
private readonly connection: Connection,
1115
) {}
1216

1317
async find(userId) {
14-
const arts = await this.likeBoardRepository.find({ userId: userId });
15-
return arts.map((ele) => ele.board);
18+
const queryRunner = this.connection.createQueryRunner();
19+
await queryRunner.connect();
20+
await queryRunner.startTransaction();
21+
try {
22+
const boards = await queryRunner.manager.find(LikeBoard, {
23+
where: { userId },
24+
relations: ['board'],
25+
});
26+
return boards.map((ele) => ele.board);
27+
} catch (error) {
28+
await queryRunner.rollbackTransaction();
29+
throw error + 'find board!!!';
30+
} finally {
31+
await queryRunner.manager.release();
32+
}
1633
}
1734

1835
async like(boardId, userId) {
36+
const queryRunner = this.connection.createQueryRunner();
37+
await queryRunner.connect();
38+
await queryRunner.startTransaction();
1939
try {
20-
const prevLike = await this.likeBoardRepository.findOne({
21-
userId: userId,
22-
});
23-
if (prevLike.board !== boardId) {
24-
await this.likeBoardRepository.save({
40+
const board = await queryRunner.manager.findOne(Board, { id: boardId });
41+
const prevLike = await queryRunner.manager.findOne(LikeBoard, {
42+
where: {
2543
userId: userId,
2644
board: boardId,
45+
},
46+
});
47+
if (!prevLike) {
48+
await queryRunner.manager.save(LikeBoard, {
49+
userId: userId,
50+
board: board,
2751
});
52+
await queryRunner.commitTransaction();
53+
return true;
2854
} else {
29-
await this.likeBoardRepository.delete({ userId: userId });
55+
await queryRunner.manager.delete(LikeBoard, { board: boardId });
56+
await queryRunner.commitTransaction();
57+
return false;
3058
}
31-
return true;
3259
} catch (error) {
60+
await queryRunner.rollbackTransaction();
3361
throw error + 'like board!!!';
62+
} finally {
63+
await queryRunner.manager.release();
3464
}
3565
}
3666

0 commit comments

Comments
 (0)