-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from KNU-HAEDAL/feat/user
유저 수정 기능 개발(아키텍처 구조 예제 추가)
- Loading branch information
Showing
12 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/user/UserReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
17 changes: 17 additions & 0 deletions
17
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
25 changes: 25 additions & 0 deletions
25
...-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserReaderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
17 changes: 17 additions & 0 deletions
17
...i-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserStoreImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |