Skip to content

Commit

Permalink
회원가입시 성별, 나이대 받도록 수정
Browse files Browse the repository at this point in the history
Controller return Response Entity 신규 양식으로 변경
가입, 로그인시 발생하는 위험요소에 대한 Exception 처리
  • Loading branch information
gunsight1 committed Jan 19, 2024
1 parent e9a036e commit b80e0fc
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.kernel360.member.code;

import com.kernel360.code.BusinessCode;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor
public enum MemberBusinessCode implements BusinessCode {

SUCCESS_REQUEST_JOIN_MEMBER_CREATED(HttpStatus.CREATED.value(), "BMEC001", "회원가입 성공"),
SUCCESS_REQUEST_LOGIN_MEMBER(HttpStatus.OK.value(), "BMEC002", "로그인 성공");


private final int status;
private final String code;
private final String message;

@Override
public int getStatus() {
return status;
}

@Override
public String getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kernel360.member.code;

import com.kernel360.code.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor
public enum MemberErrorCode implements ErrorCode {

FAILED_NOT_MAPPING_ORDINAL_TO_NAME(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMEC001", "DB값과 ENUM의 ORDINAL이 불일치하여 NAME을 찾을 수 없음."),
FAILED_NOT_MAPPING_ORDINAL_TO_VALUE(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMEC002", "DB값과 ENUM의 VALUE가 불일치하여 VALUE을 찾을 수 없음."),
FAILED_NOT_MAPPING_ENUM_VALUEOF(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMEC003", "PARAMETER 값과 ENUM의 NAME이 불일치함."),
FAILED_GENERATE_JOIN_MEMBER_INFO(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMEC004", "회원가입에 필요한 정보 생성 실패"),
FAILED_GENERATE_LOGIN_REQUEST_INFO(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMEC005", "정보 불일치로 인한 로그인 정보 생성 실패"),
FAILED_REQUEST_LOGIN(HttpStatus.BAD_REQUEST.value(), "EMEC006", "정보 불일치로 인한 로그인 실패");



private final int status;
private final String code;
private final String message;

@Override
public int getStatus() {
return status;
}

@Override
public String getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import com.kernel360.member.dto.MemberDto;
import com.kernel360.member.service.MemberService;
import com.kernel360.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.kernel360.member.code.MemberBusinessCode.SUCCESS_REQUEST_JOIN_MEMBER_CREATED;
import static com.kernel360.member.code.MemberBusinessCode.SUCCESS_REQUEST_LOGIN_MEMBER;

@Slf4j
@RestController
@RequiredArgsConstructor
Expand All @@ -18,25 +21,21 @@ public class MemberController {
private final MemberService memberService;

@PostMapping("/join")
public ResponseEntity<String> joinMember(@RequestBody MemberDto joinRequestDto) {
public ResponseEntity<?> joinMember(@RequestBody MemberDto joinRequestDto) {

try {
memberService.joinMember(joinRequestDto);
} catch (IllegalArgumentException args) {
log.error("가입에러발생", args);
}
memberService.joinMember(joinRequestDto);

return new ResponseEntity<>("", HttpStatus.CREATED);
return ApiResponse.toResponseEntity(SUCCESS_REQUEST_JOIN_MEMBER_CREATED);
}

@PostMapping("/login")
public ResponseEntity<MemberDto> login(@RequestBody MemberDto loginDto) {
public ResponseEntity<?> login(@RequestBody MemberDto loginDto) {

MemberDto memberInfo = memberService.login(loginDto);

//부가정보가 입력 되어있는가 > 차량정보, 세차정보, boolean (감싸서 보내든 말든 노상관)

return new ResponseEntity<>(memberInfo, HttpStatus.OK);
return ApiResponse.toResponseEntity(SUCCESS_REQUEST_LOGIN_MEMBER, memberInfo);
}

@GetMapping("/duplicatedCheckId/{id}")
Expand Down
24 changes: 14 additions & 10 deletions module-api/src/main/java/com/kernel360/member/dto/MemberDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.kernel360.member.dto;

import com.kernel360.member.entity.Member;
import com.kernel360.member.enumset.Age;
import com.kernel360.member.enumset.Gender;

import java.time.LocalDate;

Expand Down Expand Up @@ -54,8 +56,8 @@ public static MemberDto from(Member entity) {
entity.getId(),
entity.getEmail(),
entity.getPassword(),
entity.getGender(),
entity.getAge(),
Gender.ordinalToName(entity.getGender()),
Age.ordinalToValue(entity.getAge()),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
Expand All @@ -70,24 +72,26 @@ public Member toEntity() {
this.id(),
this.email(),
this.password(),
this.gender(),
this.age()
Gender.valueOf(this.gender()).ordinal(),
Age.valueOf(this.age()).ordinal()
);
}

/** joinMember **/
public static MemberDto of(
String id,
String email,
String password
String password,
String gender,
String age
){
return new MemberDto(
null,
id,
email,
password,
null,
null,
gender,
age,
null,
null,
null,
Expand All @@ -97,14 +101,14 @@ public static MemberDto of(
}

/** Login Binding **/
public static MemberDto login(Member entity,String jwtToken) {
public static MemberDto login(Member entity, String jwtToken) {
return MemberDto.of(
entity.getMemberNo(),
entity.getId(),
entity.getEmail(),
null,
entity.getGender(),
entity.getAge(),
Gender.ordinalToName(entity.getGender()),
Age.ordinalToValue(entity.getAge()),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
Expand Down
25 changes: 25 additions & 0 deletions module-api/src/main/java/com/kernel360/member/enumset/Age.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kernel360.member.enumset;

import com.kernel360.exception.BusinessException;
import com.kernel360.member.code.MemberErrorCode;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum Age {
AGE_20(20),
AGE_30(30),
AGE_40(40),
AGE_50(50),
AGE_60(60);

private final int value;

public static String ordinalToValue(int key) {
for (Age age : values()) {
if (age.ordinal() == key) {
return String.valueOf(age.value);
}
}
throw new BusinessException(MemberErrorCode.FAILED_NOT_MAPPING_ORDINAL_TO_VALUE);
}
}
21 changes: 21 additions & 0 deletions module-api/src/main/java/com/kernel360/member/enumset/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kernel360.member.enumset;

import com.kernel360.exception.BusinessException;
import com.kernel360.member.code.MemberErrorCode;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum Gender {
man(0),
woman(1);

private final int value;
public static String ordinalToName(int value) {
for (Gender gender : values()) {
if (gender.ordinal() == value) {
return gender.name();
}
}
throw new BusinessException(MemberErrorCode.FAILED_NOT_MAPPING_ORDINAL_TO_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.kernel360.auth.entity.Auth;
import com.kernel360.auth.repository.AuthRepository;
import com.kernel360.exception.BusinessException;
import com.kernel360.member.code.MemberErrorCode;
import com.kernel360.member.dto.MemberDto;
import com.kernel360.member.entity.Member;
import com.kernel360.member.enumset.Age;
import com.kernel360.member.enumset.Gender;
import com.kernel360.member.repository.MemberRepository;
import com.kernel360.utils.ConvertSHA256;
import com.kernel360.utils.JWT;
Expand Down Expand Up @@ -31,15 +35,25 @@ public class MemberService {
public void joinMember(MemberDto requestDto) {

Member entity = getNewJoinMemberEntity(requestDto);
if(entity == null) { throw new BusinessException(MemberErrorCode.FAILED_GENERATE_JOIN_MEMBER_INFO); }

memberRepository.save(entity);
}

protected Member getNewJoinMemberEntity(MemberDto requestDto) {

String encodePassword = ConvertSHA256.convertToSHA256(requestDto.password());
int genderOrdinal;
int ageOrdinal;

return Member.createJoinMember(requestDto.id(), requestDto.email(), encodePassword);
try{
genderOrdinal = Gender.valueOf(requestDto.gender()).ordinal();
ageOrdinal = Age.valueOf(requestDto.age()).ordinal();
}catch (Exception e){
throw new BusinessException(MemberErrorCode.FAILED_NOT_MAPPING_ENUM_VALUEOF);
}

return Member.createJoinMember(requestDto.id(), requestDto.email(), encodePassword, genderOrdinal, ageOrdinal);
}

/**
Expand All @@ -49,9 +63,15 @@ protected Member getNewJoinMemberEntity(MemberDto requestDto) {
public MemberDto login(MemberDto loginDto) {

Member loginEntity = getReqeustLoginEntity(loginDto);
if(loginEntity == null) { throw new BusinessException(MemberErrorCode.FAILED_GENERATE_LOGIN_REQUEST_INFO); }

Member memberEntity = memberRepository.findOneByIdAndPassword(loginEntity.getId(), loginEntity.getPassword());
if(memberEntity == null) { throw new BusinessException(MemberErrorCode.FAILED_REQUEST_LOGIN); }

String token = jwt.generateToken(memberEntity.getId());

String encryptToken = ConvertSHA256.convertToSHA256(token);

Auth authJwt = authRepository.findOneByMemberNo(memberEntity.getMemberNo());

//결과 없으면 entity로 신규 생성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setup() {
void 회원입요청() throws Exception {

/** given 목데이터 세팅 **/
MemberDto memberDto = MemberDto.of("testID", "[email protected]", "testPassword");
MemberDto memberDto = MemberDto.of("testID", "[email protected]", "testPassword", "man","30");

ObjectMapper objectMapper = new ObjectMapper();
String param = objectMapper.writeValueAsString(memberDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;


Expand All @@ -14,8 +12,8 @@ class MemberTest {
private String id;
private String email;
private String password;
private String gender;
private LocalDate birthdate;
private int gender;
private int age;
private Member member;

@BeforeEach
Expand All @@ -24,10 +22,10 @@ class MemberTest {
id = "user123";
email = "[email protected]";
password = "password123";
gender = "Female";
birthdate = LocalDate.of(1990, 1, 1);
gender = 0;
age = 3;

member = Member.of(memberNo, id, email, password, gender, birthdate);
member = Member.of(memberNo, id, email, password, gender, age);
}

@Test
Expand All @@ -37,6 +35,6 @@ class MemberTest {
assertThat(member.getEmail()).isEqualTo(email);
assertThat(member.getPassword()).isEqualTo(password);
assertThat(member.getGender()).isEqualTo(gender);
assertThat(member.getBirthdate()).isEqualTo(birthdate);
assertThat(member.getAge()).isEqualTo(age);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void init() {
void 회원_로직_테스트() {

/** given **/
MemberDto requestDto = MemberDto.of("testID", "[email protected]", "testPassword");
MemberDto requestDto = MemberDto.of("testID", "[email protected]", "testPassword", "man", "AGE_40");
Member member = memberService.getNewJoinMemberEntity(requestDto);

/** when **/
Expand Down Expand Up @@ -81,7 +81,7 @@ public void init() {
/** given **/
MemberDto loginDto = MemberDto.of("test03", "1234qwer");
Member mockLoginEntity = Member.loginMember(loginDto.id(), loginDto.password());
Member mockEntity = Member.of(502L, loginDto.id(), "[email protected]", "0eb9de69892882d54516e03e30098354a2e39cea36adab275b6300c737c942fd", null, null);
Member mockEntity = Member.of(502L, loginDto.id(), "[email protected]", "0eb9de69892882d54516e03e30098354a2e39cea36adab275b6300c737c942fd", 0, 0);
String mockToken = "dummy_token";

/** stub **/
Expand All @@ -105,7 +105,7 @@ public void init() {
void 토큰_발급_저장_테스트() {

/** given **/
Member memberEntity = Member.of(502L, "test03", null, null, null, null);
Member memberEntity = Member.of(502L, "test03", null, null, 0, 0);
String mockToken = "mockToken";
Auth auth = Auth.jwt(null, 502L, mockToken);

Expand Down Expand Up @@ -136,7 +136,7 @@ public void init() {

/** given **/
String id = "test01";
Member memberEntity = Member.of(51L, "test01", null, null, null, null);
Member memberEntity = Member.of(51L, "test01", null, null, 0, 0);

/** stub **/
when(memberRepository.findOneById(anyString())).thenReturn(memberEntity);
Expand Down Expand Up @@ -175,7 +175,7 @@ public void init() {

/** given **/
String email = "[email protected]";
Member memberEntity = Member.of(51L, "test01", "[email protected]", null, null, null);
Member memberEntity = Member.of(51L, "test01", "[email protected]", null, 0, 0);

/** stub **/
when(memberRepository.findOneByEmail(anyString())).thenReturn(memberEntity);
Expand Down
Loading

0 comments on commit b80e0fc

Please sign in to comment.