Skip to content

Commit

Permalink
[Fix]페이징 삭제할 곳 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
do-dop committed Dec 20, 2024
1 parent 6e24111 commit 9815ffd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public class RecordController {
@GetMapping("/{pet_id}/{category}")
public ApiResponse<RecordListResponse> getRecordList(
@AuthenticationPrincipal Member member,
@PathVariable(name = "pet_id") Long petId, @PathVariable(name = "category") Category category
@PathVariable(name = "pet_id") Long petId, @PathVariable(name = "category") Category category,
@RequestParam(name = "page", defaultValue = "0") int page
){
List<RecordListResponseDto> records = recordService.getRecordsByCategory(member, petId, category);
RecordListResponse result = new RecordListResponse(category, records);
List<RecordListResponseDto> records = recordService.getRecordsByCategory(member, petId, category, page, 21);
var result = new RecordListResponse(category, records);
return ApiResponse.onSuccess(result);
}

Expand Down Expand Up @@ -88,10 +89,9 @@ public ApiResponse<RecordListResponse> getRecordListAtDate(
@AuthenticationPrincipal Member member,
@PathVariable(name = "pet_id") Long petId,
@PathVariable(name = "category") Category category,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "date") String date
){
List<RecordListResponseDto> records = recordService.getRecordsAtDate(member, petId, category, page, 21, date);
List<RecordListResponseDto> records = recordService.getRecordsAtDate(member, petId, category, date);
RecordListResponse result = new RecordListResponse(category, records);
return ApiResponse.onSuccess(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

@Repository
public interface RecordRepository extends JpaRepository<Record, Long> {
List<Record> findByPet_PetIdAndCategory(Long petId, Category category);
Page<Record> findByPet_PetIdAndCategory(Long petId, Category category, Pageable pageable);

Optional<Record> findByPet_PetIdAndRecordId(Long petId, Long recordId);

Page<Record> findByPet_PetIdAndCategoryAndCreatedAtBetween(Long petId, Category category, LocalDateTime startOfDay, LocalDateTime endOfDay, Pageable pageable);
List<Record> findByPet_PetIdAndCategoryAndCreatedAtBetween(Long petId, Category category, LocalDateTime startOfDay, LocalDateTime endOfDay);

Optional<Record> findByRecordId(Long recordId);

Expand Down
24 changes: 9 additions & 15 deletions src/main/java/ttakkeun/ttakkeun_server/service/RecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ public class RecordService {
private final S3ImageService s3ImageService;
private final ResultRepository resultRepository;

public List<RecordListResponseDto> getRecordsByCategory(Member member, Long petId, Category category) {
public List<RecordListResponseDto> getRecordsByCategory(Member member, Long petId, Category category, int page, int size) {
if (member == null || member.getMemberId() == null) {
throw new MemberHandler(MEMBER_NOT_FOUND);
}
//memberId로 petId를 확인
Pet pet = petRepository.findByPetIdAndMember(petId, member).orElseThrow(() -> new PetHandler(PET_NOT_FOUND));

List<Record> records = recordRepository.findByPet_PetIdAndCategory(pet.getPetId(), category);
// 페이지 요청 생성 (createdAt 기준으로 정렬)
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Record> recordPage = recordRepository.findByPet_PetIdAndCategory(pet.getPetId(), category, pageable);

// DTO로 변환 및 정렬
return records.stream()
.map(RecordConverter::t0RecordListResponseDto)
.sorted((r1, r2) -> {
return recordPage.stream()
.map(RecordConverter::t0RecordListResponseDto).sorted((r1, r2) -> {
int dateComparison = r2.getCreatedAtDate().compareTo(r1.getCreatedAtDate());
if (dateComparison != 0) {
return dateComparison;
Expand Down Expand Up @@ -190,7 +191,7 @@ public RecordResponseDTO.DeleteResultDTO deleteRecord(Long recordId) {
}
}

public List<RecordListResponseDto> getRecordsAtDate(Member member, Long petId, Category category, int page, int size, String date) {
public List<RecordListResponseDto> getRecordsAtDate(Member member, Long petId, Category category, String date) {
if (member == null || member.getMemberId() == null) {
throw new ExceptionHandler(MEMBER_NOT_FOUND);
}
Expand All @@ -206,19 +207,12 @@ public List<RecordListResponseDto> getRecordsAtDate(Member member, Long petId, C
LocalDateTime startOfDay = targetDate.atStartOfDay();
LocalDateTime endOfDay = targetDate.atTime(LocalTime.MAX);

// 페이지 요청 생성 (createdAt 기준으로 정렬)
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));

// Repository에서 해당 날짜의 기록 조회
Page<Record> recordPage = recordRepository.findByPet_PetIdAndCategoryAndCreatedAtBetween(pet.getPetId(), category, startOfDay, endOfDay, pageable);
List<Record> recordPage = recordRepository.findByPet_PetIdAndCategoryAndCreatedAtBetween(pet.getPetId(), category, startOfDay, endOfDay);

// DTO로 변환 및 정렬
return recordPage.stream()
.map(record -> new RecordListResponseDto(
record.getRecordId(),
record.getCreatedAt().toLocalDate(), // LocalDate 추출
record.getCreatedAt().toLocalTime() // LocalTime 추출
))
.map(RecordConverter::t0RecordListResponseDto)
.sorted((r1, r2) -> {
int dateComparison = r2.getCreatedAtDate().compareTo(r1.getCreatedAtDate());
if (dateComparison != 0) {
Expand Down

0 comments on commit 9815ffd

Please sign in to comment.