Skip to content

Commit

Permalink
Merge pull request #167 from FOREGG-DEV/dev
Browse files Browse the repository at this point in the history
♻️ refactor: 전체 하루기록 보기 페이징 처리
  • Loading branch information
DongJun1110 authored Oct 7, 2024
2 parents 7ea4918 + b80ee98 commit 7884553
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/foregg/foreggserver/controller/DailyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import foregg.foreggserver.apiPayload.ApiResponse;
import foregg.foreggserver.dto.dailyDTO.*;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyAllResponseDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyByCountResponseDTO;
import foregg.foreggserver.dto.injectionDTO.InjectionResponseDTO;
import foregg.foreggserver.service.dailyService.DailyQueryService;
Expand Down Expand Up @@ -30,12 +31,24 @@ public class DailyController {
private final InjectionQueryService injectionQueryService;
private final S3Service s3Service;

@Operation(summary = "전체 하루기록 보기 API")
@GetMapping("")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@PreAuthorize("isAuthenticated()")
public ApiResponse<DailyAllResponseDTO> getAllDaily(@RequestParam(name = "page") int page) {
DailyAllResponseDTO allDaily = dailyQueryService.getAllDaily(page);
return ApiResponse.onSuccess(allDaily);
}

@Operation(summary = "하루기록 보기 API")
@GetMapping("/byDate/{date}")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@PreAuthorize("isAuthenticated()")

public ApiResponse<DailyResponseDTO> getDaily(@PathVariable(name = "date") String date) {
return ApiResponse.onSuccess(dailyQueryService.getDaily(date));
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/foregg/foreggserver/converter/DailyConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import foregg.foreggserver.domain.User;
import foregg.foreggserver.dto.dailyDTO.DailyRequestDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyAllResponseDTO;
import foregg.foreggserver.dto.dailyDTO.DailyResponseDTO.DailyByCountResponseDTO;
import foregg.foreggserver.dto.dailyDTO.SideEffectRequestDTO;
import foregg.foreggserver.dto.dailyDTO.SideEffectResponseDTO;
import foregg.foreggserver.util.DateUtil;
import org.springframework.data.domain.Page;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -18,6 +20,27 @@

public class DailyConverter {

public static DailyAllResponseDTO toDailyAllResponse(Page<Daily> dailyPage) {
List<DailyByCountResponseDTO> result = new ArrayList<>();
for (Daily daily : dailyPage.getContent()) {
DailyByCountResponseDTO dto = DailyByCountResponseDTO.builder()
.id(daily.getId())
.dailyConditionType(daily.getDailyConditionType())
.date(daily.getDate())
.content(daily.getContent())
.build();
result.add(dto);
}

return DailyAllResponseDTO.builder()
.dto(result)
.currentPage(dailyPage.getNumber())
.totalPages(dailyPage.getTotalPages())
.totalItems(dailyPage.getTotalElements())
.build();
}


public static Daily toDaily(DailyRequestDTO dto, User user, String imageUrl, int count) {
return Daily.builder()
.dailyConditionType(dto.getDailyConditionType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -21,6 +23,17 @@ public class DailyResponseDTO {
private String imageUrl;
private String reply;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class DailyAllResponseDTO {
private List<DailyByCountResponseDTO> dto;
private int currentPage;
private int totalPages;
private long totalItems;
}

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -31,5 +44,4 @@ public static class DailyByCountResponseDTO {
private DailyConditionType dailyConditionType;
private String content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import foregg.foreggserver.domain.Daily;
import foregg.foreggserver.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -15,7 +17,7 @@ public interface DailyRepository extends JpaRepository<Daily, Long> {

Optional<Daily> findByIdAndUser(Long id, User user);

Optional<List<Daily>> findByUser(User user);
Page<Daily> findByUser(User user, Pageable pageable);

Optional<List<Daily>> findByUserAndCount(User user, int count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import foregg.foreggserver.service.userService.UserQueryService;
import foregg.foreggserver.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -32,6 +35,15 @@ public class DailyQueryService {
private final UserQueryService userQueryService;
private final SideEffectRepository sideEffectRepository;

public DailyAllResponseDTO getAllDaily(int page) {
User user = userQueryService.getUser(SecurityUtil.getCurrentUser());
Pageable pageable = PageRequest.of(page, 10);
Page<Daily> dailyPage = dailyRepository.findByUser(user, pageable);

return DailyConverter.toDailyAllResponse(dailyPage);
}


public DailyResponseDTO getDaily(String date) {
User user = userQueryService.returnWifeOrHusband();
Daily daily = dailyRepository.findByDateAndUser(date, user);
Expand Down

0 comments on commit 7884553

Please sign in to comment.