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] 게시글 softdelete 적용 #132 #142

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import com.dontbe.www.DontBeServer.common.entity.BaseTimeEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -18,7 +19,11 @@
@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@Where(clause = "is_deleted = false")
public class Content extends BaseTimeEntity {

private static final long CONTENT_RETENTION_PERIOD = 14L; // 게시글 삭제 후 보유기간 14일로 설정

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -35,9 +40,21 @@ public class Content extends BaseTimeEntity {

@OneToMany(mappedBy = "content", cascade = CascadeType.REMOVE)
private List<Comment> comments = new ArrayList<>();

@Column(name = "is_deleted", columnDefinition = "BOOLEAN DEFAULT false")
private boolean isDeleted;

private LocalDateTime deleteAt;

@Builder
public Content(Member member, String contentText) {
this.member = member;
this.contentText = contentText;
}

public void softDelete() {
this.isDeleted = true;
this.deleteAt = LocalDateTime.now().plusDays(CONTENT_RETENTION_PERIOD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface ContentRepository extends JpaRepository<Content, Long> {
Optional<Content> findContentById(Long contentId);

@Query("DELETE FROM Content c WHERE c.isDeleted = true AND c.deleteAt < :currentDate")
void deleteContentScheduledForDeletion(LocalDateTime currentDate);

//게시물 전체 조회 관련

List<Content> findAllByOrderByCreatedAtDesc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void deleteContent(Long memberId, Long contentId) {
}
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("contentLiked",contentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("contentGhost",contentId);
contentRepository.deleteById(contentId);
Content deleteContent = contentRepository.findContentByIdOrThrow(contentId);
deleteContent.softDelete();

}

public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto contentLikedRequestDto) {
Expand Down Expand Up @@ -105,6 +107,7 @@ public void unlikeContent(Long memberId, Long contentId) {
targetMember, memberId, "contentLiked", contentId);
}


private void deleteValidate(Long memberId, Long contentId) {
Content content = contentRepository.findById(contentId)
.orElseThrow(() -> new IllegalArgumentException(ErrorStatus.NOT_FOUND_CONTENT.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dontbe.www.DontBeServer.api.content.service;


import com.dontbe.www.DontBeServer.api.content.repository.ContentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
@Transactional
public class ExpiredContentDeleteBatch {
private final ContentRepository contentRepository;

@Scheduled(cron = "0 0 0 * * ?") //매일 밤 자정에 실행
public void deleteExpiredUser() {
contentRepository.deleteContentScheduledForDeletion(LocalDateTime.now());
}
}
Loading