From 69866d8ae8f466ab9ab5908e491314e2cb4ed2ff Mon Sep 17 00:00:00 2001 From: Kang Duoh Date: Sun, 3 Nov 2024 03:46:33 +0900 Subject: [PATCH] =?UTF-8?q?Test:=20=EC=9D=B8=EC=A6=9D=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../momento/member/api/MemberController.java | 13 +++---- .../member/application/MemberService.java | 34 ++++++++----------- .../momento/team/api/TeamController.java | 15 ++++---- .../momento/team/application/TeamService.java | 25 +++++++------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/hackathon/momento/member/api/MemberController.java b/src/main/java/com/hackathon/momento/member/api/MemberController.java index 028fae3..3c1d5fb 100644 --- a/src/main/java/com/hackathon/momento/member/api/MemberController.java +++ b/src/main/java/com/hackathon/momento/member/api/MemberController.java @@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.security.Principal; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; @@ -24,8 +25,6 @@ @Tag(name = "사용자 정보", description = "사용자 정보를 담당하는 API 그룹") public class MemberController { - private static final Long TEMP_MEMBER_ID = 1L; // 테스트용 임시 사용자 ID - private final MemberService memberService; @PutMapping("/complete-profile") @@ -39,9 +38,10 @@ public class MemberController { } ) public RspTemplate completeProfile( + Principal principal, @Valid @RequestBody ProfileReqDto reqDto) { - memberService.completeProfile(TEMP_MEMBER_ID, reqDto); + memberService.completeProfile(principal, reqDto); return new RspTemplate<>(HttpStatus.CREATED, "프로필 완성 성공"); } @@ -55,8 +55,8 @@ public RspTemplate completeProfile( @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public RspTemplate getProfile() { - ProfileResDto profile = memberService.getProfile(TEMP_MEMBER_ID); + public RspTemplate getProfile(Principal principal) { + ProfileResDto profile = memberService.getProfile(principal); return new RspTemplate<>(HttpStatus.OK, "프로필 조회 성공", profile); } @@ -72,9 +72,10 @@ public RspTemplate getProfile() { } ) public RspTemplate updateProfile( + Principal principal, @Valid @RequestBody UpdateProfileReqDto reqDto) { - ProfileResDto updatedProfile = memberService.updateProfile(TEMP_MEMBER_ID, reqDto); + ProfileResDto updatedProfile = memberService.updateProfile(principal, reqDto); return new RspTemplate<>(HttpStatus.OK, "프로필 수정 성공", updatedProfile); } } diff --git a/src/main/java/com/hackathon/momento/member/application/MemberService.java b/src/main/java/com/hackathon/momento/member/application/MemberService.java index d1d8bcb..fbda086 100644 --- a/src/main/java/com/hackathon/momento/member/application/MemberService.java +++ b/src/main/java/com/hackathon/momento/member/application/MemberService.java @@ -7,10 +7,13 @@ import com.hackathon.momento.member.domain.repository.MemberRepository; import com.hackathon.momento.member.exception.FirstLoginOnlyException; import com.hackathon.momento.member.exception.MemberNotFoundException; +import java.security.Principal; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +@Slf4j @Service @Transactional(readOnly = true) @RequiredArgsConstructor @@ -19,10 +22,8 @@ public class MemberService { private final MemberRepository memberRepository; @Transactional - public void completeProfile(Long memberId, ProfileReqDto reqDto) { -// Member member = getMemberByPrincipal(principal); - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); + public void completeProfile(Principal principal, ProfileReqDto reqDto) { + Member member = getMemberByPrincipal(principal); if (!member.isFirstLogin()) { throw new FirstLoginOnlyException(); @@ -32,27 +33,22 @@ public void completeProfile(Long memberId, ProfileReqDto reqDto) { memberRepository.save(member); } - public ProfileResDto getProfile(Long memberId) { -// Member member = getMemberByPrincipal(principal); - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); - + public ProfileResDto getProfile(Principal principal) { + Member member = getMemberByPrincipal(principal); return ProfileResDto.from(member); } @Transactional - public ProfileResDto updateProfile(Long memberId, UpdateProfileReqDto reqDto) { -// Member member = getMemberByPrincipal(principal); - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); - + public ProfileResDto updateProfile(Principal principal, UpdateProfileReqDto reqDto) { + Member member = getMemberByPrincipal(principal); member.updateProfile(reqDto.name(), reqDto.stack(), reqDto.persona(), reqDto.ability()); + return ProfileResDto.from(member); } -// private Member getMemberByPrincipal(Principal principal) { -// Long memberId = Long.parseLong(principal.getName()); -// return memberRepository.findById(memberId) -// .orElseThrow(MemberNotFoundException::new); -// } + private Member getMemberByPrincipal(Principal principal) { + Long memberId = Long.parseLong(principal.getName()); + return memberRepository.findById(memberId) + .orElseThrow(MemberNotFoundException::new); + } } diff --git a/src/main/java/com/hackathon/momento/team/api/TeamController.java b/src/main/java/com/hackathon/momento/team/api/TeamController.java index 07d89f1..e8d763d 100644 --- a/src/main/java/com/hackathon/momento/team/api/TeamController.java +++ b/src/main/java/com/hackathon/momento/team/api/TeamController.java @@ -8,6 +8,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.security.Principal; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @@ -23,8 +24,6 @@ @Tag(name = "팀", description = "팀을 담당하는 API 그룹") public class TeamController { - private static final Long TEMP_MEMBER_ID = 1L; // 테스트용 임시 사용자 ID - private final TeamService teamService; @PostMapping("/building") @@ -38,10 +37,10 @@ public class TeamController { } ) public RspTemplate saveTeamBuilding( -// Principal principal, + Principal principal, @Valid @RequestBody TeamBuildingReqDto reqDto) { - teamService.saveTeamBuilding(TEMP_MEMBER_ID, reqDto); + teamService.saveTeamBuilding(principal, reqDto); return new RspTemplate<>(HttpStatus.CREATED, "팀 빌딩 정보 저장 성공"); } @@ -71,8 +70,8 @@ public RspTemplate executeTeamBuilding() { @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public RspTemplate> getTeamInfoProfile() { - List teamProfile = teamService.getTeamInfoProfile(TEMP_MEMBER_ID); + public RspTemplate> getTeamInfoProfile(Principal principal) { + List teamProfile = teamService.getTeamInfoProfile(principal); return new RspTemplate<>(HttpStatus.OK, "팀 정보 조회 성공", teamProfile); } @@ -86,8 +85,8 @@ public RspTemplate> getTeamInfoProfile() { @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public RspTemplate checkDuplicateRequest() { - boolean isDuplicate = teamService.checkDuplicate(TEMP_MEMBER_ID); + public RspTemplate checkDuplicateRequest(Principal principal) { + boolean isDuplicate = teamService.checkDuplicate(principal); return new RspTemplate<>(HttpStatus.OK, String.valueOf(isDuplicate)); } } diff --git a/src/main/java/com/hackathon/momento/team/application/TeamService.java b/src/main/java/com/hackathon/momento/team/application/TeamService.java index 4c00253..8cf13d1 100644 --- a/src/main/java/com/hackathon/momento/team/application/TeamService.java +++ b/src/main/java/com/hackathon/momento/team/application/TeamService.java @@ -14,6 +14,7 @@ import com.hackathon.momento.team.domain.repository.TeamBuildingRepository; import com.hackathon.momento.team.domain.repository.TeamInfoRepository; import com.hackathon.momento.team.exception.TeamBuildingConflictException; +import java.security.Principal; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -47,10 +48,8 @@ public class TeamService { private String promptTemplate; @Transactional - public void saveTeamBuilding(Long memberId, TeamBuildingReqDto reqDto) { -// Long memberId = Long.parseLong(principal.getName()); - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); + public void saveTeamBuilding(Principal principal, TeamBuildingReqDto reqDto) { + Member member = getMemberByPrincipal(principal); if (teamBuildingRepository.existsByMemberAndStatus(member, Status.PENDING)) { throw new TeamBuildingConflictException(); @@ -196,10 +195,8 @@ private List parseResToTeamInfo(String resContent, List return teamInfos; } - public List getTeamInfoProfile(Long memberId) { - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); - + public List getTeamInfoProfile(Principal principal) { + Member member = getMemberByPrincipal(principal); List completedTeams = teamBuildingRepository.findByMemberAndStatus(member, Status.COMPLETED); @@ -209,10 +206,14 @@ public List getTeamInfoProfile(Long memberId) { .collect(Collectors.toList()); } - public boolean checkDuplicate(Long memberId) { - Member member = memberRepository.findById(memberId) - .orElseThrow(MemberNotFoundException::new); - + public boolean checkDuplicate(Principal principal) { + Member member = getMemberByPrincipal(principal); return teamBuildingRepository.existsByMemberAndStatus(member, Status.PENDING); } + + private Member getMemberByPrincipal(Principal principal) { + Long memberId = Long.parseLong(principal.getName()); + return memberRepository.findById(memberId) + .orElseThrow(MemberNotFoundException::new); + } } \ No newline at end of file