Skip to content

Commit

Permalink
Refactor: 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
gunGeongun committed Nov 15, 2024
1 parent 5afeae8 commit 452a937
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

@Getter
public enum ResultType {
HIGH(""),
MEDIUM(""),
LOW("");
HIGH("심각한 상태에요"),
MEDIUM("초기 상태예요"),
LOW("문제가 없어요");
private final String description;

ResultType(String description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -61,15 +62,20 @@ public PagingResponse<HospitalStatisticsResponse.Info> getAllHospitalStatistics(
}

@GetMapping("/record/monthly")
public PagingResponse<HospitalStatisticsResponse.Info> getMonthlyHospitalStatistics(
public HospitalStatisticsResponse.Monthly getMonthlyStatistics(
@Authenticate Long memberId,
@PageableDefault(page = 0, size = 10) Pageable pageable,
@RequestParam(name = "date", required = false) LocalDate date
) {
Page<HospitalStatisticsModel.Info> monthlyHospitalStatistics =
hospitalStatisticsService.getMonthlyHospitalStatistics(memberId, date, pageable);
HospitalStatisticsResponse.Infos infoList =
HospitalStatisticsResponse.Infos.from(monthlyHospitalStatistics);
return PagingResponse.from(infoList.infos());
return hospitalStatisticsService.getMonthlyStatistics(memberId, date);
}

@GetMapping("/day")
public HospitalStatisticsResponse.Info getDailyHospitalStatistics(
Long memberId,
@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date
) {
HospitalStatisticsModel.Info dailyStatistic =
hospitalStatisticsService.getDailyHospitalStatistics(memberId, date);
return HospitalStatisticsResponse.Info.from(dailyStatistic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ public static HospitalStatisticsResponse.Infos from(Page<HospitalStatisticsModel
.build();
}
}

@Builder
public record Monthly(
Long totalCount,
Long highCount,
Long mediumCount,
Long lowCount
) {
public static Monthly from(Long total, Long high, Long medium, Long low) {
return Monthly.builder()
.totalCount(total)
.highCount(high)
.mediumCount(medium)
.lowCount(low)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface HospitalStatisticsRepository extends JpaRepository<HospitalStatistics, Long> {
Page<HospitalStatistics> findAllByMember(Member member, Pageable pageable);
Expand All @@ -20,4 +23,13 @@ Page<HospitalStatistics> findAllByMemberAndCreatedAtBetween(
@Param("endDate") LocalDateTime endDate,
Pageable pageable
);

@Query("SELECT h FROM HospitalStatistics h WHERE h.member = :member AND h.createdAt BETWEEN :startDate AND :endDate")
List<HospitalStatistics> findAllByMemberAndCreatedAtBetween(
@Param("member") Member member,
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate
);

Optional<HospitalStatistics> findByMemberAndDate(Member member, LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import com.hackathon.nullnullteam.global.exception.EntityNotFoundException;
import com.hackathon.nullnullteam.hospitalstatistics.HospitalStatistics;
import com.hackathon.nullnullteam.hospitalstatistics.ResultType;
import com.hackathon.nullnullteam.hospitalstatistics.controller.dto.HospitalStatisticsResponse;
import com.hackathon.nullnullteam.hospitalstatistics.infrastructure.repository.HospitalStatisticsRepository;
import com.hackathon.nullnullteam.member.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
Expand All @@ -30,4 +34,29 @@ public Page<HospitalStatistics> getMonthlyByMember(
return hospitalStatisticsRepository.findAllByMemberAndCreatedAtBetween(
member, startDate, endDate, pageable);
}

public HospitalStatistics getByMemberAndDate(Member member, LocalDate date) {
return hospitalStatisticsRepository.findByMemberAndDate(member, date)
.orElseThrow(() -> new EntityNotFoundException("해당 날짜의 병원 기록이 없습니다."));
}

public HospitalStatisticsResponse.Monthly getMonthlyStatistics(
Member member, LocalDateTime startDate, LocalDateTime endDate) {

List<HospitalStatistics> statistics = hospitalStatisticsRepository
.findAllByMemberAndCreatedAtBetween(member, startDate, endDate);

Long total = (long) statistics.size();
Long highCount = statistics.stream()
.filter(s -> s.getResult() == ResultType.HIGH)
.count();
Long mediumCount = statistics.stream()
.filter(s -> s.getResult() == ResultType.MEDIUM)
.count();
Long lowCount = statistics.stream()
.filter(s -> s.getResult() == ResultType.LOW)
.count();

return HospitalStatisticsResponse.Monthly.from(total, highCount, mediumCount, lowCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hackathon.nullnullteam.global.constants.DateConstants;
import com.hackathon.nullnullteam.hospitalstatistics.HospitalStatistics;
import com.hackathon.nullnullteam.hospitalstatistics.controller.dto.HospitalStatisticsResponse;
import com.hackathon.nullnullteam.hospitalstatistics.service.dto.HospitalStatisticsCommand;
import com.hackathon.nullnullteam.hospitalstatistics.service.dto.HospitalStatisticsModel;
import com.hackathon.nullnullteam.member.Member;
Expand Down Expand Up @@ -64,4 +65,25 @@ public Page<HospitalStatisticsModel.Info> getMonthlyHospitalStatistics(
hospitalStatisticsReaderService.getMonthlyByMember(member, startDate, endDate, pageable);
return hospitalStatistics.map(HospitalStatisticsModel.Info::from);
}

@Transactional(readOnly = true)
public HospitalStatisticsModel.Info getDailyHospitalStatistics(Long memberId, LocalDate date) {
Member member = memberReaderService.getMemberById(memberId);
HospitalStatistics statistics =
hospitalStatisticsReaderService.getByMemberAndDate(member, date);
return HospitalStatisticsModel.Info.from(statistics);
}

public HospitalStatisticsResponse.Monthly getMonthlyStatistics(Long memberId, LocalDate date) {
Member member = memberReaderService.getMemberById(memberId);

LocalDateTime startDate = date == null ?
DateConstants.DEFAULT_START_DATE :
date.withDayOfMonth(1).atStartOfDay();
LocalDateTime endDate = date == null ?
LocalDateTime.now() :
date.withDayOfMonth(date.lengthOfMonth()).atTime(LocalTime.MAX);

return hospitalStatisticsReaderService.getMonthlyStatistics(member, startDate, endDate);
}
}

This file was deleted.

0 comments on commit 452a937

Please sign in to comment.