From 8a4b6fc7a5a246e752de1446e9ccfeecad6faeec Mon Sep 17 00:00:00 2001 From: devjun10 Date: Fri, 26 Jul 2024 16:12:19 +0900 Subject: [PATCH] feat: User save api development --- .../core/user/application/UserWriteService.java | 5 +++++ .../core/user/application/service/UserService.java | 10 +++++++++- .../app/core/user/persistence/UserRepository.java | 14 +++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/project/dailyge/app/core/user/application/UserWriteService.java diff --git a/api/src/main/java/project/dailyge/app/core/user/application/UserWriteService.java b/api/src/main/java/project/dailyge/app/core/user/application/UserWriteService.java new file mode 100644 index 0000000..3c4c62d --- /dev/null +++ b/api/src/main/java/project/dailyge/app/core/user/application/UserWriteService.java @@ -0,0 +1,5 @@ +package project.dailyge.app.core.user.application; + +public interface UserWriteService { + Long save(String name); +} diff --git a/api/src/main/java/project/dailyge/app/core/user/application/service/UserService.java b/api/src/main/java/project/dailyge/app/core/user/application/service/UserService.java index 2760b03..6d239ff 100644 --- a/api/src/main/java/project/dailyge/app/core/user/application/service/UserService.java +++ b/api/src/main/java/project/dailyge/app/core/user/application/service/UserService.java @@ -3,13 +3,14 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import project.dailyge.app.core.user.application.UserReadService; +import project.dailyge.app.core.user.application.UserWriteService; import project.dailyge.app.core.user.exception.UserNotFoundException; import project.dailyge.entity.user.User; import project.dailyge.entity.user.UserEntityReadRepository; @Service @RequiredArgsConstructor -public class UserService implements UserReadService { +public class UserService implements UserReadService, UserWriteService { private final UserEntityReadRepository userReadRepository; @@ -18,4 +19,11 @@ public User findById(final Long userId) { return userReadRepository.findById(userId) .orElseThrow(() -> new UserNotFoundException("사용자를 찾을 수 없습니다.")); } + + @Override + public Long save(final String name) { + final User newUser = new User(null, name); + userReadRepository.save(newUser); + return newUser.getId(); + } } diff --git a/api/src/main/java/project/dailyge/app/core/user/persistence/UserRepository.java b/api/src/main/java/project/dailyge/app/core/user/persistence/UserRepository.java index f749d2e..71d91d3 100644 --- a/api/src/main/java/project/dailyge/app/core/user/persistence/UserRepository.java +++ b/api/src/main/java/project/dailyge/app/core/user/persistence/UserRepository.java @@ -7,21 +7,25 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; @Repository public class UserRepository implements UserEntityReadRepository { + private final AtomicLong idGenerator = new AtomicLong(0); private final Map map = new HashMap<>(); - public UserRepository() { - map.put(1L, new User(1L, "User-01")); - map.put(2L, new User(1L, "User-02")); - } - @Override public Optional findById(final Long userId) { return Optional.ofNullable( map.get(userId) ); } + + @Override + public void save(final User user) { + final Long newId = idGenerator.incrementAndGet(); + user.insert(newId); + map.put(newId, user); + } }