Skip to content

Commit

Permalink
Merge pull request #24 from Leets-Official/fix/#22/유저정보-조회-및-게시글-조회-수정
Browse files Browse the repository at this point in the history
[fix] 유저정보 조회 및 게시글 조회 수정
  • Loading branch information
ehs208 authored Nov 12, 2024
2 parents 9f60b4a + 7713520 commit ea3b662
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.leets.xcellentbe.domain.user.domain.User;

public interface ArticleRepository extends JpaRepository<Article, UUID> {
@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")
@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")
List<ArticlesWithMediaDto[]> findPostsByWriter(User user);

@Query("SELECT a FROM Article a WHERE a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.leets.xcellentbe.domain.article.dto;

import java.util.List;
import java.util.stream.Collectors;

import com.leets.xcellentbe.domain.article.domain.Article;
import com.leets.xcellentbe.domain.hashtag.domain.Hashtag;
Expand All @@ -15,28 +16,29 @@ public class ArticlesResponseDto {
private String writer;
private String content;
private Boolean isPinned;
private List<Hashtag> hashtags;
private Article rePost;
private List<String> hashtags;
// private Article rePost;
private List<String> filePath;

@Builder
private ArticlesResponseDto(String writer, String content, Boolean isPinned, List<Hashtag> hashtags,
Article rePost,
List<String> filePath) {
private ArticlesResponseDto(String writer, String content, Boolean isPinned, List<String> hashtags
, List<String> filePath) {
this.writer = writer;
this.content = content;
this.isPinned = isPinned;
this.hashtags = hashtags;
this.rePost = rePost;
// this.rePost = rePost;
this.filePath = filePath;
}

public static ArticlesResponseDto of(Article article, List<String> filePath) {
return ArticlesResponseDto.builder()
.writer(article.getWriter().getUserName())
.content(article.getContent())
.hashtags(article.getHashtags())
.rePost(article.getRePost())
.hashtags(article.getHashtags().stream()
.map(Hashtag::getContent)
.collect(Collectors.toList()))
// .rePost(article.getRePost())
.filePath(filePath)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface FollowRepository extends JpaRepository<Follow, Long> {
Page<Follow> findByFollower(User user, Pageable pageable);

Page<Follow> findByFollowing(User user, Pageable pageable);

int countByFollower(User user);

int countByFollowing(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public FollowInfoResponseDto(String customId, String userName) {
this.userName = userName;
}

public static FollowInfoResponseDto from(Follow follow) {
User user = follow.getFollowing();
public static FollowInfoResponseDto from(Follow follow, boolean isFollower) {
User user = isFollower ? follow.getFollower() : follow.getFollowing();
return FollowInfoResponseDto.builder()
.customId(user.getCustomId())
.userName(user.getUserName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public void followUser(FollowRequestDto requestDto, HttpServletRequest request)
throw new FollowOperationError();
}

if (user.equals(targetUser)) { // 자기 자신을 팔로우시 예외처리
throw new FollowOperationError();
}

Follow follow = Follow.create(user, targetUser);
followRepository.save(follow);
}
Expand Down Expand Up @@ -78,7 +82,7 @@ public Page<FollowInfoResponseDto> getFollowingList(String customId, int pageNo)
Pageable pageable = createPageable(pageNo);

return followRepository.findByFollower(user, pageable)
.map(FollowInfoResponseDto::from);
.map(follow -> FollowInfoResponseDto.from(follow, false));
}

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

return followRepository.findByFollowing(user, pageable)
.map(FollowInfoResponseDto::from);
.map(follow -> FollowInfoResponseDto.from(follow, true));
}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public ResponseEntity<GlobalResponseDto<String>> updateProfile(
@GetMapping("/info")
@Operation(summary = "프로필 조회", description = "특정 사용자의 프로필 내용을 조회합니다.")
public ResponseEntity<GlobalResponseDto<UserProfileResponseDto>> getProfileWithoutToken(
@RequestParam String customId) {
@RequestParam String customId, HttpServletRequest request) {
return ResponseEntity.status(HttpStatus.OK)
.body(GlobalResponseDto.success(userService.getProfileWithoutToken(customId)));
.body(GlobalResponseDto.success(userService.getProfileWithoutToken(customId, request)));
}

@PatchMapping("/profile-image")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.leets.xcellentbe.domain.user.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.leets.xcellentbe.domain.user.domain.User;

import lombok.Builder;
Expand All @@ -21,11 +22,20 @@ public class UserProfileResponseDto {
private int userBirthYear;
private int userBirthMonth;
private int userBirthDay;
private int followersCount;
private int followingsCount;

@JsonProperty("isFollowing")
private boolean isFollowing;

@JsonProperty("isMyProfile")
private boolean isMyProfile;

@Builder
private UserProfileResponseDto(String email, String customId, String userName, String profileImageUrl,
String backgroundProfileImageUrl, String phoneNumber, String description, String websiteUrl, String location,
int userBirthYear, int userBirthMonth, int userBirthDay) {
int userBirthYear, int userBirthMonth, int userBirthDay, int followersCount, int followingsCount,
boolean isFollowing, boolean isMyProfile) {
this.email = email;
this.customId = customId;
this.userName = userName;
Expand All @@ -38,9 +48,14 @@ private UserProfileResponseDto(String email, String customId, String userName, S
this.userBirthYear = userBirthYear;
this.userBirthMonth = userBirthMonth;
this.userBirthDay = userBirthDay;
this.followersCount = followersCount;
this.followingsCount = followingsCount;
this.isMyProfile = isMyProfile;
this.isFollowing = isFollowing;
}

public static UserProfileResponseDto from(User user) {
public static UserProfileResponseDto from(User user, int followersCount, int followingsCount, boolean isFollowing,
boolean isMyProfile) {
return UserProfileResponseDto.builder()
.email(user.getEmail())
.customId(user.getCustomId())
Expand All @@ -54,6 +69,10 @@ public static UserProfileResponseDto from(User user) {
.userBirthYear(user.getUserBirth().getYear())
.userBirthMonth(user.getUserBirth().getMonth())
.userBirthDay(user.getUserBirth().getDay())
.followersCount(followersCount)
.followingsCount(followingsCount)
.isMyProfile(isMyProfile)
.isFollowing(isFollowing)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.leets.xcellentbe.domain.article.domain.repository.ArticleRepository;
import com.leets.xcellentbe.domain.follow.domain.repository.FollowRepository;
import com.leets.xcellentbe.domain.user.domain.User;
import com.leets.xcellentbe.domain.user.domain.repository.UserRepository;
import com.leets.xcellentbe.domain.user.dto.UserProfileRequestDto;
Expand All @@ -27,7 +27,7 @@ public class UserService {
private final PasswordEncoder passwordEncoder;
private final JwtService jwtService;
private final S3UploadService s3UploadService;
private final ArticleRepository articleRepository;
private final FollowRepository followRepository;

// 회원가입 메소드
public String register(UserSignUpRequestDto userSignUpRequestDto) {
Expand All @@ -54,13 +54,25 @@ public String register(UserSignUpRequestDto userSignUpRequestDto) {
// 본인 정보 조회 메소드
public UserProfileResponseDto getProfile(HttpServletRequest request) {
User user = getUser(request);
return UserProfileResponseDto.from(user);

int followerCount = followRepository.countByFollowing(user);
int followingCount = followRepository.countByFollower(user);

return UserProfileResponseDto.from(user, followerCount, followingCount, false, true);
}

// 특정 사용자 정보 조회 메소드
public UserProfileResponseDto getProfileWithoutToken(String customId) {
public UserProfileResponseDto getProfileWithoutToken(String customId, HttpServletRequest request) {
User myinfo = getUser(request);
User user = userRepository.findByCustomId(customId).orElseThrow(UserNotFoundException::new);
return UserProfileResponseDto.from(user);

boolean isMyProfile = myinfo.equals(user);
boolean isFollowing = followRepository.findByFollowerAndFollowing(myinfo, user).isPresent();

int followerCount = followRepository.countByFollowing(user);
int followingCount = followRepository.countByFollower(user);

return UserProfileResponseDto.from(user, followerCount, followingCount, isFollowing, isMyProfile);
}

// 사용자 정보 수정 메소드
Expand Down

0 comments on commit ea3b662

Please sign in to comment.