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

refactor: 엑셀 다운로드 따닥 방지 #1133

Merged
merged 8 commits into from
Dec 15, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package in.koreatech.koin.domain.coop.exception;

import java.time.LocalDate;

import in.koreatech.koin.global.exception.DuplicationException;

public class DuplicateExcelRequestException extends DuplicationException {
private static final String DEFAULT_MESSAGE = "동일한 요청을 30초 안에 다시 보낼 수 없습니다!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개행 한 줄 필요할 것 같습니다


public DuplicateExcelRequestException(String message) {
super(message);
}

public DuplicateExcelRequestException(String message, String detail) {
super(message, detail);
}

public static DuplicateExcelRequestException withDetail(LocalDate startDate, LocalDate endDate) {
return new DuplicateExcelRequestException(DEFAULT_MESSAGE,
"startDate: '" + startDate + "'" + "endDate: " + endDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package in.koreatech.koin.domain.coop.model;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;

import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;

@Getter
@RedisHash(value = "excelDownload")
public class ExcelDownloadCache {

private static final long CACHE_EXPIRE_SECONDS = 30L;

@Id
private String id;

@TimeToLive(unit = TimeUnit.SECONDS)
private final Long expiration;

@Builder
private ExcelDownloadCache(String id, Long expiration) {
this.id = id;
this.expiration = expiration;
}

public static ExcelDownloadCache from(String id) {
return ExcelDownloadCache.builder()
.id(id)
.expiration(CACHE_EXPIRE_SECONDS)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package in.koreatech.koin.domain.coop.repository;

import java.util.Optional;

import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.coop.model.ExcelDownloadCache;

public interface ExcelDownloadCacheRepository extends Repository<ExcelDownloadCache, String> {

ExcelDownloadCache save(ExcelDownloadCache excelDownloadCache);

Optional<ExcelDownloadCache> findById(String id);

boolean existsById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
import in.koreatech.koin.domain.coop.dto.SoldOutRequest;
import in.koreatech.koin.domain.coop.exception.DiningLimitDateException;
import in.koreatech.koin.domain.coop.exception.DiningNowDateException;
import in.koreatech.koin.domain.coop.exception.DuplicateExcelRequestException;
import in.koreatech.koin.domain.coop.exception.StartDateAfterEndDateException;
import in.koreatech.koin.domain.coop.model.Coop;
import in.koreatech.koin.domain.coop.model.DiningImageUploadEvent;
import in.koreatech.koin.domain.coop.model.DiningNotifyCache;
import in.koreatech.koin.domain.coop.model.DiningSoldOutEvent;
import in.koreatech.koin.domain.coop.model.ExcelDownloadCache;
import in.koreatech.koin.domain.coop.repository.CoopRepository;
import in.koreatech.koin.domain.coop.repository.DiningNotifyCacheRepository;
import in.koreatech.koin.domain.coop.repository.DiningSoldOutCacheRepository;
import in.koreatech.koin.domain.coop.repository.ExcelDownloadCacheRepository;
import in.koreatech.koin.domain.coopshop.model.CoopShopType;
import in.koreatech.koin.domain.coopshop.service.CoopShopService;
import in.koreatech.koin.domain.dining.model.Dining;
Expand All @@ -65,6 +68,7 @@ public class CoopService {
private final ApplicationEventPublisher eventPublisher;
private final DiningRepository diningRepository;
private final DiningSoldOutCacheRepository diningSoldOutCacheRepository;
private final ExcelDownloadCacheRepository excelDownloadCacheRepository;
private final DiningNotifyCacheRepository diningNotifyCacheRepository;
private final CoopRepository coopRepository;
private final UserTokenRepository userTokenRepository;
Expand Down Expand Up @@ -166,6 +170,7 @@ public CoopLoginResponse coopLogin(CoopLoginRequest request) {
}

public ByteArrayInputStream generateDiningExcel(LocalDate startDate, LocalDate endDate, Boolean isCafeteria) {
checkDuplicateExcelRequest(startDate, endDate);
validateDates(startDate, endDate);
List<Dining> dinings = fetchDiningData(startDate, endDate, isCafeteria);

Expand All @@ -192,7 +197,6 @@ private void validateDates(LocalDate startDate, LocalDate endDate) {
if (startDate.isAfter(today) || endDate.isAfter(today)) {
throw new DiningNowDateException("오늘 날짜 이후 기간은 설정할 수 없어요.");
}

if (startDate.isAfter(endDate)) {
throw new StartDateAfterEndDateException("시작일은 종료일 이전으로 설정해주세요.");
}
Expand Down Expand Up @@ -265,10 +269,10 @@ private void fillDiningRow(Dining dining, Row row, CellStyle commonStyle) {
row.createCell(6).setCellValue(Optional.ofNullable(dining.getSoldOut()).map(Object::toString).orElse(""));
row.createCell(7).setCellValue(Optional.ofNullable(dining.getIsChanged()).map(Object::toString).orElse(""));

for (int i = 0; i < EXCEL_COLUMN_COUNT; i++) {
row.getCell(i).setCellStyle(commonStyle);
}
for (int i = 0; i < EXCEL_COLUMN_COUNT; i++) {
row.getCell(i).setCellStyle(commonStyle);
}
}

private String formatMenu(List<String> menu) {
return String.join("\n", menu);
Expand All @@ -281,4 +285,13 @@ private ByteArrayInputStream writeWorkbookToStream(SXSSFWorkbook workbook) throw
return new ByteArrayInputStream(out.toByteArray());
}
}

private void checkDuplicateExcelRequest(LocalDate startDate, LocalDate endDate) {
boolean isCacheExist = excelDownloadCacheRepository.existsById(startDate.toString() + endDate.toString());

if (isCacheExist) {
throw DuplicateExcelRequestException.withDetail(startDate, endDate);
}
excelDownloadCacheRepository.save(ExcelDownloadCache.from(startDate.toString() + endDate.toString()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

startDate, endDate만 파라미터로 전달하고, 둘을 toString하여 합치는 로직은 from 안에 넣는 건 어떨까요!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그게 더 깔끔하고 좋을 것 같네요 👍

}
}
Loading