Skip to content

Commit

Permalink
Merge pull request moyeothon#20 from kduoh99/Test/moyeothon#19
Browse files Browse the repository at this point in the history
Test: 인증 객체 적용
  • Loading branch information
kduoh99 authored Nov 2, 2024
2 parents 2eb73c5 + 69866d8 commit 6f626ce
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -39,9 +38,10 @@ public class MemberController {
}
)
public RspTemplate<Void> completeProfile(
Principal principal,
@Valid @RequestBody ProfileReqDto reqDto) {

memberService.completeProfile(TEMP_MEMBER_ID, reqDto);
memberService.completeProfile(principal, reqDto);
return new RspTemplate<>(HttpStatus.CREATED, "프로필 완성 성공");
}

Expand All @@ -55,8 +55,8 @@ public RspTemplate<Void> completeProfile(
@ApiResponse(responseCode = "500", description = "서버 오류")
}
)
public RspTemplate<ProfileResDto> getProfile() {
ProfileResDto profile = memberService.getProfile(TEMP_MEMBER_ID);
public RspTemplate<ProfileResDto> getProfile(Principal principal) {
ProfileResDto profile = memberService.getProfile(principal);
return new RspTemplate<>(HttpStatus.OK, "프로필 조회 성공", profile);
}

Expand All @@ -72,9 +72,10 @@ public RspTemplate<ProfileResDto> getProfile() {
}
)
public RspTemplate<ProfileResDto> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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);
}
}
15 changes: 7 additions & 8 deletions src/main/java/com/hackathon/momento/team/api/TeamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -38,10 +37,10 @@ public class TeamController {
}
)
public RspTemplate<Void> saveTeamBuilding(
// Principal principal,
Principal principal,
@Valid @RequestBody TeamBuildingReqDto reqDto) {

teamService.saveTeamBuilding(TEMP_MEMBER_ID, reqDto);
teamService.saveTeamBuilding(principal, reqDto);
return new RspTemplate<>(HttpStatus.CREATED, "팀 빌딩 정보 저장 성공");
}

Expand Down Expand Up @@ -71,8 +70,8 @@ public RspTemplate<Void> executeTeamBuilding() {
@ApiResponse(responseCode = "500", description = "서버 오류")
}
)
public RspTemplate<List<TeamInfoResDto>> getTeamInfoProfile() {
List<TeamInfoResDto> teamProfile = teamService.getTeamInfoProfile(TEMP_MEMBER_ID);
public RspTemplate<List<TeamInfoResDto>> getTeamInfoProfile(Principal principal) {
List<TeamInfoResDto> teamProfile = teamService.getTeamInfoProfile(principal);
return new RspTemplate<>(HttpStatus.OK, "팀 정보 조회 성공", teamProfile);
}

Expand All @@ -86,8 +85,8 @@ public RspTemplate<List<TeamInfoResDto>> getTeamInfoProfile() {
@ApiResponse(responseCode = "500", description = "서버 오류")
}
)
public RspTemplate<String> checkDuplicateRequest() {
boolean isDuplicate = teamService.checkDuplicate(TEMP_MEMBER_ID);
public RspTemplate<String> checkDuplicateRequest(Principal principal) {
boolean isDuplicate = teamService.checkDuplicate(principal);
return new RspTemplate<>(HttpStatus.OK, String.valueOf(isDuplicate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -196,10 +195,8 @@ private List<TeamInfo> parseResToTeamInfo(String resContent, List<TeamBuilding>
return teamInfos;
}

public List<TeamInfoResDto> getTeamInfoProfile(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);

public List<TeamInfoResDto> getTeamInfoProfile(Principal principal) {
Member member = getMemberByPrincipal(principal);
List<TeamBuilding> completedTeams = teamBuildingRepository.findByMemberAndStatus(member,
Status.COMPLETED);

Expand All @@ -209,10 +206,14 @@ public List<TeamInfoResDto> 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);
}
}

0 comments on commit 6f626ce

Please sign in to comment.