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

코드 리뷰~ #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions src/main/java/kr/where/backend/join/JoinController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kr.where.backend.join;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import kr.where.backend.api.exception.JsonException;
import kr.where.backend.auth.authUser.AuthUser;
import kr.where.backend.auth.authUser.AuthUserInfo;
import kr.where.backend.join.swagger.JoinApiDocs;
import kr.where.backend.jwt.dto.ResponseRefreshTokenDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v3/join")
public class JoinController implements JoinApiDocs {
private final JoinService joinService;

@PostMapping("")
public ResponseEntity<ResponseRefreshTokenDTO> join(@AuthUserInfo final AuthUser authUser) {

return ResponseEntity.ok(joinService.join(authUser));
}
}
47 changes: 47 additions & 0 deletions src/main/java/kr/where/backend/join/JoinService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kr.where.backend.join;

import kr.where.backend.api.HaneApiService;
import kr.where.backend.api.json.CadetPrivacy;
import kr.where.backend.auth.authUser.AuthUser;
import kr.where.backend.jwt.dto.ResponseRefreshTokenDTO;
import kr.where.backend.join.exception.JoinException;
import kr.where.backend.jwt.JwtService;
import kr.where.backend.member.Member;
import kr.where.backend.member.MemberService;
import kr.where.backend.member.exception.MemberException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class JoinService {
private static final String TOKEN_HANE = "hane";
private final MemberService memberService;
private final HaneApiService haneApiService;
private final JwtService jwtService;

@Transactional
public ResponseRefreshTokenDTO join(final AuthUser authUser) {
final Member member = memberService.findOne(authUser.getIntraId())
.orElseThrow(MemberException.NoMemberException::new);
if (member.isAgree()) {
throw new JoinException.DuplicatedJoinMember();
}
memberService.createAgreeMember(
CadetPrivacy
.builder()
Copy link
Collaborator

Choose a reason for hiding this comment

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

아직 어느 상황에서 빌더 패턴을 사용하는게 좋은지 잘 모르겠습니다.
보통 객체를 생성 할 때 생성자에서 사용하는것보다 가독성이 좋아서 사용하는걸까요????

Copy link
Contributor Author

Choose a reason for hiding this comment

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

음 많은이유가 있는데, 개인의 취향이라고 생각하면 편합니다
빌더 패턴을 쓰는 이유는 생성자를 사용시 변수를 지정하지 않으면 null로 채우주는 편리성 때문에 사용하는데
저같은 경우에는 가독성 측면에 좋아보여서 사용합니다.

.build(),
haneApiService
.getHaneInfo(member.getIntraName(), TOKEN_HANE)
Copy link
Collaborator

@leeesooha leeesooha Oct 13, 2024

Choose a reason for hiding this comment

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

TOKEN_HANE의 값이 "hane" 로 설정되어있습니다. 그런데 TOKEN_HANE를 쓰는곳을 계속 따라 들어가니 아래 코드가 나왔습니다.

    public static HttpEntity<MultiValueMap<String, String>> requestHaneInfo(final String token) {
        final HttpHeaders headers = new HttpHeaders();

        headers.add(HttpHeaders.AUTHORIZATION, BEARER + token);
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

        final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

        return new HttpEntity<>(params, headers);
    }
    
    
    
위 코드 4번째 줄에서 "BEARER + token"이라고 되있는데 token은 "hane" string이 그냥 들어 가는것 같습니다. 
제 이해로는 "BEARER + 진짜 토큰"을 넣어야 할거은데 "BEARER + hane"가 들어가네요
왜 이렇게 작성된 건지 알수 있을까요?
      

Copy link
Contributor Author

Choose a reason for hiding this comment

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

HANE string이 그냥 들어가지 않고, 디비에서 token 이름이 'hane'인 컬럼을 조회해서 사용합니다

);

return ResponseRefreshTokenDTO
.builder()
.refreshToken(
jwtService.createRefreshToken(authUser.getIntraId(), authUser.getIntraName())
)
.build();
}
}
14 changes: 14 additions & 0 deletions src/main/java/kr/where/backend/join/exception/JoinErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.where.backend.join.exception;

import kr.where.backend.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum JoinErrorCode implements ErrorCode {
DUPLICATED_JOIN(1700, "이미 동의한 유저입니다");

private final int errorCode;
private final String errorMessage;
}
15 changes: 15 additions & 0 deletions src/main/java/kr/where/backend/join/exception/JoinException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.where.backend.join.exception;

import kr.where.backend.exception.CustomException;

public class JoinException extends CustomException {
public JoinException(final JoinErrorCode joinErrorCode) {
super(joinErrorCode);
}

public static class DuplicatedJoinMember extends JoinException {
public DuplicatedJoinMember() {
super(JoinErrorCode.DUPLICATED_JOIN);
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/kr/where/backend/join/swagger/JoinApiDocs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kr.where.backend.join.swagger;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.where.backend.api.exception.JsonException;
import kr.where.backend.auth.authUser.AuthUser;
import kr.where.backend.auth.authUser.AuthUserInfo;
import kr.where.backend.jwt.dto.ResponseRefreshTokenDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;

@Tag(name = "join", description = "join API")
public interface JoinApiDocs {
@Operation(summary = "3.1 JoinMember API", description = "동의 맴버 생성하는 Post API",
responses = {
@ApiResponse(responseCode = "200", description = "맴버 생성 성공",
content = @Content(schema = @Schema(implementation = ResponseRefreshTokenDTO.class))),
@ApiResponse(responseCode = "404", description = "맴버 생성 실패",
content = @Content(schema = @Schema(implementation = JsonException.class)))
}

)
@PostMapping("")
ResponseEntity<ResponseRefreshTokenDTO> join(@AuthUserInfo final AuthUser authUser);
}