Skip to content

Commit

Permalink
Merge pull request #28 from CoolPeace-yanolza/feature/member
Browse files Browse the repository at this point in the history
멤버 엔티티에 이름 필드 추가
  • Loading branch information
tkddn204 authored Jan 4, 2024
2 parents 88affd0 + d15199a commit 70dd9f8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.coolpeace.domain.member.dto.request.RefreshAccessTokenRequest;
import com.coolpeace.domain.member.dto.response.MemberLoginResponse;
import com.coolpeace.domain.member.dto.response.MemberRefreshAccessTokenResponse;
import com.coolpeace.domain.member.dto.response.MemberRegisterResponse;
import com.coolpeace.domain.member.service.MemberService;
import com.coolpeace.global.jwt.security.JwtPrincipal;
import com.coolpeace.global.resolver.AuthJwtCredential;
Expand All @@ -28,15 +27,15 @@ public class MemberController {
public ResponseEntity<MemberLoginResponse> login(
@Valid @RequestBody MemberLoginRequest loginRequest
) {
return ResponseEntity.ok(MemberLoginResponse.from(memberService.login(loginRequest)));
return ResponseEntity.ok(memberService.login(loginRequest));
}

@PostMapping("/register")
public ResponseEntity<MemberRegisterResponse> register(
public ResponseEntity<Void> register(
@Valid @RequestBody MemberRegisterRequest registerRequest
) {
return ResponseEntity.created(URI.create("/"))
.body(MemberRegisterResponse.from(memberService.registerAsOwner(registerRequest)));
memberService.registerAsOwner(registerRequest);
return ResponseEntity.created(URI.create("/")).build();
}

@GetMapping("/register/check/email")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public record MemberRegisterRequest(
@Email(message = "이메일 형식에 맞지 않습니다.", regexp = "^([\\w-]+(?:\\.[\\w-]+)*)@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-z]{2,6}(?:\\.[a-z]{2})?)$")
String email,
@NotBlank(message = "비밀번호를 입력해주세요.")
String password
String password,

@NotBlank(message = "이름을 입력해주세요.")
String name
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
public record MemberLoginResponse (
String accessToken,
String refreshToken,
String name,
long expiresIn
) {
public static MemberLoginResponse from(JwtPair jwtPair) {
return new MemberLoginResponse(jwtPair.accessToken(), jwtPair.refreshToken(), jwtPair.expiresIn());
public static MemberLoginResponse from(String name, JwtPair jwtPair) {
return new MemberLoginResponse(jwtPair.accessToken(), jwtPair.refreshToken(), name, jwtPair.expiresIn());
}
}

This file was deleted.

10 changes: 7 additions & 3 deletions src/main/java/com/coolpeace/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Member extends BaseTimeEntity {
@Column(nullable = false)
private String password;

@Column(nullable = false)
private String name;

@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private final List<MemberRole> roles = new ArrayList<>();

Expand All @@ -41,13 +44,14 @@ public class Member extends BaseTimeEntity {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<ServiceTerm> serviceTerms;

private Member(String email, String password, Role role) {
private Member(String email, String password, String name, Role role) {
this.email = email;
this.password = password;
this.name =name;
this.roles.add(MemberRole.from(this, role));
}

public static Member from(String email, String password, Role role) {
return new Member(email, password, role);
public static Member from(String email, String password, String name, Role role) {
return new Member(email, password, name, role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.coolpeace.domain.member.dto.request.MemberLoginRequest;
import com.coolpeace.domain.member.dto.request.MemberRegisterEmailCheckRequest;
import com.coolpeace.domain.member.dto.request.MemberRegisterRequest;
import com.coolpeace.domain.member.dto.response.MemberLoginResponse;
import com.coolpeace.domain.member.entity.Member;
import com.coolpeace.domain.member.entity.Role;
import com.coolpeace.domain.member.entity.type.RoleType;
Expand Down Expand Up @@ -34,28 +35,31 @@ public Member getMemberByEmail(String email) {
}

@Transactional
public JwtPair login(MemberLoginRequest loginRequest) {
public MemberLoginResponse login(MemberLoginRequest loginRequest) {
Member storedMember = memberRepository.findByEmail(loginRequest.email())
.orElseThrow(MemberNotFoundException::new);

if (!passwordEncoder.matches(loginRequest.password(), storedMember.getPassword())) {
throw new MemberWrongPasswordException();
}

return jwtService.createTokenPair(
JwtPair newTokenPair = jwtService.createTokenPair(
JwtPayload.fromNow(String.valueOf(storedMember.getId()), storedMember.getEmail()));

return MemberLoginResponse.from(storedMember.getName(), newTokenPair);
}

@Transactional
public JwtPair registerAsOwner(MemberRegisterRequest registerRequest) {
public void registerAsOwner(MemberRegisterRequest registerRequest) {
validateMemberEmail(registerRequest.email());
Role role = roleRepository.findByRoleType(RoleType.OWNER).orElseThrow(MemberRoleNotFoundException::new);
Member newMember = memberRepository.save(
Member.from(registerRequest.email(), passwordEncoder.encode(registerRequest.password()), role)
memberRepository.save(
Member.from(
registerRequest.email(),
passwordEncoder.encode(registerRequest.password()),
registerRequest.name(),
role)
);

return jwtService.createTokenPair(
JwtPayload.fromNow(String.valueOf(newMember.getId()), newMember.getEmail()));
}

@Transactional(readOnly = true)
Expand Down
3 changes: 2 additions & 1 deletion src/test/http/register.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Content-Type: application/json

{
"email": "[email protected]",
"password": "pass"
"password": "pass",
"name": "testname"
}

0 comments on commit 70dd9f8

Please sign in to comment.