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

[BE] 회원 정보 조회 기능 구현 #852

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public EventAppResponse saveEventGuest(EventGuestAppRequest request) {
Event event = new Event(request.eventName(), userId, token);
eventRepository.save(event);

eventMemberRepository.save(new EventMember(event, request.nickname()));
eventMemberRepository.save(EventMember.createHost(event, request.nickname()));
return EventAppResponse.of(event);
}

Expand All @@ -64,7 +64,7 @@ public EventAppResponse saveEvent(EventAppRequest request) {
eventRepository.save(event);

String nickname = userService.findNicknameById(request.userId());
eventMemberRepository.save(new EventMember(event, nickname));
eventMemberRepository.save(EventMember.createHost(event, nickname));

return EventAppResponse.of(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import haengdong.user.domain.User;

public record UserAppResponse(
String nickname,
String bankName,
String accountNumber,
Boolean isGuest
boolean isGuest,
String profileImage
) {
public static UserAppResponse of(User user) {
return new UserAppResponse(user.getBank(), user.getAccountNumber(), user.isGuest());
return new UserAppResponse(user.getNickname(), user.getBank(), user.getAccountNumber(), user.isGuest(),
user.getPicture()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public EventMember(Long id, Event event, String name, boolean isDeposited) {
this.isDeposited = isDeposited;
}

public static EventMember createHost(Event event, String name) {
return new EventMember(null, event, name, true);
}

private void validateName(String name) {
int nameLength = name.length();
if (nameLength < MIN_NAME_LENGTH || nameLength > MAX_NAME_LENGTH) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package haengdong.user.application;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import haengdong.user.application.request.UserJoinAppRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -13,6 +15,7 @@
public class KakaoUserService {

private static final String NICKNAME_KEY = "nickname";
private static final String PICTURE_KEY = "picture";

private final UserService userService;
private final KakaoClient kakaoClient;
Expand All @@ -24,10 +27,12 @@ public Long joinByKakao(String code, String redirectUri) {

String memberNumber = decodedJWT.getSubject();
String nickname = decodedJWT.getClaim(NICKNAME_KEY).asString();
Claim pictureClaim = decodedJWT.getClaim(PICTURE_KEY);
String picture = pictureClaim.isNull() ? null : pictureClaim.asString();

log.info("로그인 성공 : {}, {}, {}", code, memberNumber, nickname);
log.info("로그인 성공 : {}, {}, {}, {}", code, memberNumber, nickname, picture);

return userService.join(memberNumber, nickname);
return userService.join(new UserJoinAppRequest(memberNumber, nickname, picture));
}

public String getClientId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import haengdong.common.exception.HaengdongException;
import haengdong.event.application.response.UserAppResponse;
import haengdong.user.application.request.UserGuestSaveAppRequest;
import haengdong.user.application.request.UserJoinAppRequest;
import haengdong.user.application.request.UserUpdateAppRequest;
import haengdong.user.domain.User;
import haengdong.user.domain.repository.UserRepository;
Expand All @@ -28,9 +29,9 @@ public Long joinGuest(UserGuestSaveAppRequest request) {
}

@Transactional
public Long join(String memberNumber, String nickname) {
User user = userRepository.findByMemberNumber(memberNumber)
.orElseGet(() -> userRepository.save(User.createMember(nickname, memberNumber)));
public Long join(UserJoinAppRequest request) {
User user = userRepository.findByMemberNumber(request.memberNumber())
.orElseGet(() -> userRepository.save(request.toUser()));

return user.getId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package haengdong.user.application.request;

import haengdong.user.domain.User;

public record UserJoinAppRequest(
String memberNumber,
String nickname,
String picture
) {
public User toUser() {
return User.createMember(nickname, memberNumber, picture);
}
}
11 changes: 7 additions & 4 deletions server/src/main/java/haengdong/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,23 @@ public class User extends BaseEntity {
@Column(unique = true)
private String memberNumber;

private User(String nickname, String password, String bank, String accountNumber, String memberNumber) {
private String picture;

private User(String nickname, String password, String bank, String accountNumber, String memberNumber, String picture) {
this.nickname = nickname;
this.password = new Password(password);
this.bank = bank;
this.accountNumber = accountNumber;
this.memberNumber = memberNumber;
this.picture = picture;
}

public static User createGuest(String nickName, String password) {
return new User(nickName, password, "", "", null);
return new User(nickName, password, "", "", null, null);
}

public static User createMember(String nickName, String memberNumber) {
return new User(nickName, "0000", "", "", memberNumber);
public static User createMember(String nickName, String memberNumber, String picture) {
return new User(nickName, "0000", "", "", memberNumber, picture);
}

public void changeNickname(String nickname) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package haengdong.user.presentation;

import haengdong.event.application.response.UserAppResponse;
import haengdong.user.presentation.response.UserResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -56,14 +58,20 @@ public ResponseEntity<Void> kakaoLogin(
) {
log.info("Kakao login code, redirectUri: {}, {}", code, redirectUri);
Long userId = kakaoUserService.joinByKakao(code, redirectUri);
String jwtToken = authService.createGuestToken(userId);
String jwtToken = authService.createMemberToken(userId);

ResponseCookie responseCookie = createResponseCookie(jwtToken);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, responseCookie.toString())
.build();
}

@GetMapping("/api/users/mine")
public ResponseEntity<UserResponse> findUser(@Login Long userId) {
UserAppResponse response = userService.findById(userId);
return ResponseEntity.ok(UserResponse.of(response));
}

@DeleteMapping("/api/users")
public ResponseEntity<Void> deleteUser(@Login Long userId) {
userService.withdraw(userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package haengdong.user.presentation.response;

import haengdong.event.application.response.UserAppResponse;

public record UserResponse(
String nickname,
String bankName,
String accountNumber,
boolean isGuest,
String profileImage
) {

public static UserResponse of(UserAppResponse response) {
return new UserResponse(
response.nickname(), response.bankName(), response.accountNumber(), response.isGuest(),
response.profileImage()
);
}
}
Loading