Skip to content

Commit

Permalink
Merge pull request #57 from HanaFun/revenue
Browse files Browse the repository at this point in the history
fix: revenue exception handling
  • Loading branch information
doSeung11 authored Jul 3, 2024
2 parents 935c5bd + e3119d9 commit 0241242
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

public interface RevenueRepository extends JpaRepository<RevenueEntity, Long> {
Optional<RevenueEntity> findByCreatedDateBetween(LocalDateTime startDateTime, LocalDateTime endDateTime);

@Query(value = "SELECT SUM(R.revenue) FROM RevenueEntity R WHERE R.lessonEntity = :lessonEntity")
Long totalRevenueByLessonId(@Param("lessonEntity") LessonEntity lessonEntity);

Optional<RevenueEntity> findByLessonEntityAndCreatedDateBetween(LessonEntity lessonEntity, LocalDateTime startDateTime, LocalDateTime endDateTime);
Optional<List<RevenueEntity>> findByLessonEntityLessonIdAndCreatedDateBetween(Long lessonId, LocalDateTime startDateTime, LocalDateTime endDateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.hanaro.hanafun.host.exception.HostNotFoundException;
import com.hanaro.hanafun.lesson.domain.LessonEntity;
import com.hanaro.hanafun.lesson.domain.LessonRepository;
import com.hanaro.hanafun.lesson.exception.LessonNotFoundException;
import com.hanaro.hanafun.revenue.domain.RevenueEntity;
import com.hanaro.hanafun.revenue.domain.RevenueRepository;
import com.hanaro.hanafun.revenue.dto.*;
import com.hanaro.hanafun.revenue.exception.RevenueNotFoundException;
import com.hanaro.hanafun.revenue.mapper.RevenueMapper;
import com.hanaro.hanafun.revenue.service.RevenueService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -45,13 +47,19 @@ public TotalRevenueResDto totalRevenue(Long userId) {
@Override
@Transactional
public List<MonthRevenueResDto> monthRevenue(Long userId, Integer year, Integer month) {
HostEntity hostEntity = hostRepository.findByUserEntityUserId(userId).orElseThrow(() -> new HostNotFoundException());
List<LessonEntity> lessonEntityList = lessonRepository.findByHostEntityHostId(hostEntity.getHostId()).orElseThrow();
//호스트 가져오기
HostEntity hostEntity = hostRepository.findByUserEntityUserId(userId)
.orElseThrow(() -> new HostNotFoundException());

//호스트의 전체 강좌 목록 가져오기
List<LessonEntity> lessonEntityList = lessonRepository.findByHostEntityHostId(hostEntity.getHostId())
.orElseThrow(() -> new LessonNotFoundException());

YearMonth yearMonth = YearMonth.of(year, month);
LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay();
LocalDateTime endOfMonth = yearMonth.atEndOfMonth().atTime(23, 59, 59);

//강좌마다 해당년월의 매출 하나씩 건지고, collect 합쳐서 해당년월의 전체 매출 리스트 구하기
List<RevenueEntity> revenueEntityList = lessonEntityList.stream()
.flatMap(lessonEntity -> revenueRepository.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth).stream())
.collect(Collectors.toList());
Expand All @@ -68,7 +76,10 @@ public List<LessonRevenueResDto> lessonRevenue(Integer year, Long lessonId) {
LocalDateTime startOfYear = searchYear.atMonth(1).atDay(1).atStartOfDay();
LocalDateTime endOfYear = searchYear.atMonth(12).atEndOfMonth().atTime(23,59,59);

List<RevenueEntity> revenueEntityList = revenueRepository.findByLessonEntityLessonIdAndCreatedDateBetween(lessonId, startOfYear, endOfYear).orElseThrow();
//강좌의 해당 년 매출 리스트 구하기
List<RevenueEntity> revenueEntityList = revenueRepository
.findByLessonEntityLessonIdAndCreatedDateBetween(lessonId, startOfYear, endOfYear)
.orElseThrow(() -> new RevenueNotFoundException());

return revenueEntityList.stream()
.map(RevenueMapper::revenueEntityToLessonRevenueResDto)
Expand All @@ -78,13 +89,16 @@ public List<LessonRevenueResDto> lessonRevenue(Integer year, Long lessonId) {
@Override
@Transactional
public UpdatePriceResDto updatePrice(UpdatePriceReqDto updatePriceReqDto) {
LessonEntity lessonEntity = lessonRepository.findById(updatePriceReqDto.getLessonId()).orElseThrow();
LessonEntity lessonEntity = lessonRepository.findById(updatePriceReqDto.getLessonId())
.orElseThrow(() -> new LessonNotFoundException());

YearMonth yearMonth = YearMonth.of(updatePriceReqDto.getYear(), updatePriceReqDto.getMonth());
LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay();
LocalDateTime endOfMonth = yearMonth.atEndOfMonth().atTime(23, 59, 59);

RevenueEntity revenueEntity = revenueRepository.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth).orElseThrow();
RevenueEntity revenueEntity = revenueRepository
.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth)
.orElseThrow(() -> new RevenueNotFoundException());
revenueEntity.setMaterialPrice(updatePriceReqDto.getMaterialPrice());
revenueEntity.setRentalPrice(updatePriceReqDto.getRentalPrice());
revenueEntity.setEtcPrice(updatePriceReqDto.getEtcPrice());
Expand Down

0 comments on commit 0241242

Please sign in to comment.