Skip to content

Commit

Permalink
feat: User save api development
Browse files Browse the repository at this point in the history
  • Loading branch information
devjun10 committed Jul 26, 2024
1 parent 0c5b924 commit 8a4b6fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package project.dailyge.app.core.user.application;

public interface UserWriteService {
Long save(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, User> map = new HashMap<>();

public UserRepository() {
map.put(1L, new User(1L, "User-01"));
map.put(2L, new User(1L, "User-02"));
}

@Override
public Optional<User> 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);
}
}

0 comments on commit 8a4b6fc

Please sign in to comment.