Skip to content

Commit

Permalink
Modify/#175 delete pray (#176)
Browse files Browse the repository at this point in the history
* modify: pray 삭제시 보관함에 있는 기도들도 같이 삭제되게 변경

* refactor: 불필요한 코드 삭제

* refactor: 기존 존재하는 delete All 로직을 Batch 처리로 진행되도록 변경

* modify: native query를 이용해 deleted된 pray도 shared pray에서 불러올 수 있게 변경
  • Loading branch information
dong2ast authored May 13, 2024
1 parent c413f61 commit 7872079
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ public class SharedPrayResponseDto {
@Schema(description = "기도제목 생성일", example = "2021-01-01 00:00:00")
private LocalDateTime createdAt;

public static SharedPrayResponseDto of(SharedPray sharedPray) {
Pray pray = sharedPray.getPray();
public static SharedPrayResponseDto of(SharedPray sharedPray, Pray pray) {

return new SharedPrayResponseDto(sharedPray.getId(), sharedPray.getPray().getId(),
sharedPray.getPray().getMember().getUserId(),
sharedPray.getPray().getMember().getName(),
sharedPray.getPray().getContent(), sharedPray.getPray().getDeadline(),
return new SharedPrayResponseDto(sharedPray.getId(), pray.getId(),
pray.getMember().getUserId(),
pray.getMember().getName(),
pray.getContent(), pray.getDeadline(),
sharedPray.getCreatedAt());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Objects;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand Down Expand Up @@ -50,4 +52,7 @@ default Pray cancelPray(Long prayId, String username) {
pray.deleteLastPrayedAt();
return pray;
}

@Query(value = "SELECT * FROM Pray p where (p.pray_id = :id)", nativeQuery = true)
Pray getPrayByIdIgnoreDelete(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public interface SharedPrayRepository extends JpaRepository<SharedPray, Long> {
@EntityGraph(attributePaths = {"member"})
List<SharedPray> findAllByMemberOrderByCreatedAtDesc(Member member);

@EntityGraph(attributePaths = {"pray"})
List<SharedPray> findAllByPray(Pray pray);

boolean existsByMemberAndPray(Member member, Pray pray);

List<SharedPray> findAllByCreatedAtBefore(LocalDate threshold);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/uspray/uspray/service/ShareService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uspray.uspray.service;

import com.uspray.uspray.domain.Pray;
import com.uspray.uspray.domain.SharedPray;
import com.uspray.uspray.infrastructure.SharedPrayRepository;
import java.time.LocalDate;
Expand All @@ -17,6 +18,12 @@ public class ShareService {
@Transactional
public void cleanSharedPray(LocalDate threshold) {
List<SharedPray> sharedPrayList = sharedPrayRepository.findAllByCreatedAtBefore(threshold);
sharedPrayRepository.deleteAll(sharedPrayList);
sharedPrayRepository.deleteAllInBatch(sharedPrayList);
}

@Transactional
public void deleteByOriginPray(Pray pray) {
List<SharedPray> sharedPrays = sharedPrayRepository.findAllByPray(pray);
sharedPrayRepository.deleteAllInBatch(sharedPrays);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.uspray.uspray.infrastructure.PrayRepository;
import com.uspray.uspray.infrastructure.ScrapAndHeartRepository;
import com.uspray.uspray.service.FCMNotificationService;
import com.uspray.uspray.service.ShareService;
import java.time.LocalDate;
import java.util.List;
import javax.transaction.Transactional;
Expand All @@ -40,6 +41,7 @@ public class PrayFacade {
private final NotificationLogRepository notificationLogRepository;
private final FCMNotificationService fcmNotificationService;
private final ScrapAndHeartRepository scrapAndHeartRepository;
private final ShareService shareService;

@Transactional
public PrayResponseDto createPray(PrayRequestDto prayRequestDto, String username) {
Expand Down Expand Up @@ -195,6 +197,7 @@ public PrayResponseDto deletePray(Long prayId, String username) {
if (scrapAndHeart != null) {
scrapAndHeart.deletePrayId();
}
shareService.deleteByOriginPray(pray);
prayRepository.delete(pray);
return PrayResponseDto.of(pray);
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/uspray/uspray/service/facade/ShareFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.uspray.uspray.infrastructure.SharedPrayRepository;
import com.uspray.uspray.service.FCMNotificationService;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -48,9 +48,15 @@ public List<SharedPrayResponseDto> getSharedPrayList(String username) {
List<SharedPray> sharedPrayList = sharedPrayRepository.findAllByMemberOrderByCreatedAtDesc(
member);

return sharedPrayList.stream()
.map(SharedPrayResponseDto::of)
.collect(Collectors.toList());
List<SharedPrayResponseDto> result = new ArrayList<>();

for (SharedPray sharedPray : sharedPrayList) {
Pray pray = prayRepository.getPrayByIdIgnoreDelete(
sharedPray.getPray().getId());
result.add(SharedPrayResponseDto.of(sharedPray, pray));
}

return result;
}

@Transactional
Expand Down

0 comments on commit 7872079

Please sign in to comment.