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/history controller #188

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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 @@ -18,18 +18,7 @@ public interface HistoryRepository extends JpaRepository<History, Long>, History

Page<History> findByMemberAndOriginPrayIdIsNotNull(Member member, Pageable pageable);


Optional<History> findByIdAndMember(Long historyId, Member member);

default History getHistoryByIdAndMember(Long historyId, Member member) {
return findByIdAndMember(historyId, member)
.orElseThrow(() -> new NotFoundException(ErrorStatus.HISTORY_NOT_FOUND_EXCEPTION));
}

default History getHistoryById(Long historyId) {
return findById(historyId)
.orElseThrow(() -> new NotFoundException(
ErrorStatus.HISTORY_NOT_FOUND_EXCEPTION
));
}
Optional<History> findById(Long historyId);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.uspray.uspray.domain.history.service;

import com.uspray.uspray.domain.member.service.MemberService;
import com.uspray.uspray.domain.pray.dto.pray.request.PrayRequestDto;
import com.uspray.uspray.domain.pray.dto.pray.response.PrayResponseDto;
import com.uspray.uspray.domain.history.model.History;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.domain.pray.service.PrayFacade;
import com.uspray.uspray.domain.history.repository.HistoryRepository;
import com.uspray.uspray.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,15 +15,15 @@
public class HistoryFacade {

private final PrayFacade prayFacade;
private final MemberRepository memberRepository;
private final HistoryRepository historyRepository;
private final MemberService memberService;
private final HistoryService historyService;

@Transactional
public PrayResponseDto historyToPray(PrayRequestDto prayRequestDto, String username, Long historyId) {
Member member = memberRepository.getMemberByUserId(username);
History history = historyRepository.getHistoryByIdAndMember(historyId, member);
Member member = memberService.findMemberByUserId(username);
History history = historyService.getHistoryByIdAndMember(historyId, member);

historyRepository.delete(history);
historyService.deleteHistory(history);

return prayFacade.createPray(prayRequestDto, username,
history.getStartDate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,42 @@ public HistoryListResponseDto searchHistoryList(String username,
return new HistoryListResponseDto(historyList.getContent(), historyList.getTotalPages());
}

@Transactional(readOnly = true)
public HistoryDetailResponseDto getHistoryDetail(String username, Long historyId) {
Member member = memberRepository.getMemberByUserId(username);
History history = historyRepository.getHistoryById(historyId);
if (!history.getMember().getId().equals(member.getId())) {
throw new NotFoundException(ErrorStatus.HISTORY_NOT_FOUND_EXCEPTION);
}

if (history.getCategoryType() == CategoryType.SHARED) {
Pray originPray = prayRepository.getPrayById(history.getOriginPrayId());
return HistoryDetailResponseDto.shared(history, originPray);
}
return HistoryDetailResponseDto.of(history);
}
@Transactional(readOnly = true)
public HistoryDetailResponseDto getHistoryDetail(String username, Long historyId) {
Member member = memberRepository.getMemberByUserId(username);
History history = getHistoryById(historyId);
if (history.getCategoryType() == CategoryType.SHARED) {
Pray originPray = prayRepository.getPrayById(history.getOriginPrayId());
return HistoryDetailResponseDto.shared(history, originPray);
}
return HistoryDetailResponseDto.of(history);
}

@Transactional
public void saveHistory(History history) {
historyRepository.save(history);
}

@Transactional
public void deleteHistory(History history) {
historyRepository.delete(history);
}

@Transactional(readOnly = true)
public History getHistoryByIdAndMember(Long historyId, Member member) {
return historyRepository.findByIdAndMember(historyId, member)
.orElseThrow(() -> new NotFoundException(
ErrorStatus.HISTORY_NOT_FOUND_EXCEPTION
));
}

@Transactional(readOnly = true)
public History getHistoryById(Long historyId) {
return historyRepository.findById(historyId)
.orElseThrow(() -> new NotFoundException(
ErrorStatus.HISTORY_NOT_FOUND_EXCEPTION
));
}

@Transactional
public void createHistory(Pray pray, Integer totalCountOrNull) {
Expand All @@ -91,4 +113,5 @@ public void createHistory(Pray pray, Integer totalCountOrNull) {
.build();
historyRepository.save(history);
}

}