-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat : Owner 회원가입 & 로그인 구현
- Loading branch information
Showing
17 changed files
with
550 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 24 additions & 2 deletions
26
src/main/java/com/prgrms/catchtable/member/domain/Gender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
private boolean isEqual(String input) { | ||
return input.equals(this.type); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/prgrms/catchtable/owner/controller/OwnerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/prgrms/catchtable/owner/dto/OwnerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/prgrms/catchtable/owner/dto/request/JoinOwnerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 | ||
) { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/prgrms/catchtable/owner/dto/request/LoginOwnerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
) { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/prgrms/catchtable/owner/dto/response/JoinOwnerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/prgrms/catchtable/owner/repository/OwnerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/prgrms/catchtable/owner/service/OwnerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.