Skip to content

Commit

Permalink
feat: 사용자 이메일로 접근
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoon-Jemin committed Feb 10, 2024
1 parent 0b4c49c commit a6ef6ae
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aromanticcat.umcproject.converter;

import aromanticcat.umcproject.entity.FriendStatus;
import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.repository.LetterBoxRepository;
import aromanticcat.umcproject.web.dto.Friend.FriendResponseDTO;
import aromanticcat.umcproject.entity.Friend;
Expand Down Expand Up @@ -29,4 +31,19 @@ public static FriendResponseDTO.WaitingFriendDTO toWaitingFriendDTO(Friend frien
.build();
}

public static Friend toFriend(Member fromMember, Member toMember, boolean isSend){

return Friend.builder()
.friendName(toMember.getNickname())
.friendId(toMember.getId())
.member(fromMember)
.fromMemberId(fromMember.getId())
.fromMemberName(fromMember.getNickname())
.toMemberId(toMember.getId())
.toMemberName(toMember.getNickname())
.friendStatus(FriendStatus.WAITING)
.isFrom(isSend) // true이면 fromMember가 false이면 toMember가 친구 요청을 보냄
.build();
}

}
2 changes: 1 addition & 1 deletion src/main/java/aromanticcat/umcproject/entity/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Friend extends BaseEntity {

private String friendName;

private Long friendId; // 친구 아이디
private Long friendId; // 친구 아이디 (우편함 번호)

@JoinColumn(name = "member_id")
@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
public interface FriendRepository extends JpaRepository<Friend, Long> {

@Query("SELECT f FROM Friend f WHERE f.member.id = :memberId AND f.friendStatus IN (:friendStatus)")
Page<Friend> findFriendByMemberIdAndFriendStatus(Long memberId, Pageable pageable, Set<FriendStatus> friendStatus);
Page<Friend> findFriendByMemberAndFriendStatus(Member member, Pageable pageable, Set<FriendStatus> friendStatus);

Page<Friend> findFriendByMemberIdAndFriendStatus(Long memberId, Pageable pageable, FriendStatus friendStatus);
Page<Friend> findFriendByMemberAndFriendStatus(Member member, Pageable pageable, FriendStatus friendStatus);

List<Friend> findFriendByMemberIdAndFriendName(Long memberId, String friendName);
List<Friend> findFriendByMemberAndFriendName(Member member, String friendName);

List<Friend> findFriendByMemberIdAndFriendId(Long memberId, Long friendId);
List<Friend> findFriendByMemberAndFriendId(Member member, Long friendId);

Friend findByMemberIdAndFriendId(Long memberId, Long friendId);
Friend findByMemberAndFriendId(Member member, Long friendId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.entity.MemberMission;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface MemberMissionRepository extends JpaRepository<MemberMission, Long> {

List<MemberMission> findByMemberId(Long memberId);
MemberMission findByMemberIdAndMissionId(Long memberId, Long missionId);
List<MemberMission> findByMember(Member member);
MemberMission findByMemberAndMissionId(Member member, Long missionId);

Boolean existsByMemberIdAndMissionId(Long memberId, Long missionId);
Boolean existsByMemberAndMissionId(Member member, Long missionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

public interface FriendCommandService {

void requestFriendship(Long memberId,Long toMemberLetterBoxId);
void requestFriendship(String userEmail,Long toMemberLetterBoxId);

void approveFriendship(Long memberId, Long friendId);
void approveFriendship(String userEmail, Long friendId);

void rejectFriendship(Long memberId, Long friendId);
void rejectFriendship(String userEmail, Long friendId);

void setCloseFriend(Long memberId, Long friendId);
void setCloseFriend(String userEmail, Long friendId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aromanticcat.umcproject.service.FriendService;

import aromanticcat.umcproject.converter.FriendConverter;
import aromanticcat.umcproject.entity.Friend;
import aromanticcat.umcproject.entity.FriendStatus;
import aromanticcat.umcproject.entity.Member;
Expand All @@ -18,41 +19,26 @@ public class FriendCommandServiceImpl implements FriendCommandService {
private final MemberRepository memberRepository;
private final FriendRepository friendRepository;

public Member getMember(String email){
return memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다. ID: " + email));
}

@Override
@Transactional
public void requestFriendship(Long memberId, Long toMemberId) {
public void requestFriendship(String userEmail, Long toMemberId) {

// 친구 요청을 보내는 사용자
Member fromMember = memberRepository.findById(memberId).orElse(null);
Member fromMember = getMember(userEmail);

// 친구 요청을 받는 사용자
Member toMember = memberRepository.findById(toMemberId).orElse(null);

// dto의 정보를 기반으로 새로운 친구 객체 생성1 (fromMember 기준)
Friend newFriend1 = Friend.builder()
.friendName(toMember.getNickname())
.friendId(toMemberId)
.member(fromMember)
.fromMemberId(fromMember.getId())
.fromMemberName(fromMember.getNickname())
.toMemberId(toMember.getId())
.toMemberName(toMember.getNickname())
.friendStatus(FriendStatus.WAITING)
.isFrom(true) // true이면 fromMember가 친구 요청을 보냄
.build();

// dto의 정보를 기반으로 새로운 친구 객체 생성2 (toMember 기준)
Friend newFriend2 = Friend.builder()
.friendName(fromMember.getNickname())
.friendId(fromMember.getId())
.member(toMember)
.fromMemberId(toMember.getId())
.fromMemberName(toMember.getNickname())
.toMemberId(fromMember.getId())
.toMemberName(fromMember.getNickname())
.friendStatus(FriendStatus.WAITING)
.isFrom(false) // false이면 toMember가 친구 요청을 보냄
.build();
// 새로운 친구 객체 생성1 (fromMember 기준)
Friend newFriend1 = FriendConverter.toFriend(fromMember, toMember, true);

// 새로운 친구 객체 생성2 (toMember 기준)
Friend newFriend2 = FriendConverter.toFriend(toMember, fromMember, false);

// 각 사용자의 친구 리스트에 새로 만든 친구 객체 추가
toMember.getFriends().add(newFriend1);
Expand All @@ -68,9 +54,11 @@ public void requestFriendship(Long memberId, Long toMemberId) {

@Override
@Transactional
public void approveFriendship(Long memberId, Long friendId) { // 친구 요청을 승인한 경우
public void approveFriendship(String userEmail, Long friendId) { // 친구 요청을 승인한 경우

Friend newFriend = friendRepository.findByMemberIdAndFriendId(memberId, friendId);
Member member = getMember(userEmail);

Friend newFriend = friendRepository.findByMemberAndFriendId(member, friendId);
Friend counterpart = friendRepository.findById(newFriend.getCounterpartId()).orElse(null);

newFriend.changeFriendStatus(FriendStatus.APPROVED);
Expand All @@ -80,9 +68,11 @@ public void approveFriendship(Long memberId, Long friendId) { // 친구 요청

@Override
@Transactional
public void rejectFriendship(Long memberId, Long friendId) {
public void rejectFriendship(String userEmail, Long friendId) {

Member member = getMember(userEmail);

Friend newFriend = friendRepository.findByMemberIdAndFriendId(memberId, friendId);
Friend newFriend = friendRepository.findByMemberAndFriendId(member, friendId);
Friend counterpart = friendRepository.findById(newFriend.getCounterpartId()).orElse(null);

newFriend.changeFriendStatus(FriendStatus.REJECTED);
Expand All @@ -92,9 +82,11 @@ public void rejectFriendship(Long memberId, Long friendId) {

@Override
@Transactional
public void setCloseFriend(Long memberId, Long friendId) {
public void setCloseFriend(String userEmail, Long friendId) {

Member member = getMember(userEmail);

Friend friend = friendRepository.findByMemberIdAndFriendId(memberId, friendId);
Friend friend = friendRepository.findByMemberAndFriendId(member, friendId);

friend.changeFriendStatus(FriendStatus.CLOSE_FRIEND);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
public interface FriendQueryService {


List<Friend> getFriendReceivedList(Long memberId);
List<Friend> getFriendReceivedList(String userEmail);

List<Friend> getFriendRequestedList(Long memberId);
List<Friend> getFriendRequestedList(String userEmail);

List<FriendResponseDTO.FriendInfoDTO> findFriendList(Long memberId, Integer page);
List<FriendResponseDTO.FriendInfoDTO> findFriendList(String userEmail, Integer page);

List<FriendResponseDTO.FriendInfoDTO> findCloseFriendList(Long memberId, Integer page);
List<FriendResponseDTO.FriendInfoDTO> findCloseFriendList(String userEmail, Integer page);

List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendName(Long memberId, String friendName);
List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendName(String userEmail, String friendName);

List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendId(Long memberId, Long friendId);
List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendId(String userEmail, Long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public class FriendQueryServiceImpl implements FriendQueryService {
private final MemberRepository memberRepository;
private final FriendRepository friendRepository;

public Member getMember(String email){
return memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다. ID: " + email));
}

@Override
@Transactional
public List<Friend> getFriendReceivedList(Long memberId) { // 요청 받은 친구 정보만 가져오기
public List<Friend> getFriendReceivedList(String userEmail) { // 요청 받은 친구 정보만 가져오기

// 해당 API를 요청한 사용자 객체를 가져온 뒤 해당 객체의 친구 리스트를 가져옴
Member member = memberRepository.findById(memberId).orElse(null);
Member member = getMember(userEmail);
List<Friend> friendList = member.getFriends();
List<Friend> results = new ArrayList<>();

Expand All @@ -46,10 +51,10 @@ public List<Friend> getFriendReceivedList(Long memberId) { // 요청 받은

@Override
@Transactional
public List<Friend> getFriendRequestedList(Long memberId) {
public List<Friend> getFriendRequestedList(String userEmail) {

// 해당 API를 요청한 사용자 객체를 가져온 뒤 해당 객체의 친구 리스트를 가져옴
Member member = memberRepository.findById(memberId).orElse(null);
Member member = getMember(userEmail);
List<Friend> friendList = member.getFriends();
List<Friend> results = new ArrayList<>();

Expand All @@ -65,7 +70,9 @@ public List<Friend> getFriendRequestedList(Long memberId) {

@Override
@Transactional
public List<FriendResponseDTO.FriendInfoDTO> findFriendList(Long memberId, Integer page) {
public List<FriendResponseDTO.FriendInfoDTO> findFriendList(String userEmail, Integer page) {

Member member = getMember(userEmail);

// page는 페이지의 번호, 12는 한 페이지에 보여줄 친구의 수
Pageable pageable = PageRequest.of(page,12);
Expand All @@ -75,7 +82,7 @@ public List<FriendResponseDTO.FriendInfoDTO> findFriendList(Long memberId, Integ
friendStatus.add(FriendStatus.APPROVED);
friendStatus.add(FriendStatus.CLOSE_FRIEND);

Page<Friend> friendPage = friendRepository.findFriendByMemberIdAndFriendStatus(memberId, pageable, friendStatus);
Page<Friend> friendPage = friendRepository.findFriendByMemberAndFriendStatus(member, pageable, friendStatus);

List<Friend> friendList = friendPage.getContent();

Expand All @@ -89,12 +96,14 @@ public List<FriendResponseDTO.FriendInfoDTO> findFriendList(Long memberId, Integ

@Override
@Transactional
public List<FriendResponseDTO.FriendInfoDTO> findCloseFriendList(Long memberId, Integer page) {
public List<FriendResponseDTO.FriendInfoDTO> findCloseFriendList(String userEmail, Integer page) {

Member member = getMember(userEmail);

// page는 페이지의 번호, 12는 한 페이지에 보여줄 친구의 수
Pageable pageable = PageRequest.of(page,12);

Page<Friend> friendPage = friendRepository.findFriendByMemberIdAndFriendStatus(memberId, pageable, FriendStatus.CLOSE_FRIEND);
Page<Friend> friendPage = friendRepository.findFriendByMemberAndFriendStatus(member, pageable, FriendStatus.CLOSE_FRIEND);

List<Friend> friendList = friendPage.getContent();

Expand All @@ -107,9 +116,11 @@ public List<FriendResponseDTO.FriendInfoDTO> findCloseFriendList(Long memberId,
}

@Override
public List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendName(Long memberId, String friendName) { // 중복된 이름이 있을 수 있으므로 List 반환
public List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendName(String userEmail, String friendName) { // 중복된 이름이 있을 수 있으므로 List 반환

Member member = getMember(userEmail);

List<Friend> friendList = friendRepository.findFriendByMemberIdAndFriendName(memberId, friendName);
List<Friend> friendList = friendRepository.findFriendByMemberAndFriendName(member, friendName);

List<FriendResponseDTO.FriendInfoDTO> friendInfoDTOList = friendList.stream()
.map(FriendConverter::toFriendInfoDTO)
Expand All @@ -120,9 +131,11 @@ public List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendName(Long memberId
}

@Override
public List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendId(Long memberId, Long friendId) {
public List<FriendResponseDTO.FriendInfoDTO> getFriendbyFriendId(String userEmail, Long friendId) {

Member member = getMember(userEmail);

List<Friend> friendList = friendRepository.findFriendByMemberIdAndFriendId(memberId, friendId);
List<Friend> friendList = friendRepository.findFriendByMemberAndFriendId(member, friendId);

List<FriendResponseDTO.FriendInfoDTO> friendInfoDTOList = friendList.stream()
.map(FriendConverter::toFriendInfoDTO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aromanticcat.umcproject.service.MissionService;

import aromanticcat.umcproject.entity.Member;

public interface MissionCommandService {
void stepCompleted(Long memberId, Long missionId);
void stepCompleted(String userEmail, Long missionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ public class MissionCommandServiceImpl implements MissionCommandService{
private final MemberRepository memberRepository;
private final MissionRepository missionRepository;

public Member getMember(String email){
return memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다. ID: " + email));
}

@Override
@Transactional
public void stepCompleted(Long memberId, Long missionId) { // 미션 안에서 step 하나를 완료
public void stepCompleted(String userEmail, Long missionId) { // 미션 안에서 step 하나를 완료

Member member = getMember(userEmail);

Boolean isExist = memberMissionRepository.existsByMemberIdAndMissionId(memberId, missionId);
Member member = memberRepository.findById(memberId).orElse(null);
Boolean isExist = memberMissionRepository.existsByMemberAndMissionId(member, missionId);
Mission mission = missionRepository.findById(missionId).orElse(null);

if (!isExist) { // 처음 시작된 미션인 경우
MemberMission newMemberMission = MissionConverter.toMemberMission(member, mission);
memberMissionRepository.save(newMemberMission);
}

MemberMission memberMission = memberMissionRepository.findByMemberIdAndMissionId(memberId, missionId);
MemberMission memberMission = memberMissionRepository.findByMemberAndMissionId(member, missionId);

if (memberMission.getMissionStatus() == MissionStatus.COMPLETED) {
throw new RuntimeException("미션이 이미 완료되었습니다. 미션 ID: " + missionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

public interface MissionQueryService {
List<MissionResponseDTO.MissionInfoDTO> findMissionList(Long memberId);
List<MissionResponseDTO.MissionInfoDTO> findMissionList(String userEmail);

MissionResponseDTO.MissionDetailsDTO findMissionDetails(Long missionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ public class MissionQueryServiceImpl implements MissionQueryService{

private final MissionRepository missionRepository;
private final MemberMissionRepository memberMissionRepository;
private final MemberRepository memberRepository;

public Member getMember(String email){
return memberRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다. ID: " + email));
}

@Override
@Transactional
public List<MissionResponseDTO.MissionInfoDTO> findMissionList(Long memberId) {
public List<MissionResponseDTO.MissionInfoDTO> findMissionList(String userEmail) {

Member member = getMember(userEmail);

// 사용자가 참여한 미션 목록 조회
List<MemberMission> memberMissionList = memberMissionRepository.findByMemberId(memberId);
List<MemberMission> memberMissionList = memberMissionRepository.findByMember(member);

// 모든 미션 목록 조회
List<Mission> allMissionList = missionRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class RandomNicknameService {
public String generateRandomNickname() {
Random random = new Random();
String adjective = ADJECTIVES[random.nextInt(ADJECTIVES.length)];
String animal = ANIMALS[random.nextInt(ANIMALS.length)]
;
String animal = ANIMALS[random.nextInt(ANIMALS.length)];
return adjective + " " + animal;
}
}
Loading

0 comments on commit a6ef6ae

Please sign in to comment.