-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat : Owner 회원가입 & 로그인 구현 #60
Changes from 22 commits
ed8ada9
28c3c45
2179ae0
ae90cfb
76678a9
15849da
5b67aea
22cfd9b
eed4958
44c78af
2fe4fb5
135068d
a0f3690
885b63d
32a9584
7ea8395
7b1923e
e030fcc
86e1a5a
80aca16
4d234f9
bd3b603
845a497
a6d3196
2a8801b
4b756c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
package com.prgrms.catchtable.member.domain; | ||
|
||
import static com.prgrms.catchtable.common.exception.ErrorCode.BAD_REQUEST_INPUT_GENDER_TYPE; | ||
|
||
import com.prgrms.catchtable.common.exception.custom.BadRequestCustomException; | ||
import java.util.Arrays; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum Gender { | ||
MALE, | ||
FEMALE | ||
MALE("male"), | ||
FEMALE("female"); | ||
|
||
private final String type; | ||
|
||
public static Gender of(String input) { | ||
return Arrays.stream(values()) | ||
.filter(gender -> gender.isEqual(input)) | ||
.findAny() | ||
.orElseThrow(() -> new BadRequestCustomException(BAD_REQUEST_INPUT_GENDER_TYPE)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. request에서 검증하지 않고 Enum에서 input을 검증하면 캡슐화의 장점이 있는건지 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enum으로의 변환로직? 이라고 생각할 때, request dto보다는 Enum 안에서 따로 로직이 있어야 된다고 생각하였습니다..! |
||
|
||
private boolean isEqual(String input) { | ||
return input.equals(this.type); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.prgrms.catchtable.owner.controller; | ||
|
||
import com.prgrms.catchtable.jwt.token.Token; | ||
import com.prgrms.catchtable.owner.dto.request.JoinOwnerRequest; | ||
import com.prgrms.catchtable.owner.dto.request.LoginOwnerRequest; | ||
import com.prgrms.catchtable.owner.dto.response.JoinOwnerResponse; | ||
import com.prgrms.catchtable.owner.service.OwnerService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/owners") | ||
public class OwnerController { | ||
|
||
private final OwnerService ownerService; | ||
|
||
@PostMapping("/join") | ||
public ResponseEntity<JoinOwnerResponse> join( | ||
@Valid @RequestBody JoinOwnerRequest joinOwnerRequest) { | ||
JoinOwnerResponse joinOwnerResponse = ownerService.joinOwner(joinOwnerRequest); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED).body(joinOwnerResponse); | ||
} | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<Token> login(@Valid @RequestBody LoginOwnerRequest loginOwnerRequest) { | ||
Token responseToken = ownerService.loginOwner(loginOwnerRequest); | ||
|
||
return ResponseEntity.ok(responseToken); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.prgrms.catchtable.owner.dto; | ||
|
||
import com.prgrms.catchtable.member.domain.Gender; | ||
import com.prgrms.catchtable.owner.domain.Owner; | ||
import com.prgrms.catchtable.owner.dto.request.JoinOwnerRequest; | ||
import com.prgrms.catchtable.owner.dto.response.JoinOwnerResponse; | ||
|
||
public class OwnerMapper { | ||
|
||
public static Owner toEntity(JoinOwnerRequest joinOwnerRequest, String encodePassword, | ||
Gender gender) { | ||
return Owner.builder() | ||
.name(joinOwnerRequest.name()) | ||
.email(joinOwnerRequest.email()) | ||
.password(encodePassword) | ||
.phoneNumber(joinOwnerRequest.phoneNumber()) | ||
.gender(gender) | ||
.dateBirth(joinOwnerRequest.dateBirth()) | ||
.build(); | ||
} | ||
|
||
public static JoinOwnerResponse from(Owner owner) { | ||
return JoinOwnerResponse.builder() | ||
.name(owner.getName()) | ||
.email(owner.getEmail()) | ||
.phoneNumber(owner.getPhoneNumber()) | ||
.gender(owner.getGender().getType()) | ||
.dateBirth(owner.getDateBirth()) | ||
.build(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.prgrms.catchtable.owner.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.Pattern; | ||
import java.time.LocalDate; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record JoinOwnerRequest( | ||
|
||
String name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. message 옵션으로 검증 메세지를 추가해줘도 좋을 것 같아요! |
||
String email, | ||
String password, | ||
@Pattern(regexp = "^(01[016789]){1}-([0-9]{3,4}){1}-([0-9]{4}){1}$") | ||
String phoneNumber, | ||
String gender, | ||
@JsonFormat(pattern = "yyyy-MM-dd") | ||
LocalDate dateBirth | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.prgrms.catchtable.owner.dto.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record LoginOwnerRequest( | ||
|
||
String email, | ||
String password | ||
|
||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.prgrms.catchtable.owner.dto.response; | ||
|
||
import java.time.LocalDate; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record JoinOwnerResponse( | ||
|
||
String name, | ||
String email, | ||
String phoneNumber, | ||
String gender, | ||
LocalDate dateBirth | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package com.prgrms.catchtable.owner.repository; | ||
|
||
import com.prgrms.catchtable.owner.domain.Owner; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OwnerRepository extends JpaRepository<Owner, Long> { | ||
|
||
boolean existsOwnerByEmail(String email); | ||
|
||
Optional<Owner> findOwnerByEmail(String email); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.prgrms.catchtable.owner.service; | ||
|
||
import static com.prgrms.catchtable.common.exception.ErrorCode.ALREADY_EXIST_OWNER; | ||
import static com.prgrms.catchtable.common.exception.ErrorCode.BAD_REQUEST_EMAIL_OR_PASSWORD; | ||
|
||
import com.prgrms.catchtable.common.exception.custom.BadRequestCustomException; | ||
import com.prgrms.catchtable.jwt.provider.JwtTokenProvider; | ||
import com.prgrms.catchtable.jwt.token.Token; | ||
import com.prgrms.catchtable.member.domain.Gender; | ||
import com.prgrms.catchtable.owner.domain.Owner; | ||
import com.prgrms.catchtable.owner.dto.OwnerMapper; | ||
import com.prgrms.catchtable.owner.dto.request.JoinOwnerRequest; | ||
import com.prgrms.catchtable.owner.dto.request.LoginOwnerRequest; | ||
import com.prgrms.catchtable.owner.dto.response.JoinOwnerResponse; | ||
import com.prgrms.catchtable.owner.repository.OwnerRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OwnerService { | ||
|
||
private final OwnerRepository ownerRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public JoinOwnerResponse joinOwner(JoinOwnerRequest joinOwnerRequest) { | ||
|
||
//이미 존재하는 이메일이라면 | ||
validateExistsOwner(joinOwnerRequest); | ||
|
||
String encodePassword = passwordEncoder.encode(joinOwnerRequest.password()); | ||
|
||
Gender gender = Gender.of(joinOwnerRequest.gender()); | ||
|
||
Owner joinOwner = ownerRepository.save( | ||
OwnerMapper.toEntity(joinOwnerRequest, encodePassword, gender)); | ||
|
||
return OwnerMapper.from(joinOwner); | ||
|
||
} | ||
|
||
private void validateExistsOwner(JoinOwnerRequest joinOwnerRequest) { | ||
if (ownerRepository.existsOwnerByEmail(joinOwnerRequest.email())) { | ||
throw new BadRequestCustomException(ALREADY_EXIST_OWNER); | ||
} | ||
} | ||
|
||
public Token loginOwner(LoginOwnerRequest loginRequest) { | ||
|
||
//email 확인 | ||
Owner loginOwner = ownerRepository.findOwnerByEmail(loginRequest.email()) | ||
.orElseThrow(() -> new BadRequestCustomException(BAD_REQUEST_EMAIL_OR_PASSWORD)); | ||
|
||
//password 확인 | ||
validatePassword(loginRequest, loginOwner); | ||
|
||
return jwtTokenProvider.createToken(loginOwner.getEmail()); | ||
} | ||
|
||
private void validatePassword(LoginOwnerRequest loginRequest, Owner loginOwner) { | ||
if (!passwordEncoder.matches(loginRequest.password(), loginOwner.getPassword())) { | ||
throw new BadRequestCustomException(BAD_REQUEST_EMAIL_OR_PASSWORD); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BadRequestCustomException으로 감싸주니까
INVALID_EMAIL_OR_PASSWORD
이렇게 네이밍해도 괜찮지 않을까 생각이 들었습니다!