From e628c992f2ffc90797206a4deb47b3f6b727210b Mon Sep 17 00:00:00 2001 From: woosung1223 Date: Mon, 30 Sep 2024 18:35:17 +0900 Subject: [PATCH 1/2] =?UTF-8?q?:art:=20chore:=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=84=A0=EC=96=B8=20=EC=88=9C=EC=84=9C=20=EC=A1=B0?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toyouserver/application/UserService.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java index b9b5515..1e70530 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java @@ -41,19 +41,6 @@ public List findUsersWithFilteringOptions(Long userId, String sear return findUsersWithSpecificGroup(user, search, groupId); } - @Transactional - public void updateUser(Long userId, UserUpdateRequest request) { - User foundUser = userRepository.findById(userId) - .orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND)); - User updatedUser = userRepository.findById(request.id()) - .map(each -> each.updateInfo(request.name(), request.birthday(), - request.introduction(), request.imageUrl())) - .orElseThrow(() -> new ToyouException(ResponseType.BAD_REQUEST)); - - updateUserGroups(foundUser, request.groups()); - userRepository.save(updatedUser); - } - private List findAllUsersWithSameGroups(User user, String search) { return memberRepository.findByUser(user).stream() .map(Member::getGroup) @@ -76,6 +63,19 @@ private List findUsersWithSpecificGroup(User user, String search, .toList(); } + @Transactional + public void updateUser(Long userId, UserUpdateRequest request) { + User foundUser = userRepository.findById(userId) + .orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND)); + User updatedUser = userRepository.findById(request.id()) + .map(each -> each.updateInfo(request.name(), request.birthday(), + request.introduction(), request.imageUrl())) + .orElseThrow(() -> new ToyouException(ResponseType.BAD_REQUEST)); + + updateUserGroups(foundUser, request.groups()); + userRepository.save(updatedUser); + } + private void updateUserGroups(User user, List groupRequests) { List members = memberRepository.findByUser(user); memberRepository.deleteAll(members); From 525529a3a0fa78ed871fe43403ab38c85ef01973 Mon Sep 17 00:00:00 2001 From: woosung1223 Date: Mon, 30 Sep 2024 18:52:43 +0900 Subject: [PATCH 2/2] =?UTF-8?q?:sparkles:=20feat:=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=83=9D=EC=9D=BC=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20?= =?UTF-8?q?=EC=8B=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toyouserver/application/UserService.java | 38 +++++++++++++++---- .../persistance/EventRepository.java | 3 ++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java index 1e70530..91f9eba 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java @@ -1,10 +1,15 @@ package slvtwn.khu.toyouserver.application; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.common.response.ResponseType; +import slvtwn.khu.toyouserver.domain.Event; +import slvtwn.khu.toyouserver.domain.EventType; import slvtwn.khu.toyouserver.domain.Group; import slvtwn.khu.toyouserver.domain.Member; import slvtwn.khu.toyouserver.domain.User; @@ -12,6 +17,7 @@ import slvtwn.khu.toyouserver.dto.UserResponse; import slvtwn.khu.toyouserver.dto.UserUpdateRequest; import slvtwn.khu.toyouserver.exception.ToyouException; +import slvtwn.khu.toyouserver.persistance.EventRepository; import slvtwn.khu.toyouserver.persistance.GroupRepository; import slvtwn.khu.toyouserver.persistance.MemberRepository; import slvtwn.khu.toyouserver.persistance.UserRepository; @@ -21,9 +27,12 @@ @Transactional(readOnly = true) public class UserService { + private static final int PERIOD_UPPER_BOUND = 100; + private final UserRepository userRepository; private final MemberRepository memberRepository; private final GroupRepository groupRepository; + private final EventRepository eventRepository; public UserResponse getProfile(Long userId) { User user = userRepository.findById(userId) @@ -65,15 +74,30 @@ private List findUsersWithSpecificGroup(User user, String search, @Transactional public void updateUser(Long userId, UserUpdateRequest request) { - User foundUser = userRepository.findById(userId) + User user = userRepository.findById(userId) .orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND)); - User updatedUser = userRepository.findById(request.id()) - .map(each -> each.updateInfo(request.name(), request.birthday(), - request.introduction(), request.imageUrl())) - .orElseThrow(() -> new ToyouException(ResponseType.BAD_REQUEST)); - updateUserGroups(foundUser, request.groups()); - userRepository.save(updatedUser); + user.updateInfo(request.name(), request.birthday(), request.introduction(), request.imageUrl()); + createBirthdayEvents(user); + updateUserGroups(user, request.groups()); + } + + private void createBirthdayEvents(User user) { + eventRepository.deleteAllByUserIs(user); + List events = generatePeriodicalBirthdayEvents(user); + eventRepository.saveAll(events); + } + + private static List generatePeriodicalBirthdayEvents(User user) { + List events = new ArrayList<>(); + LocalDate birthday = user.getBirthday(); + + for (int yearAfter = 1; yearAfter <= PERIOD_UPPER_BOUND; yearAfter++) { + LocalDate birthdayYearAfter = birthday.plus(yearAfter, ChronoUnit.YEARS); + events.add(new Event("생일", birthdayYearAfter, EventType.BIRTHDAY, + "오늘 생일이에요!", user)); + } + return events; } private void updateUserGroups(User user, List groupRequests) { diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/EventRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/EventRepository.java index b2abad7..07af610 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/EventRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/EventRepository.java @@ -4,9 +4,12 @@ import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import slvtwn.khu.toyouserver.domain.Event; +import slvtwn.khu.toyouserver.domain.User; public interface EventRepository extends JpaRepository { List findEventsByDateGreaterThanEqual(LocalDate date); List findEventsByDate(LocalDate date); + + void deleteAllByUserIs(User user); }