diff --git a/backend/src/main/java/org/ftclub/cabinet/user/controller/UserController.java b/backend/src/main/java/org/ftclub/cabinet/user/controller/UserController.java index f558ed324..0c4cb3207 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/controller/UserController.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/controller/UserController.java @@ -8,6 +8,7 @@ import org.ftclub.cabinet.dto.MyProfileResponseDto; import org.ftclub.cabinet.dto.UserSessionDto; import org.ftclub.cabinet.user.domain.UserSession; +import org.ftclub.cabinet.user.service.LentExtensionService; import org.ftclub.cabinet.user.service.UserFacadeService; import org.ftclub.cabinet.user.service.UserService; import org.springframework.web.bind.annotation.GetMapping; @@ -25,6 +26,7 @@ public class UserController { private final UserFacadeService userFacadeService; private final UserService userService; + private final LentExtensionService lentExtensionService; /** * 현재 로그인한 유저의 프로필을 반환합니다. 전체 사물함 뷰에서 본인의 사물함을 표시하기 위해 사용됩니다. @@ -55,10 +57,18 @@ public LentExtensionPaginationDto getMyActiveLentExtension( return userFacadeService.getMyActiveLentExtension(userSessionDto); } + @GetMapping("/me/lent-extensions/use") + @AuthGuard(level = AuthLevel.USER_ONLY) + public void useLentExtension( + @UserSession UserSessionDto userSessionDto) { + log.info("Called useLentExtension"); + userFacadeService.useLentExtension(userSessionDto); + } + // @GetMapping("/me/lent-extensions/test") // public void testIssueLentExtension() { // log.info("Called testIssueLentExtension"); -// userService.issueLentExtension(); -// userService.deleteLentExtension(); +// lentExtensionService.issueLentExtension(); +//// lentExtensionService.deleteLentExtension(); // } } diff --git a/backend/src/main/java/org/ftclub/cabinet/user/repository/LentExtensionRepository.java b/backend/src/main/java/org/ftclub/cabinet/user/repository/LentExtensionRepository.java index c03f086d2..4ad201101 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/repository/LentExtensionRepository.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/repository/LentExtensionRepository.java @@ -1,6 +1,7 @@ package org.ftclub.cabinet.user.repository; import java.util.List; +import java.util.Optional; import org.ftclub.cabinet.user.domain.LentExtension; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -33,4 +34,9 @@ public interface LentExtensionRepository extends JpaRepository CURRENT_TIMESTAMP " + "ORDER BY le.expiredAt ASC") List findAllByUserIdNotExpired(Long userId); + + @Query("SELECT le " + + "FROM LentExtension le " + + "WHERE le.user.userId =:userId ") + Optional findOneByUserId(Long userId); } diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionService.java b/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionService.java new file mode 100644 index 000000000..8ccc2d605 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionService.java @@ -0,0 +1,24 @@ +package org.ftclub.cabinet.user.service; + +import java.util.List; +import org.ftclub.cabinet.dto.UserSessionDto; +import org.ftclub.cabinet.user.domain.LentExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +public interface LentExtensionService { + + List getLentExtensionByUserId(Long userId); + + List getLentExtensionNotExpiredByUserId(Long userId); + + Page getAllLentExtension(PageRequest pageable); + + Page getAllActiveLentExtension(PageRequest pageable); + + public void issueLentExtension(); + + public void deleteLentExtension(); + + public void useLentExtension(UserSessionDto userSessionDto); +} diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionServiceImpl.java new file mode 100644 index 000000000..2bc1d6642 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/LentExtensionServiceImpl.java @@ -0,0 +1,89 @@ +package org.ftclub.cabinet.user.service; + +import java.time.LocalDateTime; +import java.util.List; +import javax.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.ftclub.cabinet.config.CabinetProperties; +import org.ftclub.cabinet.dto.UserSessionDto; +import org.ftclub.cabinet.lent.domain.LentHistory; +import org.ftclub.cabinet.lent.repository.LentOptionalFetcher; +import org.ftclub.cabinet.occupiedtime.OccupiedTimeManager; +import org.ftclub.cabinet.occupiedtime.UserMonthDataDto; +import org.ftclub.cabinet.user.domain.LentExtension; +import org.ftclub.cabinet.user.domain.LentExtensionType; +import org.ftclub.cabinet.user.repository.LentExtensionRepository; +import org.ftclub.cabinet.user.repository.UserOptionalFetcher; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +@Log4j2 +public class LentExtensionServiceImpl implements LentExtensionService { + + private final LentExtensionRepository lentExtensionRepository; + private final LentOptionalFetcher lentOptionalFetcher; + private final UserOptionalFetcher userOptionalFetcher; + private final CabinetProperties cabinetProperties; + private final OccupiedTimeManager occupiedTimeManager; + + @Override + public Page getAllLentExtension(PageRequest pageable) { + return lentExtensionRepository.findAll(pageable); + } + + @Override + public Page getAllActiveLentExtension(PageRequest pageable) { + return lentExtensionRepository.findAllNotExpired(pageable); + } + + @Override + public List getLentExtensionByUserId(Long userId) { + return lentExtensionRepository.findAllByUserId(userId); + } + + @Override + public List getLentExtensionNotExpiredByUserId(Long userId) { + return lentExtensionRepository.findAllByUserIdNotExpired(userId); + } + + @Override + @Scheduled(cron = "${spring.schedule.cron.extension-issue-time}") + public void issueLentExtension() { + log.debug("Called issueLentExtension"); + List userMonthDataDtos = occupiedTimeManager.metLimitTimeUser( + occupiedTimeManager.getUserLastMonthOccupiedTime()); + LocalDateTime now = LocalDateTime.now(); + userMonthDataDtos.stream().forEach(userMonthDataDto -> { + LentExtension lentExtension = LentExtension.of("lentExtension", + cabinetProperties.getLentExtendTerm(), + LocalDateTime.of(now.getYear(), now.getMonth(), + now.getMonth().length(now.toLocalDate().isLeapYear()), 23, 59, 0), + LentExtensionType.ALL, + userOptionalFetcher.findUserByName(userMonthDataDto.getLogin()).getUserId()); + lentExtensionRepository.save(lentExtension); + }); + } + + @Override + @Scheduled(cron = "${spring.schedule.cron.extension-delete-time}") + public void deleteLentExtension() { + log.debug("Called deleteExtension"); + lentExtensionRepository.deleteAll(); + } + + @Override + public void useLentExtension(UserSessionDto userSessionDto) { + log.debug("Called useLentExtension"); + + LentHistory lentHistory = lentOptionalFetcher.getActiveLentHistoryWithUserId( + userSessionDto.getUserId()); + lentHistory.setExpiredAt( + lentHistory.getExpiredAt().plusDays(cabinetProperties.getLentExtendTerm())); + } +} diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeService.java b/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeService.java index 663f87473..67f9c0b9e 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeService.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeService.java @@ -196,4 +196,6 @@ void banUser(Long userId, LentType lentType, LocalDateTime startedAt, LocalDateT LentExtensionPaginationDto getMyLentExtension(UserSessionDto userSessionDto); LentExtensionPaginationDto getMyActiveLentExtension(UserSessionDto userSessionDto); + + void useLentExtension(UserSessionDto userSessionDto); } diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeServiceImpl.java index 391d05c57..fb4bfb6e1 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/UserFacadeServiceImpl.java @@ -23,6 +23,8 @@ import org.ftclub.cabinet.dto.UserProfileDto; import org.ftclub.cabinet.dto.UserProfilePaginationDto; import org.ftclub.cabinet.dto.UserSessionDto; +import org.ftclub.cabinet.exception.ExceptionStatus; +import org.ftclub.cabinet.exception.ServiceException; import org.ftclub.cabinet.lent.repository.LentOptionalFetcher; import org.ftclub.cabinet.mapper.CabinetMapper; import org.ftclub.cabinet.mapper.UserMapper; @@ -30,6 +32,7 @@ import org.ftclub.cabinet.user.domain.BanHistory; import org.ftclub.cabinet.user.domain.User; import org.ftclub.cabinet.user.domain.UserRole; +import org.ftclub.cabinet.user.repository.LentExtensionRepository; import org.ftclub.cabinet.user.repository.UserOptionalFetcher; import org.ftclub.cabinet.utils.DateUtil; import org.springframework.data.domain.Page; @@ -48,6 +51,8 @@ public class UserFacadeServiceImpl implements UserFacadeService { private final UserMapper userMapper; private final CabinetOptionalFetcher cabinetOptionalFetcher; private final CabinetMapper cabinetMapper; + private final LentExtensionService lentExtensionService; + private final LentExtensionRepository lentExtensionRepository; @Override public MyProfileResponseDto getMyProfile(UserSessionDto user) { @@ -244,10 +249,12 @@ public void updateClubUser(Long clubId, String clubName) { log.debug("Called updateClubUser"); userService.updateClubUser(clubId, clubName); } + @Override public LentExtensionPaginationDto getAllLentExtension(Integer page, Integer size) { PageRequest pageable = PageRequest.of(page, size, Sort.by("expiredAt")); - List result = userService.getAllLentExtension(pageable).stream() + List result = lentExtensionService.getAllLentExtension(pageable) + .stream() .map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList()); return userMapper.toLentExtensionPaginationDto(result, (long) result.size()); } @@ -255,7 +262,8 @@ public LentExtensionPaginationDto getAllLentExtension(Integer page, Integer size @Override public LentExtensionPaginationDto getAllActiveLentExtension(Integer page, Integer size) { PageRequest pageable = PageRequest.of(page, size, Sort.by("expiredAt")); - List result = userService.getAllActiveLentExtension(pageable) + List result = lentExtensionService.getAllActiveLentExtension( + pageable) .stream().map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList()); return userMapper.toLentExtensionPaginationDto(result, (long) result.size()); } @@ -264,8 +272,9 @@ public LentExtensionPaginationDto getAllActiveLentExtension(Integer page, Intege public LentExtensionPaginationDto getMyLentExtension(UserSessionDto userSessionDto) { log.debug("Called getMyLentExtension"); List result = - userService.getLentExtensionByUserId(userSessionDto.getUserId()) - .stream().map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList()); + lentExtensionService.getLentExtensionByUserId(userSessionDto.getUserId()) + .stream().map(userMapper::toLentExtensionResponseDto) + .collect(Collectors.toList()); return userMapper.toLentExtensionPaginationDto(result, (long) result.size()); } @@ -273,8 +282,18 @@ public LentExtensionPaginationDto getMyLentExtension(UserSessionDto userSessionD public LentExtensionPaginationDto getMyActiveLentExtension(UserSessionDto userSessionDto) { log.debug("Called getMyActiveLentExtension"); List result = - userService.getLentExtensionNotExpiredByUserId(userSessionDto.getUserId()) - .stream().map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList()); + lentExtensionService.getLentExtensionNotExpiredByUserId(userSessionDto.getUserId()) + .stream().map(userMapper::toLentExtensionResponseDto) + .collect(Collectors.toList()); return userMapper.toLentExtensionPaginationDto(result, (long) result.size()); } + + @Override + public void useLentExtension(UserSessionDto userSessionDto) { + log.debug("Called useLentExtension"); + lentExtensionRepository.findOneByUserId(userSessionDto.getUserId()) + .orElseThrow( + () -> new ServiceException(ExceptionStatus.EXTENSION_TICKET_NOT_FOUND)); + lentExtensionService.useLentExtension(userSessionDto); + } } diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/UserService.java b/backend/src/main/java/org/ftclub/cabinet/user/service/UserService.java index f1202116e..f268d754b 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/service/UserService.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/UserService.java @@ -5,10 +5,7 @@ import org.ftclub.cabinet.cabinet.domain.LentType; import org.ftclub.cabinet.dto.UserBlackholeInfoDto; import org.ftclub.cabinet.user.domain.AdminRole; -import org.ftclub.cabinet.user.domain.LentExtension; import org.ftclub.cabinet.user.domain.UserRole; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; public interface UserService { @@ -70,16 +67,4 @@ void banUser(Long userId, LentType lentType, LocalDateTime startedAt, LocalDateT * @return {@link List} */ List getAllNoRiskOfBlackholeInfo(); - - List getLentExtensionByUserId(Long userId); - - List getLentExtensionNotExpiredByUserId(Long userId); - - Page getAllLentExtension(PageRequest pageable); - - Page getAllActiveLentExtension(PageRequest pageable); - - public void issueLentExtension(); - - public void deleteLentExtension(); } diff --git a/backend/src/main/java/org/ftclub/cabinet/user/service/UserServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/user/service/UserServiceImpl.java index 2e1c511b7..c62978d1d 100644 --- a/backend/src/main/java/org/ftclub/cabinet/user/service/UserServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/user/service/UserServiceImpl.java @@ -17,14 +17,11 @@ import org.ftclub.cabinet.exception.ServiceException; import org.ftclub.cabinet.lent.repository.LentOptionalFetcher; import org.ftclub.cabinet.occupiedtime.OccupiedTimeManager; -import org.ftclub.cabinet.occupiedtime.UserMonthDataDto; import org.ftclub.cabinet.user.domain.AdminRole; import org.ftclub.cabinet.user.domain.AdminUser; import org.ftclub.cabinet.user.domain.BanHistory; import org.ftclub.cabinet.user.domain.BanPolicy; import org.ftclub.cabinet.user.domain.BanType; -import org.ftclub.cabinet.user.domain.LentExtension; -import org.ftclub.cabinet.user.domain.LentExtensionType; import org.ftclub.cabinet.user.domain.User; import org.ftclub.cabinet.user.domain.UserRole; import org.ftclub.cabinet.user.repository.AdminUserRepository; @@ -32,9 +29,6 @@ import org.ftclub.cabinet.user.repository.LentExtensionRepository; import org.ftclub.cabinet.user.repository.UserOptionalFetcher; import org.ftclub.cabinet.user.repository.UserRepository; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service @@ -225,48 +219,4 @@ public List getAllNoRiskOfBlackholeInfo() { user.getEmail(), user.getBlackholedAt())) .collect(Collectors.toList()); } - - @Override - public Page getAllLentExtension(PageRequest pageable) { - return lentExtensionRepository.findAll(pageable); - } - - @Override - public Page getAllActiveLentExtension(PageRequest pageable) { - return lentExtensionRepository.findAllNotExpired(pageable); - } - - @Override - public List getLentExtensionByUserId(Long userId) { - return lentExtensionRepository.findAllByUserId(userId); - } - - @Override - public List getLentExtensionNotExpiredByUserId(Long userId) { - return lentExtensionRepository.findAllByUserIdNotExpired(userId); - } - - @Override - public void issueLentExtension() { - log.debug("Called issueLentExtension"); - List userMonthDataDtos = occupiedTimeManager.metLimitTimeUser( - occupiedTimeManager.getUserLastMonthOccupiedTime()); - LocalDateTime now = LocalDateTime.now(); - userMonthDataDtos.stream().forEach(userMonthDataDto -> { - LentExtension lentExtension = LentExtension.of("lentExtension", - cabinetProperties.getLentExtendTerm(), - LocalDateTime.of(now.getYear(), now.getMonth(), - now.getMonth().length(now.toLocalDate().isLeapYear()), 23, 59, 0), - LentExtensionType.ALL, - userOptionalFetcher.findUserByName(userMonthDataDto.getLogin()).getUserId()); - lentExtensionRepository.save(lentExtension); - }); - } - - @Override - @Scheduled(cron = "${spring.schedule.cron.extension-delete-time}") - public void deleteLentExtension() { - log.debug("Called deleteExtension"); - lentExtensionRepository.deleteAll(); - } }