Skip to content

Commit

Permalink
♻️ SteadyService 필터링 조회 로직 분기 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
weonest committed Jan 16, 2024
1 parent 1cacc19 commit 1d40c41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 65 deletions.
45 changes: 4 additions & 41 deletions src/main/java/dev/steady/steady/dto/response/PageResponse.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
package dev.steady.steady.dto.response;

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

public record PageResponse<T>(
List<T> content,
long numberOfElements,
long page,
String prevCursor,
String nextCursor
CursorResponse cursor
) {

public static <T> PageResponse<T> promotedAtResponse(List<T> list, long page, LocalDateTime prevCursor, LocalDateTime nextCursor) {
if (Objects.isNull(prevCursor)) {
return new PageResponse<>(
list,
list.size(),
page,
null,
String.valueOf(nextCursor)
);
}
return new PageResponse<>(
list,
public static <T> PageResponse<T> of(List<T> list, CursorResponse cursorResponse) {
return new PageResponse<>(list,
list.size(),
page,
String.valueOf(prevCursor),
String.valueOf(nextCursor)
);

}

public static <T> PageResponse<T> deadlineResponse(List<T> list, long page, LocalDate prevCursor, LocalDate nextCursor) {
if (Objects.isNull(prevCursor)) {
return new PageResponse<>(
list,
list.size(),
page,
null,
String.valueOf(nextCursor)
);
}
return new PageResponse<>(
list,
list.size(),
page,
String.valueOf(prevCursor),
String.valueOf(nextCursor)
cursorResponse
);
}

Expand Down
36 changes: 15 additions & 21 deletions src/main/java/dev/steady/steady/service/SteadyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dev.steady.steady.dto.request.SteadyCreateRequest;
import dev.steady.steady.dto.request.SteadyQuestionUpdateRequest;
import dev.steady.steady.dto.request.SteadyUpdateRequest;
import dev.steady.steady.dto.response.CursorResponse;
import dev.steady.steady.dto.response.MySteadyQueryResponse;
import dev.steady.steady.dto.response.MySteadyResponse;
import dev.steady.steady.dto.response.PageResponse;
Expand All @@ -31,7 +32,7 @@
import dev.steady.steady.dto.response.SteadyQueryResponse;
import dev.steady.steady.dto.response.SteadyQuestionsResponse;
import dev.steady.steady.dto.response.SteadyRankResponse;
import dev.steady.steady.uitl.PrevCursor;
import dev.steady.steady.uitl.Cursor;
import dev.steady.user.domain.Position;
import dev.steady.user.domain.Stack;
import dev.steady.user.domain.User;
Expand All @@ -47,7 +48,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -88,32 +88,26 @@ public Long create(SteadyCreateRequest request, UserInfo userinfo) {
}

@Transactional(readOnly = true)
@Cacheable(cacheNames = "steadies", key = "#pageable.pageNumber",
@Cacheable(cacheNames = "steadies", key = "#condition.status()",
condition = "#condition.cacheable() == true")
public PageResponse<SteadyQueryResponse> getSteadies(UserInfo userInfo, FilterConditionDto condition, Pageable pageable) {
SteadyFilterResponse filterResponse = steadyRepository.findAllByFilterCondition(userInfo, condition, pageable);
List<SteadyQueryResponse> searchResponses = filterResponse.result()
.stream()
.map(SteadyQueryResponse::from).toList();

PrevCursor prevCursor = PrevCursor.from(filterResponse.prevCursor());
SteadyQueryResponse nextCursor = searchResponses.get(searchResponses.size() - 1);

if (Objects.isNull(condition.cursor().getPromotedAt())) {
return PageResponse.deadlineResponse(
searchResponses,
pageable.getPageNumber(),
prevCursor.getDeadline(),
nextCursor.deadline()
);
.map(SteadyQueryResponse::from)
.toList();

Cursor prevCursor = Cursor.cursorFromSteady(filterResponse.prevCursorSteady());
Cursor nextCursor = Cursor.cursorFromSteady(filterResponse.nextCursorSteady());

CursorResponse cursorResponse;
if (condition.cursor().isPromotedAtCursor()) {
cursorResponse = new CursorResponse<>(prevCursor.getPromotedAt(), nextCursor.getPromotedAt());
return PageResponse.of(searchResponses, cursorResponse);
}

return PageResponse.promotedAtResponse(
searchResponses,
pageable.getPageNumber(),
prevCursor.getPromotedAt(),
nextCursor.promotedAt()
);
cursorResponse = new CursorResponse<>(prevCursor.getDeadline(), nextCursor.getDeadline());
return PageResponse.of(searchResponses, cursorResponse);
}

@Transactional(readOnly = true)
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/dev/steady/steady/uitl/Cursor.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.steady.steady.uitl;

import dev.steady.steady.domain.Steady;
import io.jsonwebtoken.lang.Strings;
import lombok.AllArgsConstructor;
import lombok.Getter;

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

@Getter
@AllArgsConstructor
Expand All @@ -14,19 +16,29 @@ public class Cursor {
private LocalDateTime promotedAt;
private LocalDate deadline;

public static Cursor promotedAtCursor(String cursor) {
public static Cursor promotedAtCursorFrom(String cursor) {
if (Strings.hasText(cursor)) {
return new Cursor(CursorFormatter.getLocalDateTime(cursor), null);
}
return new Cursor(LocalDateTime.now(), null);

}

public static Cursor deadlineCursor(String cursor) {
public static Cursor deadlineCursorFrom(String cursor) {
if (Strings.hasText(cursor)) {
return new Cursor(null, CursorFormatter.getLocalDate(cursor));
}
return new Cursor(null, LocalDate.now());
}

public static Cursor cursorFromSteady(Steady prevCursor) {
if (Objects.isNull(prevCursor)) {
return new Cursor(null, null);
}
return new Cursor(prevCursor.getPromotedAt(), prevCursor.getDeadline());
}

public boolean isPromotedAtCursor() {
return Objects.nonNull(promotedAt);
}

}

0 comments on commit 1d40c41

Please sign in to comment.