Skip to content

Commit

Permalink
[BE] FEAT & REFACTOR : 연장권 사용 로직 추가 및 LentExtensionService로 연장권 관련 서비…
Browse files Browse the repository at this point in the history
…스 로직 이동
  • Loading branch information
LeeDaeWook committed Sep 19, 2023
1 parent 0164a6f commit 4dc1362
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@ public class UserController {

private final UserFacadeService userFacadeService;
private final UserService userService;
private final LentExtensionService lentExtensionService;

/**
* 현재 로그인한 유저의 프로필을 반환합니다. 전체 사물함 뷰에서 본인의 사물함을 표시하기 위해 사용됩니다.
Expand Down Expand Up @@ -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();
// }
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,4 +34,9 @@ public interface LentExtensionRepository extends JpaRepository<LentExtension, Lo
"AND le.expiredAt > CURRENT_TIMESTAMP " +
"ORDER BY le.expiredAt ASC")
List<LentExtension> findAllByUserIdNotExpired(Long userId);

@Query("SELECT le " +
"FROM LentExtension le " +
"WHERE le.user.userId =:userId ")
Optional<LentExtension> findOneByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -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<LentExtension> getLentExtensionByUserId(Long userId);

List<LentExtension> getLentExtensionNotExpiredByUserId(Long userId);

Page<LentExtension> getAllLentExtension(PageRequest pageable);

Page<LentExtension> getAllActiveLentExtension(PageRequest pageable);

public void issueLentExtension();

public void deleteLentExtension();

public void useLentExtension(UserSessionDto userSessionDto);
}
Original file line number Diff line number Diff line change
@@ -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<LentExtension> getAllLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAll(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> 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-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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
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;
import org.ftclub.cabinet.user.domain.AdminRole;
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;
Expand All @@ -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) {
Expand Down Expand Up @@ -244,18 +249,21 @@ 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<LentExtensionResponseDto> result = userService.getAllLentExtension(pageable).stream()
List<LentExtensionResponseDto> result = lentExtensionService.getAllLentExtension(pageable)
.stream()
.map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList());
return userMapper.toLentExtensionPaginationDto(result, (long) result.size());
}

@Override
public LentExtensionPaginationDto getAllActiveLentExtension(Integer page, Integer size) {
PageRequest pageable = PageRequest.of(page, size, Sort.by("expiredAt"));
List<LentExtensionResponseDto> result = userService.getAllActiveLentExtension(pageable)
List<LentExtensionResponseDto> result = lentExtensionService.getAllActiveLentExtension(
pageable)
.stream().map(userMapper::toLentExtensionResponseDto).collect(Collectors.toList());
return userMapper.toLentExtensionPaginationDto(result, (long) result.size());
}
Expand All @@ -264,17 +272,28 @@ public LentExtensionPaginationDto getAllActiveLentExtension(Integer page, Intege
public LentExtensionPaginationDto getMyLentExtension(UserSessionDto userSessionDto) {
log.debug("Called getMyLentExtension");
List<LentExtensionResponseDto> 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());
}

@Override
public LentExtensionPaginationDto getMyActiveLentExtension(UserSessionDto userSessionDto) {
log.debug("Called getMyActiveLentExtension");
List<LentExtensionResponseDto> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -70,16 +67,4 @@ void banUser(Long userId, LentType lentType, LocalDateTime startedAt, LocalDateT
* @return {@link List<UserBlackholeInfoDto>}
*/
List<UserBlackholeInfoDto> getAllNoRiskOfBlackholeInfo();

List<LentExtension> getLentExtensionByUserId(Long userId);

List<LentExtension> getLentExtensionNotExpiredByUserId(Long userId);

Page<LentExtension> getAllLentExtension(PageRequest pageable);

Page<LentExtension> getAllActiveLentExtension(PageRequest pageable);

public void issueLentExtension();

public void deleteLentExtension();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@
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;
import org.ftclub.cabinet.user.repository.BanHistoryRepository;
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
Expand Down Expand Up @@ -225,48 +219,4 @@ public List<UserBlackholeInfoDto> getAllNoRiskOfBlackholeInfo() {
user.getEmail(), user.getBlackholedAt()))
.collect(Collectors.toList());
}

@Override
public Page<LentExtension> getAllLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAll(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> getLentExtensionNotExpiredByUserId(Long userId) {
return lentExtensionRepository.findAllByUserIdNotExpired(userId);
}

@Override
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();
}
}

0 comments on commit 4dc1362

Please sign in to comment.