Skip to content

Commit

Permalink
Merge pull request #1 from KNU-HAEDAL/feat/user
Browse files Browse the repository at this point in the history
유저 수정 기능 개발(아키텍처 구조 예제 추가)
  • Loading branch information
bayy1216 authored May 29, 2024
2 parents 4f423b5 + a454bb5 commit 8eafa95
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public static <T> ApiResponse<T> success(T data) {
.build();
}

public static <T> ApiResponse<T> success(T data, String message) {
return ApiResponse.<T>builder()
.result(Result.SUCCESS)
.data(data)
.message(message)
.build();
}

public static ApiResponse<Void> fail(String errorCode, String message) {
return ApiResponse.<Void>builder()
.result(Result.FAIL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.domain.user.UserService;
import org.haedal.zzansuni.global.jwt.JwtUser;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand All @@ -17,6 +19,7 @@
@RequiredArgsConstructor
@RestController
public class UserController {
private final UserService userService;

@Operation(summary = "내 정보 조회", description = "내 정보를 조회한다.")
@GetMapping("/api/user")
Expand All @@ -32,8 +35,13 @@ public ApiResponse<UserRes.UserInfoDto> getUserInfo(

@Operation(summary = "내 정보 수정", description = "내 정보를 수정한다.")
@PatchMapping("/api/user")
public ApiResponse<Void> updateUser(@RequestBody UserReq.UserUpdateRequest request) {
return ApiResponse.success(null);
public ApiResponse<Void> updateUser(
@Valid @RequestBody UserReq.UserUpdateRequest request,
@AuthenticationPrincipal JwtUser jwtUser
) {
var command = request.toCommand();
userService.updateUser(jwtUser.getId(), command);
return ApiResponse.success(null, "수정에 성공하였습니다.");
}

@Operation(summary = "스트릭 조회", description = "스트릭을 조회한다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package org.haedal.zzansuni.controller.user;

import jakarta.validation.constraints.NotBlank;
import org.haedal.zzansuni.domain.user.UserCommand;

public class UserReq {
public record UserUpdateRequest(String nickname){}
public record UserUpdateRequest(
@NotBlank(message = "nickname은 필수입니다.") String nickname
) {
public UserCommand.Update toCommand() {
return UserCommand.Update.builder()
.nickname(nickname)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
package org.haedal.zzansuni.domain.user;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.global.security.Role;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String nickname;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Role role;

private String email;

private String password;

@Column(nullable = false)
private Integer exp;

private String authToken;

private String profileImageUrl;

public void update(UserCommand.Update userUpdate) {
this.nickname = userUpdate.getNickname();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.haedal.zzansuni.domain.user;

import jakarta.validation.constraints.NotBlank;
import lombok.Builder;
import lombok.Getter;
import org.haedal.zzansuni.core.utils.SelfValidating;

public class UserCommand {

@Getter
@Builder
public static class Update extends SelfValidating<UserCommand.Update> {
@NotBlank(message = "닉네임은 필수입니다.")
private String nickname;

public Update(String nickname) {
this.nickname = nickname;
this.validateSelf();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class UserModel {

public static UserModel from(User user){
public static UserModel from(User user) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.haedal.zzansuni.domain.user;

import java.util.Optional;

public interface UserReader {
User getById(Long id);

Optional<User> findByAuthToken(String authToken);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package org.haedal.zzansuni.domain.user;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class UserService {
private final UserReader userReader;
private final UserStore userStore;

/**
* 수정해야할 정보를 받고 해당 값으로 모두 업데이트
*/
@Transactional
public void updateUser(Long id, UserCommand.Update userUpdate) {
User user = userReader.getById(id);
user.update(userUpdate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.haedal.zzansuni.domain.user;

public interface UserStore {
User store(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.haedal.zzansuni.infrastructure.user;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.user.User;
import org.haedal.zzansuni.domain.user.UserReader;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class UserReaderImpl implements UserReader {
private final UserRepository userRepository;

@Override
public User getById(Long id) {
return userRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

@Override
public Optional<User> findByAuthToken(String authToken) {
return userRepository.findByAuthToken(authToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.haedal.zzansuni.infrastructure.user;

import org.haedal.zzansuni.domain.user.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByAuthToken(String authToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.haedal.zzansuni.infrastructure.user;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.user.User;
import org.haedal.zzansuni.domain.user.UserStore;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserStoreImpl implements UserStore {
private final UserRepository userRepository;

@Override
public User store(User user) {
return userRepository.save(user);
}
}

0 comments on commit 8eafa95

Please sign in to comment.