Skip to content

Commit 54f2fa9

Browse files
authored
리팩토링 (#61)
* fix: Like-User 양방향 매핑 * refactor: 리팩토링
1 parent 2dd226f commit 54f2fa9

File tree

13 files changed

+214
-120
lines changed

13 files changed

+214
-120
lines changed

src/main/java/org/ahpuh/surf/like/converter/LikeConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import org.ahpuh.surf.like.entity.Like;
44
import org.ahpuh.surf.post.entity.Post;
5+
import org.ahpuh.surf.user.entity.User;
56
import org.springframework.stereotype.Component;
67

78
@Component
89
public class LikeConverter {
910

10-
public Like toEntity(final Long userId, final Post post) {
11+
public Like toEntity(final User user, final Post post) {
1112
return Like.builder()
12-
.userId(userId)
13+
.user(user)
1314
.post(post)
1415
.build();
1516
}

src/main/java/org/ahpuh/surf/like/entity/Like.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.*;
44
import org.ahpuh.surf.post.entity.Post;
5+
import org.ahpuh.surf.user.entity.User;
56

67
import javax.persistence.*;
78

@@ -24,16 +25,17 @@ public class Like {
2425
@GeneratedValue(strategy = GenerationType.IDENTITY)
2526
private Long likeId;
2627

27-
@Column(name = "user_id")
28-
private Long userId;
28+
@ManyToOne(fetch = FetchType.LAZY)
29+
@JoinColumn(name = "user_id", referencedColumnName = "user_id")
30+
private User user;
2931

3032
@ManyToOne(fetch = FetchType.LAZY)
3133
@JoinColumn(name = "post_id", referencedColumnName = "post_id")
3234
private Post post;
3335

3436
@Builder
35-
public Like(final Long userId, final Post post) {
36-
this.userId = userId;
37+
public Like(final User user, final Post post) {
38+
this.user = user;
3739
this.post = post;
3840
post.addLike(this);
3941
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package org.ahpuh.surf.like.repository;
22

33
import org.ahpuh.surf.like.entity.Like;
4-
import org.ahpuh.surf.post.entity.Post;
54
import org.springframework.data.jpa.repository.JpaRepository;
65

7-
import java.util.Optional;
8-
96
public interface LikeRepository extends JpaRepository<Like, Long> {
10-
11-
Optional<Like> findByUserIdAndPost(Long userId, Post post);
12-
137
}

src/main/java/org/ahpuh/surf/like/service/LikeServiceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.ahpuh.surf.like.repository.LikeRepository;
88
import org.ahpuh.surf.post.entity.Post;
99
import org.ahpuh.surf.post.repository.PostRepository;
10+
import org.ahpuh.surf.user.entity.User;
11+
import org.ahpuh.surf.user.repository.UserRepository;
1012
import org.springframework.stereotype.Service;
1113
import org.springframework.transaction.annotation.Transactional;
1214

@@ -18,21 +20,25 @@
1820
public class LikeServiceImpl implements LikeService {
1921

2022
private final LikeRepository likeRepository;
23+
private final UserRepository userRepository;
2124
private final PostRepository postRepository;
2225

2326
private final LikeConverter likeConverter;
2427

2528
@Override
2629
public Long like(final Long userId, final Long postId) {
30+
final User userEntity = userRepository.findById(userId)
31+
.orElseThrow(() -> EntityExceptionHandler.UserNotFound(userId));
2732
final Post postEntity = postRepository.findById(postId)
2833
.orElseThrow(() -> EntityExceptionHandler.PostNotFound(postId));
29-
return likeRepository.save(likeConverter.toEntity(userId, postEntity))
34+
return likeRepository.save(likeConverter.toEntity(userEntity, postEntity))
3035
.getLikeId();
3136
}
3237

3338
@Override
3439
public void unlike(final Long postId, final Long likeId) {
35-
final Like like = likeRepository.findById(likeId).orElseThrow(() -> new IllegalArgumentException("좋아요한 기록이 없습니다." + likeId));
40+
final Like like = likeRepository.findById(likeId)
41+
.orElseThrow(() -> new IllegalArgumentException("좋아요한 기록이 없습니다." + likeId));
3642
if (!Objects.equals(like.getPost().getPostId(), postId)) {
3743
throw new IllegalArgumentException("The post ID does not match. " + postId);
3844
}

src/main/java/org/ahpuh/surf/post/controller/PostController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public ResponseEntity<Void> cancelFavorite(
105105
}
106106

107107
@GetMapping("/follow/posts")
108-
public ResponseEntity<CursorResult<ExploreDto>> explore(
108+
public ResponseEntity<CursorResult<ExploreDto>> followingExplore(
109109
@AuthenticationPrincipal final JwtAuthentication authentication,
110110
@RequestParam final Long cursorId
111111
) {
@@ -150,10 +150,10 @@ public ResponseEntity<Integer> getAllPostByCategory(
150150
}
151151

152152
@GetMapping("/posts/recent")
153-
public ResponseEntity<List<ExploreDto>> recentAllPosts(
153+
public ResponseEntity<List<RecentPostDto>> recentAllPosts(
154154
@AuthenticationPrincipal final JwtAuthentication authentication
155155
) {
156-
final List<ExploreDto> recentAllPosts = postService.recentAllPosts(authentication.userId);
156+
final List<RecentPostDto> recentAllPosts = postService.recentAllPosts(authentication.userId);
157157
return ResponseEntity.ok().body(recentAllPosts);
158158
}
159159

src/main/java/org/ahpuh/surf/post/converter/PostConverter.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.ahpuh.surf.category.entity.Category;
55
import org.ahpuh.surf.common.exception.EntityExceptionHandler;
66
import org.ahpuh.surf.common.s3.S3ServiceImpl.FileStatus;
7-
import org.ahpuh.surf.like.entity.Like;
87
import org.ahpuh.surf.post.dto.*;
98
import org.ahpuh.surf.post.entity.Post;
109
import org.ahpuh.surf.user.entity.User;
@@ -13,7 +12,6 @@
1312
import java.time.LocalDate;
1413
import java.util.ArrayList;
1514
import java.util.List;
16-
import java.util.Optional;
1715
import java.util.stream.Collectors;
1816

1917
@Component
@@ -33,7 +31,7 @@ public Post toEntity(final User user, final Category category, final PostRequest
3331
return postEntity;
3432
}
3533

36-
public PostDto toDto(final Post post, final Optional<Like> like) {
34+
public PostDto toDto(final Post post, final Long myId) {
3735
final PostDto dto = PostDto.builder()
3836
.postId(post.getPostId())
3937
.userId(post.getUser().getUserId())
@@ -46,7 +44,11 @@ public PostDto toDto(final Post post, final Optional<Like> like) {
4644
.favorite(post.getFavorite())
4745
.createdAt(post.getCreatedAt().toString())
4846
.build();
49-
like.ifPresent(likeEntity -> dto.setLiked(likeEntity.getLikeId()));
47+
post.getLikes()
48+
.stream()
49+
.filter(like -> like.getUser().getUserId().equals(myId))
50+
.findFirst()
51+
.ifPresent(likeEntity -> dto.setLiked(likeEntity.getLikeId()));
5052
return dto;
5153
}
5254

@@ -63,7 +65,7 @@ public PostResponseDto toPostResponseDto(final Post post, final Category categor
6365
.build();
6466
}
6567

66-
public AllPostResponseDto toAllPostResponseDto(final Post post, final Optional<Like> like) {
68+
public AllPostResponseDto toAllPostResponseDto(final Post post, final Long myId) {
6769
final AllPostResponseDto allPostResponseDto = AllPostResponseDto.builder()
6870
.categoryName(post.getCategory().getName())
6971
.colorCode(post.getCategory().getColorCode())
@@ -74,7 +76,11 @@ public AllPostResponseDto toAllPostResponseDto(final Post post, final Optional<L
7476
.fileUrl(post.getFileUrl())
7577
.selectedDate(post.getSelectedDate().toString())
7678
.build();
77-
like.ifPresent(likeEntity -> allPostResponseDto.setLiked(likeEntity.getLikeId()));
79+
post.getLikes()
80+
.stream()
81+
.filter(like -> like.getUser().getUserId().equals(myId))
82+
.findFirst()
83+
.ifPresent(likeEntity -> allPostResponseDto.setLiked(likeEntity.getLikeId()));
7884
return allPostResponseDto;
7985
}
8086

@@ -113,8 +119,8 @@ public List<CategorySimpleDto> sortPostScoresByCategory(
113119
return categorySimpleDtos;
114120
}
115121

116-
public ExploreDto toRecentAllPosts(final Post post, final Optional<Like> like) {
117-
final ExploreDto recentPostDtos = ExploreDto.builder()
122+
public RecentPostDto toRecentAllPosts(final Post post, final User me) {
123+
final RecentPostDto recentPostDto = RecentPostDto.builder()
118124
.userId(post.getUser().getUserId())
119125
.userName(post.getUser().getUserName())
120126
.profilePhotoUrl(post.getUser().getProfilePhotoUrl())
@@ -126,8 +132,18 @@ public ExploreDto toRecentAllPosts(final Post post, final Optional<Like> like) {
126132
.selectedDate(post.getSelectedDate())
127133
.createdAt(post.getCreatedAt())
128134
.build();
129-
like.ifPresent(likeEntity -> recentPostDtos.setLiked(likeEntity.getLikeId()));
130-
return recentPostDtos;
135+
post.getLikes()
136+
.stream()
137+
.filter(like -> like.getUser().equals(me))
138+
.findFirst()
139+
.ifPresent(like -> recentPostDto.setLiked(like.getLikeId()));
140+
if (post.getUser()
141+
.getFollowers()
142+
.stream()
143+
.anyMatch(follow -> follow.getUser().equals(me))) {
144+
recentPostDto.checkFollowed();
145+
}
146+
return recentPostDto;
131147
}
132148

133149
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.ahpuh.surf.post.dto;
2+
3+
import com.querydsl.core.annotations.QueryProjection;
4+
import lombok.*;
5+
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
9+
@Getter
10+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
11+
@AllArgsConstructor
12+
@Builder
13+
public class RecentPostDto {
14+
15+
private Long userId;
16+
17+
private String userName;
18+
19+
private String profilePhotoUrl;
20+
21+
@Builder.Default
22+
private boolean isFollowedUser = false;
23+
24+
private String categoryName;
25+
26+
private String colorCode;
27+
28+
private Long postId;
29+
30+
private String content;
31+
32+
private Integer score;
33+
34+
private String imageUrl;
35+
36+
private String fileUrl;
37+
38+
private LocalDate selectedDate;
39+
40+
private LocalDateTime createdAt;
41+
42+
@Builder.Default
43+
private Long likeId = null;
44+
45+
@Builder.Default
46+
private boolean isLiked = false;
47+
48+
@QueryProjection
49+
public RecentPostDto(final Long userId,
50+
final String userName,
51+
final String profilePhotoUrl,
52+
final String categoryName,
53+
final String colorCode,
54+
final Long postId,
55+
final String content,
56+
final Integer score,
57+
final String imageUrl,
58+
final String fileUrl,
59+
final LocalDate selectedDate,
60+
final LocalDateTime createdAt) {
61+
this.userId = userId;
62+
this.userName = userName;
63+
this.profilePhotoUrl = profilePhotoUrl;
64+
this.categoryName = categoryName;
65+
this.colorCode = colorCode;
66+
this.postId = postId;
67+
this.content = content;
68+
this.score = score;
69+
this.imageUrl = imageUrl;
70+
this.fileUrl = fileUrl;
71+
this.selectedDate = selectedDate;
72+
this.createdAt = createdAt;
73+
this.likeId = null;
74+
this.isLiked = false;
75+
}
76+
77+
public void setLiked(final Long likeId) {
78+
this.likeId = likeId;
79+
this.isLiked = true;
80+
}
81+
82+
public void checkFollowed() {
83+
this.isFollowedUser = true;
84+
}
85+
86+
}

src/main/java/org/ahpuh/surf/post/repository/PostRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostRepositor
1818

1919
List<Post> findAllByUserAndSelectedDateBetweenOrderBySelectedDate(User user, LocalDate start, LocalDate end);
2020

21-
List<Post> findByUserAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc(User user, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
21+
List<Post> findByUserAndSelectedDateIsLessThanEqualAndCreatedAtLessThanOrderBySelectedDateDesc(User user, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
2222

23-
List<Post> findByUserAndCategoryAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc(User user, Category category, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
23+
List<Post> findByUserAndCategoryAndSelectedDateLessThanEqualAndCreatedAtLessThanOrderBySelectedDateDesc(User user, Category category, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
2424

2525
Post findTop1ByCategoryOrderBySelectedDateDesc(Category category);
2626

src/main/java/org/ahpuh/surf/post/repository/PostRepositoryImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public List<ExploreDto> findFollowingPosts(final Long userId, final Pageable pag
3737
))
3838
.from(post)
3939
.leftJoin(follow).on(follow.user.userId.eq(userId))
40-
.where(follow.followedUser.userId.eq(post.user.userId), post.isDeleted.eq(false))
40+
.where(
41+
follow.followedUser.userId.eq(post.user.userId),
42+
post.isDeleted.eq(false)
43+
)
4144
.groupBy(post.postId, follow.followId)
4245
.orderBy(post.selectedDate.desc(), post.createdAt.desc())
4346
.limit(page.getPageSize())
@@ -63,9 +66,14 @@ public List<ExploreDto> findNextFollowingPosts(final Long userId, final LocalDat
6366
))
6467
.from(post)
6568
.leftJoin(follow).on(follow.user.userId.eq(userId))
66-
.where(follow.followedUser.userId.eq(post.user.userId), post.isDeleted.eq(false), post.selectedDate.before(selectedDate), post.createdAt.before(createdAt))
69+
.where(
70+
follow.followedUser.userId.eq(post.user.userId),
71+
post.isDeleted.eq(false),
72+
post.selectedDate.loe(selectedDate),
73+
post.createdAt.before(createdAt)
74+
)
6775
.groupBy(post.postId, follow.followId)
68-
.orderBy(post.selectedDate.desc())
76+
.orderBy(post.selectedDate.desc(), post.createdAt.desc())
6977
.limit(page.getPageSize())
7078
.fetch();
7179
}

src/main/java/org/ahpuh/surf/post/service/PostService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public interface PostService {
3535

3636
int getRecentScore(Long categoryId);
3737

38-
List<ExploreDto> recentAllPosts(Long myId);
38+
List<RecentPostDto> recentAllPosts(Long myId);
3939

4040
}

0 commit comments

Comments
 (0)