@EnableScheduling
@SpringBootApplication
public class BoardApplication {
public static void main(String[] args) {
SpringApplication.run(BoardApplication.class, args);
}
}
@Component
@Transactional
public class PostScheduler {
private final PostRepository postRepository;
@Autowired
public PostScheduler(PostRepository postRepository){
this.postRepository = postRepository;
}
@Scheduled(cron = "0 0/1 * * * *")
public void postScheduler(){
System.out.println("=== μ€μΌμ€λ¬ μμ ===");
Page<Post> posts = postRepository.findAllByAppointment(Pageable.unpaged(),"Y");
for (Post p : posts.getContent()){
LocalDateTime now = LocalDateTime.now();
if(p.getAppointmentTime().isBefore(now) ){
p.updateAppointment(null);
}
}
System.out.println("=== μ€μΌμ€λ¬ λ ===");
}
}
public String pagePostList(Model model , @PageableDefault(size = 10, sort = "updatedTime", direction = Sort.Direction.DESC) Pageable pageable){
Page<PostListDto> postListDtoList = postService.pageFindAll(pageable);
model.addAttribute("postList", postListDtoList);
return "/post/post-list";
}
public Page<PostListDto> pageFindAll(Pageable pageable){
Page<Post> PostList = postRepository.findAllByAppointment(pageable, null);
Page<PostListDto> listDto = PostList.map(
p-> new PostListDto(
p.getId(),
p.getTitle(),
p.getAuthor() == null ? "μ΅λͺ
μ μ " : p.getAuthor().getEmail(),
p.getAppointment(),
p.getAppointmentTime()
)
);
return listDto;
}
Page<Post> findAllByAppointment(Pageable pageable , String appointment);