Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 유저 생일 업데이트 시 이벤트 생성 #95

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions src/main/java/slvtwn/khu/toyouserver/application/UserService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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;
import slvtwn.khu.toyouserver.dto.GroupRequest;
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;
Expand All @@ -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)
Expand All @@ -41,19 +50,6 @@ public List<UserResponse> 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<UserResponse> findAllUsersWithSameGroups(User user, String search) {
return memberRepository.findByUser(user).stream()
.map(Member::getGroup)
Expand All @@ -76,6 +72,34 @@ private List<UserResponse> findUsersWithSpecificGroup(User user, String search,
.toList();
}

@Transactional
public void updateUser(Long userId, UserUpdateRequest request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND));

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<Event> events = generatePeriodicalBirthdayEvents(user);
eventRepository.saveAll(events);
}

private static List<Event> generatePeriodicalBirthdayEvents(User user) {
List<Event> 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<GroupRequest> groupRequests) {
List<Member> members = memberRepository.findByUser(user);
memberRepository.deleteAll(members);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, Long> {

List<Event> findEventsByDateGreaterThanEqual(LocalDate date);
List<Event> findEventsByDate(LocalDate date);

void deleteAllByUserIs(User user);
}