Skip to content

Commit

Permalink
refactor: prayRepository 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
soonga00 committed Oct 15, 2024
1 parent a95c87a commit ffb6f2e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/main/java/com/uspray/uspray/domain/pray/service/PrayFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.uspray.uspray.domain.pray.dto.pray.request.PrayUpdateRequestDto;
import com.uspray.uspray.domain.pray.dto.pray.response.PrayResponseDto;
import com.uspray.uspray.domain.pray.model.Pray;
import com.uspray.uspray.domain.pray.repository.PrayRepository;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.enums.PrayType;
import com.uspray.uspray.global.exception.ErrorStatus;
Expand All @@ -35,8 +34,6 @@
@RequiredArgsConstructor
@Slf4j
public class PrayFacade {

private final PrayRepository prayRepository;
private final HistoryService historyService;
private final NotificationLogService notificationLogService;
private final FCMNotificationService fcmNotificationService;
Expand All @@ -58,11 +55,11 @@ public PrayResponseDto createPray(PrayRequestDto prayRequestDto, String username
@Transactional
public PrayResponseDto updatePray(Long prayId, String username,
PrayUpdateRequestDto prayUpdateRequestDto) {
Pray pray = prayRepository.getPrayByIdAndMemberId(prayId, username);
Pray pray = prayService.getPrayByIdAndMemberId(prayId, username);

// 이 기도 제목을 공유한 적 없거나, 공유 받은 사람이 없으면 전부 수정 가능
// 이 기도 제목을 공유한 적 있고, 누구라도 공유 받은 사람이 있으면 기도제목 내용 수정 불가능
Pray sharedPray = prayRepository.getPrayByOriginPrayId(prayId);
Pray sharedPray = prayService.getSharedPray(prayId);
Category category = categoryService.getCategoryByIdAndMember(
prayUpdateRequestDto.getCategoryId(),
pray.getMember());
Expand All @@ -81,17 +78,17 @@ public PrayResponseDto updatePray(Long prayId, String username,

@Transactional
public void convertPrayToHistory() {
List<Pray> prayList = prayRepository.findAllByDeadlineBefore(LocalDate.now());
List<Pray> prayList = prayService.getPrayListDeadlineBefore(LocalDate.now());
for (Pray pray : prayList) {
pray.complete();
Integer sharedCount = prayRepository.getSharedCountByOriginPrayId(
Integer sharedCount = prayService.getSharedCountByOriginPrayId(
pray.getOriginPrayId());
History history = History.builder()
.pray(pray)
.totalCount(sharedCount) //sharedCount에 내 count도 포함되어 있음
.build();
historyService.saveHistory(history);
prayRepository.delete(pray);
prayService.deletePray(pray);
}
}

Expand All @@ -101,7 +98,7 @@ public void convertPrayToHistory(Pray pray) {
.pray(pray)
.build();
historyService.saveHistory(history);
prayRepository.delete(pray);
prayService.deletePray(pray);
}

@Transactional
Expand All @@ -117,7 +114,7 @@ public CategoryResponseDto deleteCategory(String username, Long categoryId) {

@Transactional
public List<PrayListResponseDto> todayPray(Long prayId, String username) {
Pray pray = prayRepository.getPrayByIdAndMemberId(prayId, username);
Pray pray = prayService.getPrayByIdAndMemberId(prayId, username);
handlePrayedToday(pray);
return getPrayList(username, pray.getPrayType().stringValue());
}
Expand Down Expand Up @@ -164,12 +161,12 @@ private void handlePrayedToday(Pray pray) {

@Transactional
public PrayResponseDto deletePray(Long prayId, String username) {
Pray pray = prayRepository.getPrayByIdAndMemberId(prayId, username);
Pray pray = prayService.getPrayByIdAndMemberId(prayId, username);
Member member = memberService.findMemberByUserId(username);

scrapAndHeartService.deleteScrapAndHeart(member, pray);
shareService.deleteByOriginPray(pray);
prayRepository.delete(pray);
prayService.deletePray(pray);
return PrayResponseDto.of(pray);
}

Expand Down Expand Up @@ -214,17 +211,17 @@ private PrayResponseDto convertToPrayResponseDto(Pray pray) {

@Transactional
public List<PrayListResponseDto> completePray(Long prayId, String username) {
Pray pray = prayRepository.getPrayByIdAndMemberId(prayId, username);
Pray pray = prayService.getPrayByIdAndMemberId(prayId, username);
pray.complete();

createHistory(pray);
prayRepository.delete(pray);
prayService.deletePray(pray);

return getPrayList(username, pray.getPrayType().stringValue());
}

private void createHistory(Pray pray) {
Integer sharedCount = prayRepository.getSharedCountByOriginPrayId(pray.getId());
Integer sharedCount = prayService.getSharedCountByOriginPrayId(pray.getId());
History history = History.builder()
.pray(pray)
.totalCount(sharedCount)
Expand All @@ -235,6 +232,6 @@ private void createHistory(Pray pray) {
@Transactional
public List<PrayListResponseDto> cancelPray(Long prayId, String username) {
return getPrayList(username,
prayRepository.cancelPray(prayId, username).getPrayType().stringValue());
prayService.cancelPray(prayId, username).getPrayType().stringValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.uspray.uspray.domain.pray.dto.pray.response.PrayResponseDto;
import com.uspray.uspray.domain.pray.model.Pray;
import com.uspray.uspray.domain.pray.repository.PrayRepository;
import java.time.LocalDate;
import java.util.List;
import javax.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -33,4 +34,28 @@ public List<Pray> getPrayListByCategory(Category category) {
public List<Pray> getPrayListByMemberAndCategory(Member member, Category category) {
return prayRepository.findAllByMemberAndCategoryOrderByCreatedAtAsc(member, category);
}

public Pray getPrayByIdAndMemberId(Long prayId, String username) {
return prayRepository.getPrayByIdAndMemberId(prayId, username);
}

public Pray getSharedPray(Long prayId) {
return prayRepository.getPrayByOriginPrayId(prayId);
}

public List<Pray> getPrayListDeadlineBefore(LocalDate currentDate) {
return prayRepository.findAllByDeadlineBefore(currentDate);
}

public Integer getSharedCountByOriginPrayId(Long originPrayId) {
return prayRepository.getSharedCountByOriginPrayId(originPrayId);
}

public void deletePray(Pray pray) {
prayRepository.delete(pray);
}

public Pray cancelPray(Long prayId, String username) {
return prayRepository.cancelPray(prayId, username);
}
}

0 comments on commit ffb6f2e

Please sign in to comment.