delete scheduler

This commit is contained in:
jinho jeong
2022-06-01 14:17:52 +09:00
parent 25016d3a2a
commit 2bfb8b7edb
5 changed files with 17 additions and 12 deletions

View File

@@ -16,5 +16,5 @@ public interface PostCommandRepository extends CrudRepository<Post, Long> {
void deleteById(Long id);
void delete(Post post);
List<Post> findAllByExpiredAt(LocalDateTime expiredAt);
List<Post> findAllByExpiredAtLessThanAndDeletedAtIsNull(LocalDateTime expiredAt);
}

View File

@@ -44,7 +44,7 @@ public class Post implements Serializable {
public Post(Long id, LocalDateTime createdAt, LocalDateTime expiredAt, String content, UserEntity writer){
this.id = id;
this.createdAt = createdAt;
this.expiredAt = expiredAt.plusHours(24);
this.expiredAt = expiredAt;
this.deletedAt = null;
this.content = content;
this.writer = writer;

View File

@@ -1,5 +1,7 @@
package com.example.oneul.domain.post.service.command;
import java.time.LocalDateTime;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.post.dao.PostCommandRepository;
@@ -26,10 +28,13 @@ public class PostCommnadServiceImpl implements PostCommandService{
@Override
public Post createPost(Post post, HttpSession httpSession){
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
LocalDateTime createdAt = LocalDateTime.now();
Post postEntity = postCommandRepository.save(
Post.builder()
.content(post.getContent())
.createdAt(createdAt)
.expiredAt(createdAt.plusHours(24))
.writer(userEntity)
.build());

View File

@@ -47,12 +47,12 @@ public class BatchConfig {
return stepBuilderFactory.get("step")
.tasklet((contribution, chunkContext) -> {
LocalDateTime expiredAt = LocalDateTime.now();
List<Post> posts = postCommandRepository.findAllByExpiredAt(expiredAt);
// posts.stream().forEach(post -> {
// post.setDeletedAt(expiredAt);
// });
// // TODO: Query DB 한테 알려줘야함
// postCommandRepository.saveAll(posts);
List<Post> posts = postCommandRepository.findAllByExpiredAtLessThanAndDeletedAtIsNull(expiredAt);
posts.stream().forEach(post -> {
post.setDeletedAt(expiredAt);
});
// TODO: Query DB 한테 알려줘야함
postCommandRepository.saveAll(posts);
log.info(expiredAt + " posts(" + posts.size() + ") are deleted.");
return RepeatStatus.FINISHED;
}).build();

View File

@@ -20,10 +20,9 @@ public class LoginCheckInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("login check prehandler");
HttpSession httpSession = request.getSession(false);
if(httpSession == null){
log.info("no session");
log.info("sessionless user access");
response.sendRedirect(loginPage);
return false;
}
@@ -35,6 +34,7 @@ public class LoginCheckInterceptor implements HandlerInterceptor{
response.sendRedirect(loginPage);
return false;
}
log.info(userEntity.toString() + " is access");
return true;
}
}