Skip to content

Commit

Permalink
[BE]REFACTOR : optional fetcher 구조로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
enaenen committed Oct 15, 2023
1 parent 3b0703b commit fd17c8c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ftclub.cabinet.user.repository;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.ftclub.cabinet.exception.ExceptionStatus;
import org.ftclub.cabinet.exception.ServiceException;
import org.ftclub.cabinet.user.domain.LentExtension;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Log4j2
public class LentExtensionOptionalFetcher {

private final LentExtensionRepository lentExtensionRepository;

/*-------------------------------------------FIND-------------------------------------------*/
/*-------------------------------------------GET--------------------------------------------*/
public LentExtension getAvailableLentExtensionByUserId(Long userId) {
log.debug("Called findLentExtensionByUserId: {}", userId);
return lentExtensionRepository.findLentExtensionByUserIdAndUsedAtIsNull(userId).orElseThrow(
() -> new ServiceException(ExceptionStatus.EXTENSION_TICKET_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public interface LentExtensionRepository extends JpaRepository<LentExtension, Lo
"WHERE le.user.userId =:userId ")
Optional<LentExtension> findOneByUserId(Long userId);

// @Query("DELETE le " +
// "FROM LentExtension le " +
// "WHERE d.user.userId =:userId ")
// void deleteByUserId(Long userId);
Optional<LentExtension> findLentExtensionByUserIdAndUsedAtIsNull(Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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.LentExtensionOptionalFetcher;
import org.ftclub.cabinet.user.repository.LentExtensionRepository;
import org.ftclub.cabinet.user.repository.UserOptionalFetcher;
import org.springframework.data.domain.Page;
Expand All @@ -26,64 +27,83 @@
@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;
private final LentExtensionRepository lentExtensionRepository;
private final LentExtensionOptionalFetcher lentExtensionOptionalFetcher;
private final LentOptionalFetcher lentOptionalFetcher;
private final UserOptionalFetcher userOptionalFetcher;
private final CabinetProperties cabinetProperties;
private final OccupiedTimeManager occupiedTimeManager;

@Override
public Page<LentExtension> getAllLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAll(pageable);
}
@Override
public Page<LentExtension> getAllLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAll(pageable);
}

@Override
public Page<LentExtension> getAllActiveLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAllNotExpired(pageable);
}
@Override
public Page<LentExtension> getAllActiveLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAllNotExpired(pageable);
}

@Override
public List<LentExtension> getLentExtensionByUserId(Long userId) {
return lentExtensionRepository.findAllByUserId(userId);
}
@Override
public List<LentExtension> getLentExtensionByUserId(Long userId) {
return lentExtensionRepository.findAllByUserId(userId);
}

@Override
public List<LentExtension> getLentExtensionNotExpiredByUserId(Long userId) {
return lentExtensionRepository.findAllByUserIdNotExpired(userId);
}
@Override
public List<LentExtension> getLentExtensionNotExpiredByUserId(Long userId) {
return lentExtensionRepository.findAllByUserIdNotExpired(userId);
}

@Override
@Scheduled(cron = "${spring.schedule.cron.extension-issue-time}")
public void issueLentExtension() {
log.debug("Called issueLentExtension");
List<UserMonthDataDto> 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-issue-time}")
public void issueLentExtension() {
log.debug("Called issueLentExtension");
List<UserMonthDataDto> 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 assignLentExtension(String username) {
log.debug("Called assignLentExtension {}", username);
LocalDateTime now = LocalDateTime.now();

@Override
public void useLentExtension(UserSessionDto userSessionDto) {
log.debug("Called useLentExtension");
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(username).getUserId());
lentExtensionRepository.save(lentExtension);
}

LentHistory lentHistory = lentOptionalFetcher.getActiveLentHistoryWithUserId(
userSessionDto.getUserId());
lentHistory.setExpiredAt(
lentHistory.getExpiredAt().plusDays(cabinetProperties.getLentExtendTerm()));
}
@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 {}", userSessionDto);

LentHistory lentHistory = lentOptionalFetcher.getActiveLentHistoryWithUserId(
userSessionDto.getUserId());
LentExtension findLentExtension = lentExtensionOptionalFetcher.getAvailableLentExtensionByUserId(
userSessionDto.getUserId());

findLentExtension.use();
long extensionPeriod = findLentExtension.getExtensionPeriod();
lentHistory.setExpiredAt(lentHistory.getExpiredAt().plusDays(extensionPeriod));
}
}

0 comments on commit fd17c8c

Please sign in to comment.