Skip to content

Commit

Permalink
Merge pull request #1460 from innovationacademy-kr/be/dev/fix_extensi…
Browse files Browse the repository at this point in the history
…on_delete#1457

[BE] 연장권 삭제 로직 변경 및 변경에 따른 조회 로직 수정
  • Loading branch information
Ssuamje authored Nov 29, 2023
2 parents 994be2c + 5b5aed5 commit 597bada
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 397 deletions.
1 change: 1 addition & 0 deletions backend/database/cabi_local.sql
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ CREATE TABLE `lent_extension`
`type` varchar(32) not null,
`expired_at` datetime(6) not null,
`used_at` datetime(6) null,
`deleted_at` datetime(6) null,
PRIMARY KEY (lent_extension_id),
CONSTRAINT lent_extension_user_user_id_fk
FOREIGN KEY (user_id) REFERENCES user (user_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import org.ftclub.cabinet.user.domain.LentExtensionType;

@Getter
@AllArgsConstructor
@ToString
public class LentExtensionResponseDto {

private long lentExtensionId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package org.ftclub.cabinet.user.domain;

import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -21,6 +8,10 @@
import org.ftclub.cabinet.exception.DomainException;
import org.ftclub.cabinet.exception.ExceptionStatus;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Entity
@Table(name = "LENT_EXTENSION")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,60 +20,79 @@
@Log4j2
public class LentExtension {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LENT_EXTENSION_ID")
private Long lentExtensionId;

@Column(name = "name", nullable = false)
private String name;
@Column(name = "extension_period", nullable = false)
private int extensionPeriod;
@Column(name = "expired_at", nullable = false)
private LocalDateTime expiredAt;
@Enumerated(value = EnumType.STRING)
@Column(name = "type", nullable = false)
private LentExtensionType lentExtensionType;
@Column(name = "used_at")
private LocalDateTime usedAt;

@JoinColumn(name = "USER_ID", insertable = false, updatable = false)
@ManyToOne(fetch = FetchType.LAZY)
private User user;

@NotNull
@Column(name = "USER_ID", nullable = false)
private Long userId;

protected LentExtension(String name, int extensionPeriod, LocalDateTime expiredAt,
LentExtensionType lentExtensionType, Long userId) {
this.name = name;
this.extensionPeriod = extensionPeriod;
this.expiredAt = expiredAt;
this.lentExtensionType = lentExtensionType;
this.userId = userId;
}

public static LentExtension of(String name, int extensionPeriod, LocalDateTime expiredAt,
LentExtensionType lentExtensionType, Long userId) {
LentExtension lentExtension = new LentExtension(name, extensionPeriod, expiredAt,
lentExtensionType, userId);
if (!lentExtension.isValid()) {
throw new DomainException(ExceptionStatus.INVALID_STATUS);
}
return lentExtension;
}

private boolean isValid() {
return name != null && extensionPeriod > 0 && expiredAt != null &&
lentExtensionType != null && userId != null;
}

public void use() {
this.usedAt = LocalDateTime.now();
}

public int compareTo(LentExtension lentExtension) {
return this.expiredAt.compareTo(lentExtension.expiredAt);
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LENT_EXTENSION_ID")
private Long lentExtensionId;

@Column(name = "name", nullable = false)
private String name;
@Column(name = "extension_period", nullable = false)
private int extensionPeriod;
@Column(name = "expired_at", nullable = false)
private LocalDateTime expiredAt;
@Enumerated(value = EnumType.STRING)
@Column(name = "type", nullable = false)
private LentExtensionType lentExtensionType;
@Column(name = "used_at")
private LocalDateTime usedAt;
@Column(name = "deleted_at")
private LocalDateTime deletedAt;

@JoinColumn(name = "USER_ID", insertable = false, updatable = false)
@ManyToOne(fetch = FetchType.LAZY)
private User user;

@NotNull
@Column(name = "USER_ID", nullable = false)
private Long userId;

protected LentExtension(String name, int extensionPeriod, LocalDateTime expiredAt,
LentExtensionType lentExtensionType, Long userId) {
this.name = name;
this.extensionPeriod = extensionPeriod;
this.expiredAt = expiredAt;
this.lentExtensionType = lentExtensionType;
this.userId = userId;
this.deletedAt = null;
}

public static LentExtension of(String name, int extensionPeriod, LocalDateTime expiredAt,
LentExtensionType lentExtensionType, Long userId) {
LentExtension lentExtension = new LentExtension(name, extensionPeriod, expiredAt,
lentExtensionType, userId);
if (!lentExtension.isValid()) {
throw new DomainException(ExceptionStatus.INVALID_STATUS);
}
return lentExtension;
}

private boolean isValid() {
return name != null && extensionPeriod > 0 && expiredAt != null &&
lentExtensionType != null && userId != null;
}

public void use() {
this.usedAt = LocalDateTime.now();
}

public int compareTo(LentExtension lentExtension) {
return this.expiredAt.compareTo(lentExtension.expiredAt);
}

public void delete(LocalDateTime at) {
this.deletedAt = at;
}

public boolean isExpiredSince(LocalDateTime at) {
return this.expiredAt.isBefore(at);
}

public boolean isDeleted() {
return this.deletedAt != null;
}

public boolean isUsed() {
return this.usedAt != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
package org.ftclub.cabinet.user.repository;

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.ftclub.cabinet.user.domain.LentExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Log4j2
public class LentExtensionOptionalFetcher {

private final LentExtensionRepository lentExtensionRepository;
private final LentExtensionRepository lentExtensionRepository;

/*-------------------------------------------FIND-------------------------------------------*/
@Transactional(readOnly = true)
public Page<LentExtension> findAllNotDeleted(PageRequest pageable) {
return new PageImpl<>(lentExtensionRepository.findAll(pageable)
.stream().filter(e -> !e.isDeleted()).collect(Collectors.toList()));
}

/*-------------------------------------------FIND-------------------------------------------*/
@Transactional(readOnly = true)
public Page<LentExtension> findAllLentExtension(PageRequest pageable) {
return lentExtensionRepository.findAll(pageable);
}
@Transactional(readOnly = true)
public Page<LentExtension> findAllNotExpiredAndNotDeleted(PageRequest pageable) {
return lentExtensionRepository.findAllNotExpired(pageable)
.stream().filter(e -> !e.isUsed() && !e.isDeleted())
.collect(Collectors.collectingAndThen(Collectors.toList(), PageImpl::new));
}

@Transactional(readOnly = true)
public Page<LentExtension> findAllNotExpired(PageRequest pageable) {
return lentExtensionRepository.findAllNotExpired(pageable);
}
@Transactional(readOnly = true)
public List<LentExtension> findAllNotExpiredAndNotDeleted() {
return lentExtensionRepository.findAll()
.stream().filter(e -> !e.isUsed() && !e.isDeleted())
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<LentExtension> findLentExtensionByUserId(Long userId) {
return lentExtensionRepository.findAllByUserId(userId);
}
@Transactional(readOnly = true)
public List<LentExtension> findNotDeletedByUserId(Long userId) {
return lentExtensionRepository.findAllByUserId(userId)
.stream().filter(e -> !e.isDeleted()).collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<LentExtension> findActiveLentExtensionsByUserId(Long userId) {
return lentExtensionRepository.findAllByUserIdAndUsedAtIsNull(userId);
}
// Active라는 표현 자체가 비즈니스적 의미를 담고 있으므로 변경해야할 필요가 있음.
// 추후 리팩터링 필요.
@Transactional(readOnly = true)
public List<LentExtension> findActiveByUserId(Long userId) {
return lentExtensionRepository.findAllByUserIdAndUsedAtIsNull(userId)
.stream().filter(e -> !e.isDeleted()).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.ftclub.cabinet.user.service;

import java.util.List;
import org.ftclub.cabinet.dto.LentExtensionResponseDto;
import org.ftclub.cabinet.dto.UserSessionDto;

import java.util.List;

public interface LentExtensionService {

void issueLentExtension();
void issueLentExtension();

void deleteLentExtension();
void deleteExpiredExtensions();

void useLentExtension(Long userId, String username);
void useLentExtension(Long userId, String username);

void assignLentExtension(String username);
void assignLentExtension(String username);

List<LentExtensionResponseDto> getActiveLentExtensionList(UserSessionDto userSessionDto);
List<LentExtensionResponseDto> getActiveLentExtensionList(UserSessionDto userSessionDto);

LentExtensionResponseDto getActiveLentExtension(UserSessionDto userSessionDto);
LentExtensionResponseDto getActiveLentExtension(UserSessionDto userSessionDto);


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.ftclub.cabinet.user.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
Expand All @@ -18,16 +14,21 @@
import org.ftclub.cabinet.lent.repository.LentOptionalFetcher;
import org.ftclub.cabinet.mapper.UserMapper;
import org.ftclub.cabinet.occupiedtime.OccupiedTimeManager;
import org.ftclub.cabinet.user.domain.LentExtensions;
import org.ftclub.cabinet.user.domain.LentExtension;
import org.ftclub.cabinet.user.domain.LentExtensionPolicy;
import org.ftclub.cabinet.user.domain.LentExtensionType;
import org.ftclub.cabinet.user.domain.LentExtensions;
import org.ftclub.cabinet.user.repository.LentExtensionOptionalFetcher;
import org.ftclub.cabinet.user.repository.LentExtensionRepository;
import org.ftclub.cabinet.user.repository.UserOptionalFetcher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
Expand Down Expand Up @@ -83,7 +84,7 @@ public List<LentExtensionResponseDto> getActiveLentExtensionList(
log.debug("Called getLentExtensionList {}", userSessionDto.getName());

LentExtensions lentExtensions = LentExtensions.builder()
.lentExtensions(lentExtensionOptionalFetcher.findActiveLentExtensionsByUserId(
.lentExtensions(lentExtensionOptionalFetcher.findActiveByUserId(
userSessionDto.getUserId())).build();

return lentExtensions.getLentExtensions().stream()
Expand All @@ -93,7 +94,7 @@ public List<LentExtensionResponseDto> getActiveLentExtensionList(
@Override
public LentExtensionResponseDto getActiveLentExtension(UserSessionDto userSessionDto) {
LentExtensionResponseDto lentExtensionResponseDto = null;
List<LentExtension> activeLentExtensionsByUserId = lentExtensionOptionalFetcher.findActiveLentExtensionsByUserId(
List<LentExtension> activeLentExtensionsByUserId = lentExtensionOptionalFetcher.findActiveByUserId(
userSessionDto.getUserId());
LentExtensions lentExtensions = LentExtensions.builder()
.lentExtensions(activeLentExtensionsByUserId).build();
Expand All @@ -106,17 +107,21 @@ public LentExtensionResponseDto getActiveLentExtension(UserSessionDto userSessio

@Override
@Scheduled(cron = "${spring.schedule.cron.extension-delete-time}")
public void deleteLentExtension() {
public void deleteExpiredExtensions() {
log.debug("Called deleteExtension");
lentExtensionRepository.deleteAll();
LocalDateTime now = LocalDateTime.now();

lentExtensionOptionalFetcher.findAllNotExpiredAndNotDeleted().stream()
.filter(e -> e.isExpiredSince(now))
.forEach(e -> e.delete(now));
}

@Override
public void useLentExtension(Long userId, String username) {
log.debug("Called useLentExtension {}", username);

List<LentExtension> findLentExtension =
lentExtensionOptionalFetcher.findLentExtensionByUserId(userId);
lentExtensionOptionalFetcher.findNotDeletedByUserId(userId);

LentExtensions lentExtensions = LentExtensions.builder().lentExtensions(findLentExtension)
.build();
Expand Down
Loading

0 comments on commit 597bada

Please sign in to comment.