Skip to content

Commit

Permalink
Merge pull request #24 from 2024-pre-onboarding-backend-F/refactor/us…
Browse files Browse the repository at this point in the history
…er_login

[refactor] 전체 DTO 수정 및 추가
  • Loading branch information
jw427 authored Sep 3, 2024
2 parents 0f4d673 + e47a433 commit 1beed68
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 25 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.jsonwebtoken:jjwt:0.9.1' // 자바 JWT 라이브러리
implementation 'javax.xml.bind:jaxb-api:2.3.1' // XML 문서와 Java 객체 간 매핑 자동화
implementation 'javax.xml.bind:jaxb-api:2.3.1' // XML 문서와 Java 객체 간 매핑 자동화
testAnnotationProcessor('org.projectlombok:lombok')

// Jackson
implementation 'com.fasterxml.jackson.core:jackson-databind'
// Spring Batch
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/wanted/ribbon/user/controller/UserApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import wanted.ribbon.user.domain.User;
import wanted.ribbon.user.dto.SignUpUserRequest;
import wanted.ribbon.user.dto.UpdateUserRequest;
import wanted.ribbon.user.dto.UserLoginRequestDto;
import wanted.ribbon.user.dto.UserLoginResponseDto;
import wanted.ribbon.user.dto.*;
import wanted.ribbon.user.service.UserService;

import java.util.UUID;
Expand All @@ -21,8 +17,8 @@ public class UserApiController {
private final UserService userService;

@PostMapping("/signup")
public ResponseEntity<UUID> signUp(@Validated @RequestBody SignUpUserRequest request){
UUID response = userService.save(request);
public ResponseEntity<SignUpResponse> signUp(@Validated @RequestBody SignUpUserRequest request){
SignUpResponse response = userService.save(request);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

Expand All @@ -34,14 +30,14 @@ public ResponseEntity<UserLoginResponseDto> login(@RequestBody UserLoginRequestD
}

@GetMapping("/profile/{userId}")
public ResponseEntity<User> getUserInfo(@PathVariable UUID userId){
User user = userService.findByUserId(userId);
return ResponseEntity.status(HttpStatus.OK).body(user);
public ResponseEntity<ProfileResponse> getUserInfo(@PathVariable UUID userId, @RequestBody ProfileRequest request){
ProfileResponse profile = userService.findByUser(userId, request);
return ResponseEntity.ok(profile);
}

@PutMapping("/settings/{userId}")
public ResponseEntity<User> updateUser(@PathVariable UUID userId, @RequestBody UpdateUserRequest request){
User updatedUser = userService.updateUser(userId, request);
public ResponseEntity<UpdateUserResponse> updateUser(@PathVariable UUID userId, @RequestBody UpdateUserRequest request){
UpdateUserResponse updatedUser = userService.updateUser(userId, request);
return ResponseEntity.status(HttpStatus.OK).body(updatedUser);
}
}
13 changes: 13 additions & 0 deletions src/main/java/wanted/ribbon/user/dto/ProfileRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package wanted.ribbon.user.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ProfileRequest {
private String id;
private double lat;
private double lon;
private boolean recommend;
}
9 changes: 9 additions & 0 deletions src/main/java/wanted/ribbon/user/dto/ProfileResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package wanted.ribbon.user.dto;

public record ProfileResponse(
String message,
String id,
double lat,
double lon,
boolean recommend
) { }
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package wanted.ribbon.user.dto;

public record SignUpUserResponse(
import java.util.UUID;

public record SignUpResponse(
String message,
UUID userId,
String id

) { }
12 changes: 12 additions & 0 deletions src/main/java/wanted/ribbon/user/dto/UpdateUserResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wanted.ribbon.user.dto;

import java.util.UUID;

public record UpdateUserResponse(
String message,
UUID userId,
double lat,
double lon,
boolean recommend
) { }

45 changes: 34 additions & 11 deletions src/main/java/wanted/ribbon/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package wanted.ribbon.user.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import wanted.ribbon.exception.ErrorCode;
import wanted.ribbon.exception.NotFoundException;
import wanted.ribbon.user.config.TokenProvider;
import wanted.ribbon.user.domain.User;
import wanted.ribbon.user.dto.SignUpUserRequest;
import wanted.ribbon.user.dto.UpdateUserRequest;
import wanted.ribbon.user.dto.UserLoginRequestDto;
import wanted.ribbon.user.dto.UserLoginResponseDto;
import wanted.ribbon.user.dto.*;
import wanted.ribbon.user.repository.UserRepository;

import java.time.Duration;
Expand All @@ -24,19 +22,34 @@ public class UserService {
private final TokenProvider tokenProvider; // TokenProvider를 사용
private final TokenService tokenService; // TokenService를 사용

public UUID save(SignUpUserRequest dto){
return userRepository.save(User.builder()
public SignUpResponse save(SignUpUserRequest dto){
User user = userRepository.save(User.builder()
.id(dto.getId())
.password(bCryptPasswordEncoder.encode(dto.getPassword())) // 패스워드 암호화
.build()).getUserId();
.build());

return new SignUpResponse(
"회원가입 성공",
user.getUserId(),
user.getId()
);
}

public User findByUserId(UUID userId){
return userRepository.findById(userId)
public ProfileResponse findByUser(UUID userId, ProfileRequest request){
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));

return new ProfileResponse(
"회원 조회 성공",
user.getId(),
user.getLat(),
user.getLon(),
user.isRecommend()
);
}

public User updateUser(UUID userId, UpdateUserRequest request) {
@Transactional
public UpdateUserResponse updateUser(UUID userId, UpdateUserRequest request) {
// 1. 유저 정보 조회
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));
Expand All @@ -47,9 +60,19 @@ public User updateUser(UUID userId, UpdateUserRequest request) {
user.setRecommend(request.isRecommend());

// 3. 변경된 유저 객체 저장
return userRepository.save(user);
User updatedUser = userRepository.save(user);

// 4. UpdateUserResponse 생성 및 반환
return new UpdateUserResponse(
"위도, 경도, 추천 여부 변경 완료",
user.getUserId(),
user.getLat(),
user.getLon(),
user.isRecommend()
);
}

@Transactional
public UserLoginResponseDto login(UserLoginRequestDto requestDto) {
// 사용자 조회
User user = userRepository.findById(requestDto.getId())
Expand Down

0 comments on commit 1beed68

Please sign in to comment.