-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor: 엑셀 다운로드 따닥 방지 #1133
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
858ced7
feat: 엑셀 다운로드 중복 방지 redis 적용
dradnats1012 a1df603
feat: 엑셀 다운로드 중복 방지 로직 추가
dradnats1012 607cf47
style: 라인 포맷팅
dradnats1012 d97c673
Merge branch 'develop' into refactor/excel-duplicate
dradnats1012 c83dc01
fix: 리뷰 반영
dradnats1012 eb831c1
fix: 개행 추가
dradnats1012 2200ac9
fix: ExcelDownloadCache.from 매개변수 변경
dradnats1012 ec54e64
Merge branch 'develop' into refactor/excel-duplicate
dradnats1012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/in/koreatech/koin/domain/coop/exception/DuplicateExcelRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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초 안에 다시 보낼 수 없습니다!"; | ||
|
||
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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/in/koreatech/koin/domain/coop/model/ExcelDownloadCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/in/koreatech/koin/domain/coop/repository/ExcelDownloadCacheRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
||
|
@@ -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("시작일은 종료일 이전으로 설정해주세요."); | ||
} | ||
|
@@ -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); | ||
|
@@ -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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CstartDate, endDate만 파라미터로 전달하고, 둘을 toString하여 합치는 로직은 from 안에 넣는 건 어떨까요!? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그게 더 깔끔하고 좋을 것 같네요 👍 |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
개행 한 줄 필요할 것 같습니다