1
- import { Injectable } from '@nestjs/common' ;
1
+ import { forwardRef , Inject , Injectable } from '@nestjs/common' ;
2
2
import { PostLike } from 'src/common/entities/post-like.entity' ;
3
3
import { QueryRunner } from 'typeorm' ;
4
+ import { InjectRepository } from '@nestjs/typeorm' ;
5
+ import { Repository } from 'typeorm' ;
6
+ import { PostService } from '../post/post.service' ;
7
+ import { PostLikeResponseDto } from './dtos/post-like.response' ;
8
+ import { DataNotFoundException } from 'src/common/exception/service.exception' ;
9
+ import { GetPostLikesResponseDto } from './dtos/get-post-like.response.dto' ;
4
10
5
11
@Injectable ( )
6
12
export class PostLikeService {
13
+ constructor (
14
+ @InjectRepository ( PostLike )
15
+ private readonly postLikeRepository : Repository < PostLike > ,
16
+ @Inject ( forwardRef ( ( ) => PostService ) )
17
+ private readonly postService : PostService ,
18
+ ) { }
19
+
7
20
async deletePostLikeByPostId (
8
21
postId : number ,
9
22
queryRunner : QueryRunner ,
@@ -20,4 +33,73 @@ export class PostLikeService {
20
33
} ) ,
21
34
) ;
22
35
}
23
- }
36
+
37
+ // 유저가 좋아요 누른 게시물들 조회
38
+ async getUserLikes ( userId : number ) : Promise < GetPostLikesResponseDto > {
39
+ const userLikes = await this . postLikeRepository . find ( {
40
+ where : { user : { id : userId } , status : 'activated' } ,
41
+ relations : [ 'post' , 'user' ] ,
42
+ } ) ;
43
+
44
+ const likes = userLikes . map ( ( like ) => ( {
45
+ id : like . id ,
46
+ userId : like . user . id ,
47
+ postId : like . post . id ,
48
+ status : like . status ,
49
+ createdAt : like . createdAt ,
50
+ updatedAt : like . updatedAt ,
51
+ } ) ) ;
52
+
53
+ return {
54
+ totalLikes : likes . length ,
55
+ likes : likes ,
56
+ } ;
57
+ }
58
+
59
+ // 좋아요 상태값
60
+ async toggleLike ( postId : number , userId : number ) : Promise < PostLikeResponseDto > {
61
+ const post = await this . postService . findByFields ( { where : { id : postId } } ) ;
62
+ if ( ! post ) {
63
+ throw DataNotFoundException ( '게시글을 찾을 수 없습니다.' ) ;
64
+ }
65
+
66
+ const existingLike = await this . postLikeRepository . findOne ( {
67
+ where : { post : { id : postId } , user : { id : userId } } ,
68
+ relations : [ 'user' , 'post' ] ,
69
+ } ) ;
70
+
71
+ if ( existingLike ) {
72
+ // 좋아요 actiavated인 경우 -> 좋아요 취소
73
+ // 좋아요 deactivated인 경우 -> 다시 좋아요 누름
74
+ existingLike . status = existingLike . status === 'deactivated' ? 'activated' : 'deactivated' ;
75
+ existingLike . updatedAt = new Date ( ) ;
76
+ await this . postLikeRepository . save ( existingLike ) ;
77
+ return {
78
+ id : existingLike . id ,
79
+ userId : existingLike . user . id ,
80
+ postId : existingLike . post . id ,
81
+ createdAt : existingLike . createdAt ,
82
+ status : existingLike . status ,
83
+ updatedAt : existingLike . updatedAt ,
84
+ } ;
85
+ } else {
86
+ // 좋아요 생성 (처음 좋아요 눌림)
87
+ const newLike = this . postLikeRepository . create ( {
88
+ user : { id : userId } ,
89
+ post : post ,
90
+ status : 'activated' ,
91
+ createdAt : new Date ( ) ,
92
+ updatedAt : new Date ( ) ,
93
+ } ) ;
94
+ await this . postLikeRepository . save ( newLike ) ;
95
+ return {
96
+ id : newLike . id ,
97
+ userId : newLike . user . id ,
98
+ postId : newLike . post . id ,
99
+ createdAt : newLike . createdAt ,
100
+ status : newLike . status ,
101
+ updatedAt : newLike . updatedAt ,
102
+ } ;
103
+ }
104
+ }
105
+ }
0 commit comments