Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#112 마이페이지 조회 API 구현 #113

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.aboutme.app.controller;

import com.example.aboutme.apiPayload.ApiResponse;
import com.example.aboutme.app.dto.MyPageResponse;
import com.example.aboutme.service.MemberService.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mypages")
@RequiredArgsConstructor
@Slf4j
public class MyPageController {

private final MemberService memberService;

/**
* [GET] /mypages
* @param memberId 멤버 식별자
* @return
*/
@GetMapping("")
public ApiResponse<MyPageResponse.GetMyPageDTO> getMyPage(@RequestHeader("member-id") Long memberId){

MyPageResponse.GetMyPageDTO getMyPageDTO = memberService.getMyPage(memberId);

log.info("마이페이지 조회: {}", memberId);

return ApiResponse.onSuccess(getMyPageDTO);
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/example/aboutme/app/dto/MyPageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.aboutme.app.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class MyPageResponse {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class GetMyPageDTO {
@JsonProperty("my_info")
private MyPageInfo myPageInfo;

@JsonProperty("insight")
private MyPageInsight myPageInsight;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MyPageInfo{
@JsonProperty("profile_name")
private String profileName;

@JsonProperty("space_name")
private String spaceName;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MyPageInsight{
@JsonProperty("profile_shared_num")
private int profileSharedNum;

@JsonProperty("space_shared_num")
private int spaceSharedNum;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/example/aboutme/converter/MemberConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.aboutme.converter;

import com.example.aboutme.app.dto.MyPageResponse;
import com.example.aboutme.app.dto.SocialInfoRequest;
import com.example.aboutme.domain.Member;
import com.example.aboutme.domain.constant.Social;
Expand All @@ -16,4 +17,20 @@ public static Member toMember(SocialInfoRequest.GoogleDTO googleDTO, Social soci
.jwtAccessToken(token).build();
}

public static MyPageResponse.GetMyPageDTO toGetMyPageDTO (String profileName, String spaceName, int profileSharedNum, int spaceSharedNum){
MyPageResponse.MyPageInfo myPageInfo = MyPageResponse.MyPageInfo.builder()
.profileName(profileName)
.spaceName(spaceName)
.build();

MyPageResponse.MyPageInsight myPageInsight = MyPageResponse.MyPageInsight.builder()
.profileSharedNum(profileSharedNum)
.spaceSharedNum(spaceSharedNum)
.build();

return MyPageResponse.GetMyPageDTO.builder()
.myPageInfo(myPageInfo)
.myPageInsight(myPageInsight)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.aboutme.domain.Profile;
import com.example.aboutme.domain.mapping.MemberProfile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

Expand All @@ -17,5 +19,17 @@ public interface MemberProfileRepository extends JpaRepository<MemberProfile, Lo

MemberProfile findByMemberAndProfile(Member member, Profile profile);
Boolean existsByMemberAndProfile(Member member, Profile profile);

/**
* 내 마이프로필 공유 현황 (내 마이프로필이 상대방의 보관함에 얼마나 저장되었는지)
* @param member 조회하려는 멤버
* @return 공유된 마이프로필 개수
*/
@Query("select count(*)" +
"from MemberProfile mp join Profile as p on mp.profile = p " +
"where p.member = :member")
Integer countSharedProfileByMember(@Param("member") Member member);

List<MemberProfile> findByMemberAndProfileInAndApprovedIsTrue(Member member, List<Profile> profileList);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.aboutme.repository;

import com.example.aboutme.app.dto.MyPageResponse;
import com.example.aboutme.domain.Member;
import com.example.aboutme.domain.constant.Social;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface MemberRepository extends JpaRepository<Member, Long> {
Member findByEmailAndSocial(String email, Social social);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.example.aboutme.domain.Space;
import com.example.aboutme.domain.mapping.MemberSpace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

Expand All @@ -13,4 +15,13 @@ public interface MemberSpaceRepository extends JpaRepository<MemberSpace, Long>

List<MemberSpace> findByMemberAndSpace_NicknameContaining(Member member, String keyword);

/**
* 내 마이스페이스 공유 현황 (내 마이스페이스가 상대방의 보관함에 얼마나 저장되었는지)
* @param member 조회하려는 멤버
* @return 공유된 마이스페이스 개수
*/
@Query("select count(*)" +
"from MemberSpace ms join Space s on ms.space = s " +
"where s.member = :member")
Integer countSharedSpaceByMember(@Param("member") Member member);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.example.aboutme.repository;

import com.example.aboutme.domain.Member;
import com.example.aboutme.domain.ProfileFeature;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ProfileFeatureRepository extends JpaRepository<ProfileFeature, Long> {
List<ProfileFeature> findByProfileKeyAndProfileValueContaining(String profileKey, String profileValue);

@Query("select pf.profileValue " +
"from Profile p " +
"join ProfileFeature pf on p = pf.profile " +
"where pf.profileKey = 'name' and p.member = :member " +
"order by p.isDefault desc, p.createdAt asc ")
List<String> findProfileFeature(@Param("member") Member member, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.example.aboutme.domain.Member;
import com.example.aboutme.domain.Profile;
import com.example.aboutme.domain.ProfileFeature;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.aboutme.service.MemberService;

import com.example.aboutme.app.dto.MyPageResponse;
import com.example.aboutme.domain.Member;
import org.springframework.stereotype.Service;

Expand All @@ -9,4 +10,10 @@ public interface MemberService {
Member findMember(Long memberId);
void deleteMember(Long memberId);

/**
* 마이페이지 조회
* @param memberId 멤버 식별자
* @return 마이프로필 정보
*/
MyPageResponse.GetMyPageDTO getMyPage(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.example.aboutme.apiPayload.code.status.ErrorStatus;
import com.example.aboutme.apiPayload.exception.GeneralException;
import com.example.aboutme.app.dto.MyPageResponse;
import com.example.aboutme.converter.MemberConverter;
import com.example.aboutme.domain.Member;
import com.example.aboutme.repository.MemberRepository;
import com.example.aboutme.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -14,6 +17,9 @@
public class MemberServiceImpl implements MemberService{

private final MemberRepository memberRepository;
private final ProfileFeatureRepository profileFeatureRepository;
private final MemberProfileRepository memberProfileRepository;
private final MemberSpaceRepository memberSpaceRepository;

public Member findMember(Long memberId){
return memberRepository.findById(memberId).orElseThrow(
Expand All @@ -26,4 +32,21 @@ public void deleteMember(Long memberId){
findMember(memberId);
memberRepository.deleteById(memberId);
}

/**
* 마이페이지 조회
* @param memberId 멤버 식별자
* @return 마이프로필 정보
*/
public MyPageResponse.GetMyPageDTO getMyPage(Long memberId){
Member member = findMember(memberId);

String profileName = profileFeatureRepository.findProfileFeature(member, PageRequest.of(0,1)).get(0);
Copy link
Contributor

@yueunfive yueunfive Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paging하신 이유가 궁금합니다.

@query("select pf.profileValue " +
"from Profile p " +
"join ProfileFeature pf on p = pf.profile " +
"where pf.profileKey = 'name' and p.member = :member " +
"order by p.isDefault desc, p.createdAt asc ")
List findProfileFeature(@param("member") Member member);
...
String profileName = profileFeatureRepository.findProfileFeature(member).get(0);

이렇게 페이지네이션 처리없이 조회했을 때와 비교하여 이점이 있는 건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 paging으로 처리하고싶진 않았는데,
profile feature가 여러개 나올 수 있는 상황에서 jpql로는 sql의 limit 키워드가 안먹어서
어쩔 수 없이 paging으로 처리했습니다ㅠ 혹시 더 좋은 방법이 있다면 알려주시면 감사하겠습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 확인했습니다. 저도 궁금해서 질문드린거라 더 좋은 방법은 모르겠네요😅

String spaceName = member.getSpace() != null ? member.getSpace().getNickname() : null;

int profileSharedNum = memberProfileRepository.countSharedProfileByMember(member);
int spaceSharedNum = memberSpaceRepository.countSharedSpaceByMember(member);

return MemberConverter.toGetMyPageDTO(profileName, spaceName, profileSharedNum, spaceSharedNum);
}
}
Loading