Skip to content

Commit ea3b662

Browse files
authored
Merge pull request #24 from Leets-Official/fix/#22/유저정보-조회-및-게시글-조회-수정
[fix] 유저정보 조회 및 게시글 조회 수정
2 parents 9f60b4a + 7713520 commit ea3b662

File tree

9 files changed

+63
-60
lines changed

9 files changed

+63
-60
lines changed

src/main/java/com/leets/xcellentbe/domain/article/domain/repository/ArticleRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.leets.xcellentbe.domain.user.domain.User;
1515

1616
public interface ArticleRepository extends JpaRepository<Article, UUID> {
17-
@Query("SELECT new com.leets.xcellentbe.domain.article.dto.ArticlesWithMediaDto(p, pm.filePath) FROM Article p LEFT JOIN PostMedia pm ON p.articleId = pm.article.articleId WHERE p.writer = :user")
17+
@Query("SELECT new com.leets.xcellentbe.domain.article.dto.ArticlesWithMediaDto(p, pm.filePath) FROM Article p LEFT JOIN ArticleMedia pm ON p.articleId = pm.article.articleId WHERE p.writer = :user")
1818
List<ArticlesWithMediaDto[]> findPostsByWriter(User user);
1919

2020
@Query("SELECT a FROM Article a WHERE a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC")

src/main/java/com/leets/xcellentbe/domain/article/dto/ArticlesResponseDto.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.leets.xcellentbe.domain.article.dto;
22

33
import java.util.List;
4+
import java.util.stream.Collectors;
45

56
import com.leets.xcellentbe.domain.article.domain.Article;
67
import com.leets.xcellentbe.domain.hashtag.domain.Hashtag;
@@ -15,28 +16,29 @@ public class ArticlesResponseDto {
1516
private String writer;
1617
private String content;
1718
private Boolean isPinned;
18-
private List<Hashtag> hashtags;
19-
private Article rePost;
19+
private List<String> hashtags;
20+
// private Article rePost;
2021
private List<String> filePath;
2122

2223
@Builder
23-
private ArticlesResponseDto(String writer, String content, Boolean isPinned, List<Hashtag> hashtags,
24-
Article rePost,
25-
List<String> filePath) {
24+
private ArticlesResponseDto(String writer, String content, Boolean isPinned, List<String> hashtags
25+
, List<String> filePath) {
2626
this.writer = writer;
2727
this.content = content;
2828
this.isPinned = isPinned;
2929
this.hashtags = hashtags;
30-
this.rePost = rePost;
30+
// this.rePost = rePost;
3131
this.filePath = filePath;
3232
}
3333

3434
public static ArticlesResponseDto of(Article article, List<String> filePath) {
3535
return ArticlesResponseDto.builder()
3636
.writer(article.getWriter().getUserName())
3737
.content(article.getContent())
38-
.hashtags(article.getHashtags())
39-
.rePost(article.getRePost())
38+
.hashtags(article.getHashtags().stream()
39+
.map(Hashtag::getContent)
40+
.collect(Collectors.toList()))
41+
// .rePost(article.getRePost())
4042
.filePath(filePath)
4143
.build();
4244
}

src/main/java/com/leets/xcellentbe/domain/follow/domain/repository/FollowRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public interface FollowRepository extends JpaRepository<Follow, Long> {
1515
Page<Follow> findByFollower(User user, Pageable pageable);
1616

1717
Page<Follow> findByFollowing(User user, Pageable pageable);
18+
19+
int countByFollower(User user);
20+
21+
int countByFollowing(User user);
1822
}

src/main/java/com/leets/xcellentbe/domain/follow/dto/FollowInfoResponseDto.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public FollowInfoResponseDto(String customId, String userName) {
1717
this.userName = userName;
1818
}
1919

20-
public static FollowInfoResponseDto from(Follow follow) {
21-
User user = follow.getFollowing();
20+
public static FollowInfoResponseDto from(Follow follow, boolean isFollower) {
21+
User user = isFollower ? follow.getFollower() : follow.getFollowing();
2222
return FollowInfoResponseDto.builder()
2323
.customId(user.getCustomId())
2424
.userName(user.getUserName())

src/main/java/com/leets/xcellentbe/domain/follow/service/FollowService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public void followUser(FollowRequestDto requestDto, HttpServletRequest request)
3939
throw new FollowOperationError();
4040
}
4141

42+
if (user.equals(targetUser)) { // 자기 자신을 팔로우시 예외처리
43+
throw new FollowOperationError();
44+
}
45+
4246
Follow follow = Follow.create(user, targetUser);
4347
followRepository.save(follow);
4448
}
@@ -78,7 +82,7 @@ public Page<FollowInfoResponseDto> getFollowingList(String customId, int pageNo)
7882
Pageable pageable = createPageable(pageNo);
7983

8084
return followRepository.findByFollower(user, pageable)
81-
.map(FollowInfoResponseDto::from);
85+
.map(follow -> FollowInfoResponseDto.from(follow, false));
8286
}
8387

8488
// 팔로워 목록 조회
@@ -87,7 +91,7 @@ public Page<FollowInfoResponseDto> getFollowerList(String customId, int pageNo)
8791
Pageable pageable = createPageable(pageNo);
8892

8993
return followRepository.findByFollowing(user, pageable)
90-
.map(FollowInfoResponseDto::from);
94+
.map(follow -> FollowInfoResponseDto.from(follow, true));
9195
}
9296

9397
// 커스텀아이디로 유저 검색

src/main/java/com/leets/xcellentbe/domain/postMedia/domain/PostMedia.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/com/leets/xcellentbe/domain/user/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public ResponseEntity<GlobalResponseDto<String>> updateProfile(
4242
@GetMapping("/info")
4343
@Operation(summary = "프로필 조회", description = "특정 사용자의 프로필 내용을 조회합니다.")
4444
public ResponseEntity<GlobalResponseDto<UserProfileResponseDto>> getProfileWithoutToken(
45-
@RequestParam String customId) {
45+
@RequestParam String customId, HttpServletRequest request) {
4646
return ResponseEntity.status(HttpStatus.OK)
47-
.body(GlobalResponseDto.success(userService.getProfileWithoutToken(customId)));
47+
.body(GlobalResponseDto.success(userService.getProfileWithoutToken(customId, request)));
4848
}
4949

5050
@PatchMapping("/profile-image")

src/main/java/com/leets/xcellentbe/domain/user/dto/UserProfileResponseDto.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.leets.xcellentbe.domain.user.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import com.leets.xcellentbe.domain.user.domain.User;
45

56
import lombok.Builder;
@@ -21,11 +22,20 @@ public class UserProfileResponseDto {
2122
private int userBirthYear;
2223
private int userBirthMonth;
2324
private int userBirthDay;
25+
private int followersCount;
26+
private int followingsCount;
27+
28+
@JsonProperty("isFollowing")
29+
private boolean isFollowing;
30+
31+
@JsonProperty("isMyProfile")
32+
private boolean isMyProfile;
2433

2534
@Builder
2635
private UserProfileResponseDto(String email, String customId, String userName, String profileImageUrl,
2736
String backgroundProfileImageUrl, String phoneNumber, String description, String websiteUrl, String location,
28-
int userBirthYear, int userBirthMonth, int userBirthDay) {
37+
int userBirthYear, int userBirthMonth, int userBirthDay, int followersCount, int followingsCount,
38+
boolean isFollowing, boolean isMyProfile) {
2939
this.email = email;
3040
this.customId = customId;
3141
this.userName = userName;
@@ -38,9 +48,14 @@ private UserProfileResponseDto(String email, String customId, String userName, S
3848
this.userBirthYear = userBirthYear;
3949
this.userBirthMonth = userBirthMonth;
4050
this.userBirthDay = userBirthDay;
51+
this.followersCount = followersCount;
52+
this.followingsCount = followingsCount;
53+
this.isMyProfile = isMyProfile;
54+
this.isFollowing = isFollowing;
4155
}
4256

43-
public static UserProfileResponseDto from(User user) {
57+
public static UserProfileResponseDto from(User user, int followersCount, int followingsCount, boolean isFollowing,
58+
boolean isMyProfile) {
4459
return UserProfileResponseDto.builder()
4560
.email(user.getEmail())
4661
.customId(user.getCustomId())
@@ -54,6 +69,10 @@ public static UserProfileResponseDto from(User user) {
5469
.userBirthYear(user.getUserBirth().getYear())
5570
.userBirthMonth(user.getUserBirth().getMonth())
5671
.userBirthDay(user.getUserBirth().getDay())
72+
.followersCount(followersCount)
73+
.followingsCount(followingsCount)
74+
.isMyProfile(isMyProfile)
75+
.isFollowing(isFollowing)
5776
.build();
5877
}
5978
}

src/main/java/com/leets/xcellentbe/domain/user/service/UserService.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.stereotype.Service;
55
import org.springframework.web.multipart.MultipartFile;
66

7-
import com.leets.xcellentbe.domain.article.domain.repository.ArticleRepository;
7+
import com.leets.xcellentbe.domain.follow.domain.repository.FollowRepository;
88
import com.leets.xcellentbe.domain.user.domain.User;
99
import com.leets.xcellentbe.domain.user.domain.repository.UserRepository;
1010
import com.leets.xcellentbe.domain.user.dto.UserProfileRequestDto;
@@ -27,7 +27,7 @@ public class UserService {
2727
private final PasswordEncoder passwordEncoder;
2828
private final JwtService jwtService;
2929
private final S3UploadService s3UploadService;
30-
private final ArticleRepository articleRepository;
30+
private final FollowRepository followRepository;
3131

3232
// 회원가입 메소드
3333
public String register(UserSignUpRequestDto userSignUpRequestDto) {
@@ -54,13 +54,25 @@ public String register(UserSignUpRequestDto userSignUpRequestDto) {
5454
// 본인 정보 조회 메소드
5555
public UserProfileResponseDto getProfile(HttpServletRequest request) {
5656
User user = getUser(request);
57-
return UserProfileResponseDto.from(user);
57+
58+
int followerCount = followRepository.countByFollowing(user);
59+
int followingCount = followRepository.countByFollower(user);
60+
61+
return UserProfileResponseDto.from(user, followerCount, followingCount, false, true);
5862
}
5963

6064
// 특정 사용자 정보 조회 메소드
61-
public UserProfileResponseDto getProfileWithoutToken(String customId) {
65+
public UserProfileResponseDto getProfileWithoutToken(String customId, HttpServletRequest request) {
66+
User myinfo = getUser(request);
6267
User user = userRepository.findByCustomId(customId).orElseThrow(UserNotFoundException::new);
63-
return UserProfileResponseDto.from(user);
68+
69+
boolean isMyProfile = myinfo.equals(user);
70+
boolean isFollowing = followRepository.findByFollowerAndFollowing(myinfo, user).isPresent();
71+
72+
int followerCount = followRepository.countByFollowing(user);
73+
int followingCount = followRepository.countByFollower(user);
74+
75+
return UserProfileResponseDto.from(user, followerCount, followingCount, isFollowing, isMyProfile);
6476
}
6577

6678
// 사용자 정보 수정 메소드

0 commit comments

Comments
 (0)