2
2
3
3
import com .cona .KUsukKusuk .comment .domain .Comment ;
4
4
import com .cona .KUsukKusuk .comment .dto .CommentGetResponse ;
5
+ import com .cona .KUsukKusuk .comment .dto .CommentListResponseDto ;
5
6
import com .cona .KUsukKusuk .comment .dto .CommentPaginationResponse ;
6
7
import com .cona .KUsukKusuk .comment .exception .CommentNotFoundException ;
7
8
import com .cona .KUsukKusuk .comment .exception .CommentUserNotMatchedException ;
10
11
import com .cona .KUsukKusuk .spot .exception .SpotNotFoundException ;
11
12
import com .cona .KUsukKusuk .spot .repository .SpotRepository ;
12
13
import com .cona .KUsukKusuk .user .domain .User ;
14
+ import com .cona .KUsukKusuk .user .dto .BoomarkLikeResponseDto ;
15
+ import com .cona .KUsukKusuk .user .dto .PageInfo ;
13
16
import com .cona .KUsukKusuk .user .service .UserService ;
17
+ import java .util .stream .Collectors ;
18
+ import lombok .extern .slf4j .Slf4j ;
14
19
import org .springframework .stereotype .Service ;
15
20
16
21
import java .util .ArrayList ;
17
22
import java .util .List ;
18
23
19
24
@ Service
25
+ @ Slf4j
20
26
public class CommentService {
21
27
private final CommentRepository commentRepository ;
22
28
private final SpotRepository spotRepository ;
23
29
private final UserService userService ;
30
+
24
31
public CommentService (CommentRepository commentRepository , SpotRepository spotRepository , UserService userService ) {
25
32
this .commentRepository = commentRepository ;
26
33
this .spotRepository = spotRepository ;
27
34
this .userService = userService ;
28
35
}
36
+
29
37
public Comment save (Comment comment ) {
30
38
Comment savedComment = commentRepository .save (comment );
31
39
return savedComment ;
@@ -37,13 +45,14 @@ public User getCurrentUser() {
37
45
return user ;
38
46
}
39
47
40
- public Spot getCurrentSpot (Long spotId ) {
48
+ public Spot getCurrentSpot (Long spotId ) {
41
49
Spot spot = spotRepository .findById (spotId )
42
50
.orElseThrow (() -> new SpotNotFoundException ());
43
51
return spot ;
44
52
}
45
53
46
- public Comment getCurrentComment (String commentUserName , Spot spot , Long commentId ) throws CommentNotFoundException , CommentUserNotMatchedException {
54
+ public Comment getCurrentComment (String commentUserName , Spot spot , Long commentId )
55
+ throws CommentNotFoundException , CommentUserNotMatchedException {
47
56
List <Comment > commentList = spot .getComments ();
48
57
Comment wantToUpdate = null ; // 초기화를 null로 설정
49
58
for (Comment comment : commentList ) {
@@ -59,10 +68,11 @@ public Comment getCurrentComment(String commentUserName , Spot spot, Long commen
59
68
}
60
69
61
70
//commentUserName과 comment의 작성자 일치 확인
62
- if (wantToUpdate .getUser ().getUserId ().equals (commentUserName ))
71
+ if (wantToUpdate .getUser ().getUserId ().equals (commentUserName )) {
63
72
return wantToUpdate ;
64
- else
73
+ } else {
65
74
throw new CommentUserNotMatchedException ("Don't have authority to update the comment." );
75
+ }
66
76
67
77
}
68
78
@@ -71,46 +81,54 @@ public void delete(Comment comment) {
71
81
}
72
82
73
83
74
- public List <CommentGetResponse > getUserCommentsOfAllSpots (Long userId ) {
75
- //목표 : 사용자가 쓴 comment만 list<CommentGetResponse> 형태로 반환
76
- List <Comment > comments = commentRepository .findAll ();
77
- List <CommentGetResponse > commentsByuser = new ArrayList <>();
78
- Long cNum = 0L ;
79
- for (Comment c : comments )
80
- {
81
- if (c .getUser ().getId ().equals (userId ))
82
- commentsByuser .add (CommentGetResponse .of (++cNum ,c ,c .getCreatedDate ()));
84
+ public List <CommentListResponseDto > getUserCommentsOfAllSpots (Long userId ) {
85
+ // 사용자가 쓴 comment만 가져오기
86
+ User user = getCurrentUser ();
87
+ List <Comment > comments = commentRepository .findByUser (user );
88
+ List <CommentListResponseDto > commentsByUser = new ArrayList <>();
89
+
90
+ // 가져온 comment를 CommentListResponseDto로 변환하여 리스트에 추가
91
+ for (Comment comment : comments ) {
92
+ CommentListResponseDto commentDto = CommentListResponseDto .builder ()
93
+ .spotName (comment .getSpot ().getSpotName ())
94
+ .spotId (comment .getSpot ().getId ())
95
+ .review (comment .getSpot ().getReview ())
96
+ .CommentcreateDate (comment .getSpot ().getCreatedDate ())
97
+ .author (comment .getUser ().getNickname ())
98
+ .spotImageurl (comment .getSpot ().getImageUrls ().get (0 ))
99
+ .build ();
100
+ commentsByUser .add (commentDto );
83
101
}
84
102
85
- return commentsByuser ;
86
-
103
+ return commentsByUser ;
87
104
}
88
105
89
- public CommentPaginationResponse getPagedComments (List <CommentGetResponse > commentsByUser , Long pageNum , Long commentsInPage ){
90
- //commentsByuser 한 객체마다 pagination 해줘서 pagedComments 에 넣어주기
91
- List <CommentPaginationResponse > pagedComments = new ArrayList <>();
106
+ public List <CommentListResponseDto > getPagedComments ( int
107
+ pageNumber
108
+ , int pageSize ) {
109
+
110
+ User user = getCurrentUser ();
111
+ List <Comment > comments = commentRepository .findByUser (user );
112
+ System .out .println ("comments.size() = " + comments .size ());
113
+ List <CommentListResponseDto > pagedResponse = new ArrayList <>();
92
114
93
- Long totalComments = (long ) commentsByUser .size ();
94
- Long amountInBlock = commentsInPage ;
95
- Long lastPage = totalComments / amountInBlock ; // 전체 페이지 수
96
- // 나머지가 0보다 큰 경우에는 몫에 1을 더해주기
97
- if (totalComments % amountInBlock > 0 ) {
98
- lastPage ++;
99
- }
100
- if (pageNum > lastPage || pageNum <= 0 ) {//요청하는 페이지가 존재하지 않는 경우
101
- return null ;
102
- }
103
115
104
- Long curPageNum = 1L ;
116
+ int start = Math .min (pageNumber * pageSize , comments .size ());
117
+ int end = Math .min ((pageNumber + 1 ) * pageSize , comments .size ());
105
118
106
- for (int i = 0 ; i < totalComments ; i += amountInBlock ) {
107
- int endIndex = (int ) Math .min (i + amountInBlock , commentsByUser .size ());
108
- List <CommentGetResponse > currentPageComments = commentsByUser .subList (i , endIndex );
109
- pagedComments .add (CommentPaginationResponse .of (currentPageComments , totalComments , curPageNum , lastPage , (long ) (endIndex -i )));
110
- curPageNum ++;
119
+ if (start > end ) {
120
+ start = end ;
111
121
}
122
+ PageInfo pageInfo = new PageInfo ();
123
+ pageInfo .setTotalElements (comments .size ());
124
+ pageInfo .setPage (pageNumber + 1 );
125
+ pageInfo .setSize (pageSize );
126
+ pageInfo .setTotalPages ((int ) Math .ceil ((double ) comments .size () / pageSize ));
112
127
128
+ List <Comment > pagedcomments = comments .subList (start , end );
113
129
114
- return pagedComments .get ((int ) (pageNum - 1 ));
130
+ return pagedcomments .stream ()
131
+ .map (comment -> CommentListResponseDto .of (comment , pageInfo ))
132
+ .collect (Collectors .toList ());
115
133
}
116
134
}
0 commit comments