Skip to content

Commit

Permalink
Merge pull request #907 from woowacourse-teams/BE/feature/#905-pair_add
Browse files Browse the repository at this point in the history
[BE] 페어 연동할 때 자신의 깃허브 id로 연동시 예외 반환
  • Loading branch information
reddevilmidzy authored Oct 24, 2024
2 parents 426cfa4 + 5467833 commit 8a49863
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public ResponseEntity<MemberReadResponse> getMember(@CookieValue(SIGN_IN_COOKIE_
}

@GetMapping("/member/exists")
public ResponseEntity<MemberNameResponse> existsMember(@RequestParam("user_id") String userId) {
final Member member = memberService.findMember(userId);
public ResponseEntity<MemberNameResponse> existsMember(
@CookieValue(SIGN_IN_COOKIE_NAME) final String token,
@RequestParam("user_id") String userId
) {

final Member member = memberService.checkAndFindMember(token, userId);
final MemberNameResponse response = new MemberNameResponse(member.getUsername());

return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import site.coduo.common.controller.response.ApiErrorResponse;
import site.coduo.member.controller.error.MemberApiError;
import site.coduo.member.exception.ExternalApiCallException;
import site.coduo.member.exception.InvalidMemberAddException;
import site.coduo.member.exception.MemberNotFoundException;

@Slf4j
Expand All @@ -25,6 +26,14 @@ public ResponseEntity<ApiErrorResponse> handlerMemberNotFoundException(final Mem
.body(new ApiErrorResponse(MemberApiError.MEMBER_NOT_FOUND_ERROR.getMessage()));
}

@ExceptionHandler(InvalidMemberAddException.class)
public ResponseEntity<ApiErrorResponse> handlerInvalidMemberAddException(final InvalidMemberAddException e) {
log.warn(e.getMessage());

return ResponseEntity.status(MemberApiError.INVALID_ADD_MEMBER_ERROR.getHttpStatus())
.body(new ApiErrorResponse(MemberApiError.INVALID_ADD_MEMBER_ERROR.getMessage()));
}

@ExceptionHandler(ExternalApiCallException.class)
public ResponseEntity<ApiErrorResponse> handlerExternalApiCallFailureException(final ExternalApiCallException e) {
log.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ ResponseEntity<Void> deleteMember(
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = MemberNameResponse.class)))
@ApiResponse(responseCode = "404", description = "회원이 존재하지 않음",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ApiErrorResponse.class)))
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ApiErrorResponse.class)))
ResponseEntity<MemberNameResponse> existsMember(
@Parameter(
in = ParameterIn.COOKIE,
name = "coduo_whoami",
description = "사용자가 인증에 성공하면 서버에서 발급하는 쿠키",
schema = @Schema(type = "string")
) String token,
@Parameter(
in = ParameterIn.QUERY,
name = "user_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum MemberApiError {

AUTHENTICATION_ERROR(HttpStatus.UNAUTHORIZED, "인증되지 않은 접근입니다."),
MEMBER_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."),
INVALID_ADD_MEMBER_ERROR(HttpStatus.BAD_REQUEST, "자신의 아이디로 페어 정보 연동을 할 수 없습니다."),
API_CALL_FAILURE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "외부 API와 상호작용 중 실패했습니다.");

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ default List<Member> findAll() {
default Member fetchByUserId(final String userId) {

return findByUserIdAndDeletedAtIsNull(userId)
.orElseThrow(() -> new MemberNotFoundException(String.format("%s는 찾을 수 없는 회원 아이디입니다.", userId)));
.orElseThrow(() -> new MemberNotFoundException(String.format("%s는(은) 찾을 수 없는 회원 아이디입니다.", userId)));
}

default Member fetchByLoginId(final String loginId) {

return findByLoginIdAndDeletedAtIsNull(loginId)
.orElseThrow(() -> new MemberNotFoundException(String.format("%s는 찾을 수 없는 회원입니다.", loginId)));
.orElseThrow(() -> new MemberNotFoundException(String.format("%s는(은) 찾을 수 없는 회원입니다.", loginId)));
}

boolean existsByUserIdAndDeletedAtIsNull(String userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.coduo.member.exception;

public class InvalidMemberAddException extends MemberException {

public InvalidMemberAddException(final String message) {
super(message);
}
}
12 changes: 12 additions & 0 deletions backend/src/main/java/site/coduo/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import site.coduo.member.client.dto.GithubUserResponse;
import site.coduo.member.domain.Member;
import site.coduo.member.domain.repository.MemberRepository;
import site.coduo.member.exception.InvalidMemberAddException;
import site.coduo.member.infrastructure.http.Bearer;
import site.coduo.member.infrastructure.security.JwtProvider;
import site.coduo.member.service.dto.member.MemberReadResponse;
Expand Down Expand Up @@ -57,4 +58,15 @@ public void deleteMember(final String token) {

member.delete();
}

public Member checkAndFindMember(final String token, final String userId) {
final Member loginedMember = findMemberByCredential(token);
final Member pairMember = findMember(userId);

if (loginedMember.equals(pairMember)) {
throw new InvalidMemberAddException("자신의 아이디로 페어 정보 연동을 할 수 없습니다.");
}

return pairMember;
}
}

0 comments on commit 8a49863

Please sign in to comment.