-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#33] 경매 활성화 및 종료 상태 변경 코드 추가 #34
Changes from 3 commits
58acb41
6aa40c2
9e26ab8
38681ba
c1acfd6
4ee090d
10aa692
07462f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.readyauction.batch.job; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.example.readyauction.domain.product.Product; | ||
import com.example.readyauction.domain.product.ProductCondition; | ||
|
||
@Component | ||
public class ProductConditionUpdate implements ItemProcessor<Product, Product> { | ||
// 상품의 상태를 갱신하는 로직 | ||
@Override | ||
public Product process(Product product) throws Exception { | ||
LocalDateTime now = LocalDateTime.now(); | ||
// 경매 상태 업데이트 | ||
if (now.isBefore(product.getStartDate())) { | ||
product.updateProductCondition(ProductCondition.READY); // 경매 대기중 | ||
} else if (now.isAfter(product.getCloseDate())) { | ||
product.updateProductCondition(ProductCondition.DONE); // 경매 종료됨 | ||
} else { | ||
product.updateProductCondition(ProductCondition.ACTIVE); // 경매 진행중 | ||
} | ||
return product; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.readyauction.batch.job; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.batch.item.Chunk; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.example.readyauction.domain.product.Product; | ||
import com.example.readyauction.repository.product.ProductRepository; | ||
|
||
@Component | ||
public class ProductConditionWriter implements ItemWriter<Product> { | ||
|
||
private final ProductRepository productRepository; | ||
|
||
public ProductConditionWriter(ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
@Override | ||
public void write(Chunk<? extends Product> chunk) throws Exception { | ||
List<? extends Product> products = chunk.getItems(); | ||
productRepository.saveAll(products); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.readyauction.batch.job; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.NonTransientResourceException; | ||
import org.springframework.batch.item.ParseException; | ||
import org.springframework.batch.item.UnexpectedInputException; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.example.readyauction.domain.product.Product; | ||
import com.example.readyauction.repository.product.ProductRepository; | ||
|
||
@Component | ||
public class ProductReader implements ItemReader<List<Product>> { | ||
private static final int PAGE_SIZE = 500; // 한번에 읽을 데이터 개수 (청크 사이즈) | ||
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. 요거 CHUNK_SIZE를 같이 사용하는게 어떨까요 ? 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. 아하 넵!! |
||
private final ProductRepository productRepository; | ||
private int currentPage = 0; | ||
|
||
public ProductReader(ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
@Override | ||
public List<Product> read() throws | ||
Exception, | ||
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. 예외 목록이 약간 읽기 어렵게 줄바꿈이 된 것 같아요 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. 요게 저두 불편한데,, 줄바꿈을 제가 바꿔두 save 하면 저런식으로 줄바꿈이 되더라구용,,,🥲 |
||
UnexpectedInputException, | ||
ParseException, | ||
NonTransientResourceException { | ||
Pageable pageable = PageRequest.of(currentPage, PAGE_SIZE); | ||
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. 전체를 다 읽는거라 데이터가 많아지면 점점 느려질 것 같아요, more 방식으로 읽도록 변경하는게 어떨까요 ? 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. 아 그렇네요!! more 방식으로 수정하도록 하겠습니다! |
||
List<Product> products = productRepository.findAll(pageable).getContent(); | ||
|
||
if (products.isEmpty()) { | ||
currentPage = 0; | ||
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. 요거는 혹시 무슨 의도인가요 ? 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. 엇 이게 DB에서 더이상 읽어올게없으면 다시 처음부터 읽을 수 있도록 pageNo를 0으로 하는 코드였습니다! |
||
return null; | ||
} | ||
currentPage++; | ||
return products; | ||
} | ||
} |
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.
요거 그냥 Writer에서 하고 Processor 빼도 될거같아요 ?
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.
넵 수정하도록 하겠습니다!