Skip to content

Commit

Permalink
feat #16 : 회원 닉네임 수정 및 정보 조회 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
baesunyoung6767 committed Jun 23, 2024
1 parent 52af63d commit 8f810ab
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 24 deletions.
Binary file modified build/classes/java/main/com/sunjoo/auth/domain/User.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion build/resources/main/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ eureka:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/
defaultZone: http://sunjoo-server-eureka-eureka-1:8761/eureka/
2 changes: 1 addition & 1 deletion src/main/java/com/sunjoo/auth/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.time.LocalDate;

@Entity
@Getter
@Getter @Setter
@Builder
@AllArgsConstructor
@Table(name="user")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.sunjoo.auth.domain.controller;

import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto;
import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto;
import com.sunjoo.auth.domain.dto.UserRegisterRequestDto;
import com.sunjoo.auth.domain.dto.UserRegisterResponseDto;
import com.sunjoo.auth.domain.dto.*;
import com.sunjoo.auth.domain.security.UserDetailsImpl;
import com.sunjoo.auth.domain.service.JwtService;
import com.sunjoo.auth.domain.service.RedisService;
import com.sunjoo.auth.domain.service.UserService;
Expand All @@ -13,10 +11,12 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.sql.SQLException;
import java.util.HashMap;

Expand Down Expand Up @@ -57,4 +57,17 @@ public ResponseEntity kakaoLogin(@RequestParam(required = false) String token, H
return ResponseEntity.internalServerError().build();
}
}
}

@GetMapping("/userinfo")
public ResponseEntity getUserInfo(@AuthenticationPrincipal UserDetailsImpl userDetails) {
log.info("정보 조회 요청 회원 번호 : " + userDetails.getUserNo());
UserInfoResponseDto userInfoResponseDto = userService.getUserInfo(userDetails.getUserNo());
return ResponseEntity.ok(Response.success(userInfoResponseDto));
}

@PutMapping("/nickname")
public ResponseEntity updateUserNickname(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody NickNameRequestDto requestDto) {
NickNameResponseDto nickNameResponseDto = userService.updateNickName(userDetails.getUserNo(), requestDto.getNewNickName());
return ResponseEntity.ok(Response.success(nickNameResponseDto));
}
};
13 changes: 13 additions & 0 deletions src/main/java/com/sunjoo/auth/domain/dto/NickNameRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sunjoo.auth.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class NickNameRequestDto {
String newNickName;
}
22 changes: 22 additions & 0 deletions src/main/java/com/sunjoo/auth/domain/dto/NickNameResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.sunjoo.auth.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class NickNameResponseDto {
long userNo;
String id;
String nickname; // 닉네임
String type;

public NickNameResponseDto(long userNo, String nickname, String type) {
this.userNo = userNo;
this.nickname = nickname;
this.type = type;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/sunjoo/auth/domain/dto/UserInfoResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sunjoo.auth.domain.dto;

import lombok.*;

@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoResponseDto {
long userNo;
String id;
String name;
String type;

public UserInfoResponseDto(long userNo, String name, String type) {
this.userNo = userNo;
this.name = name;
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void checkRefreshTokenAndReIssueAccessToken(HttpServletResponse response
private void checkAcceessTokenAndAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
jwtService.extractAccessToken(request).filter(jwtService::isTokenValid).ifPresent(
accessToken -> jwtService.extractUserNo(accessToken).ifPresent(
id -> userRepository.findById(id).ifPresent(
userNo -> userRepository.findByUserNo(userNo).ifPresent(
user -> saveAuthentication(user)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public String getPassword() {
}

@Override
public String getUsername() {
return user.getId();
}
public String getUsername() { return user.getId(); }

public long getUserNo() {return user.getUserNo();}
}
7 changes: 3 additions & 4 deletions src/main/java/com/sunjoo/auth/domain/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.sunjoo.auth.domain.service;

import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto;
import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto;
import com.sunjoo.auth.domain.dto.UserRegisterRequestDto;
import com.sunjoo.auth.domain.dto.UserRegisterResponseDto;
import com.sunjoo.auth.domain.dto.*;

import java.util.HashMap;

public interface UserService {
public UserRegisterResponseDto register(UserRegisterRequestDto registerRequestDto);
public KakaoLoginRequestDto getKakaoUserInfo(String accessToken);
public KakaoLoginResponseDto kakaoLogin(KakaoLoginRequestDto kakaoRequest);
public UserInfoResponseDto getUserInfo(long userNo);
public NickNameResponseDto updateNickName(long userNo, String nickName);
}
44 changes: 35 additions & 9 deletions src/main/java/com/sunjoo/auth/domain/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@
import com.nimbusds.jose.shaded.gson.JsonParser;
import com.sunjoo.auth.domain.User;
import com.sunjoo.auth.domain.UserRepository;
import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto;
import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto;
import com.sunjoo.auth.domain.dto.UserRegisterRequestDto;
import com.sunjoo.auth.domain.dto.UserRegisterResponseDto;
import com.sunjoo.auth.domain.dto.*;
import com.sunjoo.auth.global.exception.AppException;
import com.sunjoo.auth.global.exception.ErrorCode;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Optional;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -109,6 +102,39 @@ public KakaoLoginResponseDto kakaoLogin(KakaoLoginRequestDto kakaoRequest) {
return kakaoResponse;
}

@Override
public UserInfoResponseDto getUserInfo(long userNo) {
UserInfoResponseDto userInfoResponseDto = new UserInfoResponseDto();
userRepository.findByUserNo(userNo).ifPresentOrElse(
user->{
if(user.getType().equals("DEFAULT")) userInfoResponseDto.setId(user.getId());
userInfoResponseDto.setUserNo(user.getUserNo());
userInfoResponseDto.setName(user.getName());
userInfoResponseDto.setType(user.getType());
}, () -> {throw new AppException(ErrorCode.USER_NOT_FOUND);}
);
return userInfoResponseDto;
}

@Transactional
@Override
public NickNameResponseDto updateNickName(long userNo, String nickName) {
NickNameResponseDto nickNameResponseDto = new NickNameResponseDto();
User user = userRepository.findByUserNo(userNo).orElseThrow(() -> {
throw new AppException(ErrorCode.USER_NOT_FOUND);
});

user.setName(nickName);
nickNameResponseDto.setNickname(user.getName());
nickNameResponseDto.setUserNo(user.getUserNo());
nickNameResponseDto.setType(user.getType());

if(user.getType().equals("DEFAULT")) {
nickNameResponseDto.setId(user.getId());
}
return nickNameResponseDto;
}

private void userJoinValid(String id) {
userRepository.findById(id)
.ifPresent(user -> {throw new AppException(ErrorCode.DUPLICATED_USER_ID);});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Getter
public enum ErrorCode {
DUPLICATED_USER_ID(HttpStatus.CONFLICT, "아이디가 중복됩니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 유저 정보를 찾을 수 없습니다."),
DATABASE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "DB 에러");

private HttpStatus httpStatus;
Expand Down

0 comments on commit 8f810ab

Please sign in to comment.