Skip to content

Commit

Permalink
fix : cursor 응답 스키마 변경 (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Sep 20, 2024
1 parent f3b343e commit 7addf21
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.UUID;

public record CursorApiResponse(
Expand All @@ -11,12 +12,20 @@ public record CursorApiResponse(
@Schema(description = "조회한 데이터의 Cursor Value")
Object value
) {

public static CursorApiResponse toCursorResponse(UUID id, Object value) {
return new CursorApiResponse(id, value);
}

public static CursorApiResponse toCursorId(UUID id) {
return new CursorApiResponse(id, "");
return new CursorApiResponse(id, null);
}

public static CursorApiResponse noneCursor() {
return new CursorApiResponse(null, null);
}

public static <T> T getLastElement(List<T> list) {
return list.isEmpty() ? null : list.get(list.size() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ public record PaginationApiResponse<T>(
@Schema(description = "다음 조회 가능 여부")
boolean hasNext,
@Schema(description = "조회 데이터")
List<T> data
List<T> data,
@Schema(description = "조회 데이터의 cursor")
CursorApiResponse cursor
) {

@Builder
public PaginationApiResponse(
List<T> data,
boolean hasNext
boolean hasNext,
CursorApiResponse cursor
) {
this(data.size(), hasNext, data);
this(data.size(), hasNext, data, cursor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.response.CursorApiResponse;
import org.example.dto.response.PaginationApiResponse;
import org.example.security.dto.AuthenticatedInfo;
import org.example.util.ValidatorUser;
Expand Down Expand Up @@ -52,10 +54,15 @@ public ResponseEntity<PaginationApiResponse<ArtistUnsubscriptionPaginationApiPar
.map(ArtistUnsubscriptionPaginationApiParam::from)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<ArtistUnsubscriptionPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand All @@ -73,10 +80,15 @@ public ResponseEntity<PaginationApiResponse<ArtistSubscriptionPaginationApiParam
.map(ArtistSubscriptionPaginationApiParam::from)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<ArtistSubscriptionPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand Down Expand Up @@ -131,10 +143,15 @@ public ResponseEntity<PaginationApiResponse<ArtistSearchPaginationApiParam>> sea
.map(ArtistSearchPaginationApiParam::from)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<ArtistSearchPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.example.artist.service.dto.param.ArtistSearchPaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record ArtistSearchPaginationApiParam(
@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "아티스트 ID")
UUID id,

@Schema(description = "아티스트 이미지 URL")
String imageURL,
Expand All @@ -23,7 +23,7 @@ public record ArtistSearchPaginationApiParam(

public static ArtistSearchPaginationApiParam from(ArtistSearchPaginationServiceParam param) {
return new ArtistSearchPaginationApiParam(
CursorApiResponse.toCursorId(param.artistId()),
param.artistId(),
param.artistImageUrl(),
param.artistKoreanName(),
param.artistEnglishName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.example.artist.service.dto.param.ArtistSubscriptionPaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record ArtistSubscriptionPaginationApiParam(
@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "아티스트 ID")
UUID id,
@Schema(description = "아티스트 이미지 URL")
String imageURL,
@Schema(description = "아티스트 한글 이름")
Expand All @@ -15,11 +15,9 @@ public record ArtistSubscriptionPaginationApiParam(
String englishName
) {

public static ArtistSubscriptionPaginationApiParam from(
ArtistSubscriptionPaginationServiceParam param
) {
public static ArtistSubscriptionPaginationApiParam from(ArtistSubscriptionPaginationServiceParam param) {
return new ArtistSubscriptionPaginationApiParam(
CursorApiResponse.toCursorId(param.artistId()),
param.artistId(),
param.artistImageUrl(),
param.artistKoreanName(),
param.artistEnglishName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.example.artist.service.dto.param.ArtistUnsubscriptionPaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record ArtistUnsubscriptionPaginationApiParam(
@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "아티스트 ID")
UUID id,
@Schema(description = "아티스트 이미지 URL")
String imageURL,
@Schema(description = "아티스트 한글 이름")
Expand All @@ -19,7 +19,7 @@ public static ArtistUnsubscriptionPaginationApiParam from(
ArtistUnsubscriptionPaginationServiceParam param
) {
return new ArtistUnsubscriptionPaginationApiParam(
CursorApiResponse.toCursorId(param.artistId()),
param.artistId(),
param.artistImageUrl(),
param.artistKoreanName(),
param.artistEnglishName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.response.CursorApiResponse;
import org.example.dto.response.PaginationApiResponse;
import org.example.security.dto.AuthenticatedInfo;
import org.example.util.ValidatorUser;
Expand Down Expand Up @@ -49,10 +51,15 @@ public ResponseEntity<PaginationApiResponse<GenrePaginationApiParam>> getGenres(
.map(GenrePaginationApiParam::new)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<GenrePaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand All @@ -70,10 +77,15 @@ public ResponseEntity<PaginationApiResponse<GenreUnsubscriptionPaginationApiPara
.map(GenreUnsubscriptionPaginationApiParam::new)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<GenreUnsubscriptionPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand All @@ -91,10 +103,15 @@ public ResponseEntity<PaginationApiResponse<GenreSubscriptionPaginationApiParam>
.map(GenreSubscriptionPaginationApiParam::new)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<GenreSubscriptionPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.example.genre.service.dto.param.GenrePaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record GenrePaginationApiParam(

@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "장르 ID")
UUID id,

@Schema(description = "장르 이름")
String name,
Expand All @@ -18,7 +18,7 @@ public record GenrePaginationApiParam(

public GenrePaginationApiParam(GenrePaginationServiceParam param) {
this(
CursorApiResponse.toCursorId(param.id()),
param.id(),
param.name(),
param.isSubscribed()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.example.genre.service.dto.param.GenreSubscriptionPaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record GenreSubscriptionPaginationApiParam(
@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "장르 ID")
UUID id,

@Schema(description = "장르 이름")
String name
Expand All @@ -16,7 +16,7 @@ public GenreSubscriptionPaginationApiParam(
GenreSubscriptionPaginationServiceParam response
) {
this(
CursorApiResponse.toCursorId(response.id()),
response.id(),
response.name()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.example.genre.service.dto.param.GenreUnsubscriptionPaginationServiceParam;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.dto.response.CursorApiResponse;
import java.util.UUID;

public record GenreUnsubscriptionPaginationApiParam(
@Schema(description = "조회한 데이터의 Cursor")
CursorApiResponse cursor,
@Schema(description = "장르 ID")
UUID id,

@Schema(description = "장르 이름")
String name
Expand All @@ -16,7 +16,7 @@ public GenreUnsubscriptionPaginationApiParam(
GenreUnsubscriptionPaginationServiceParam response
) {
this(
CursorApiResponse.toCursorId(response.id()),
response.id(),
response.name()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.response.CursorApiResponse;
import org.example.dto.response.PaginationApiResponse;
import org.example.security.dto.AuthenticatedInfo;
import org.example.util.ValidatorUser;
Expand Down Expand Up @@ -41,10 +43,15 @@ public ResponseEntity<PaginationApiResponse<ShowPaginationApiParam>> getShows(
.map(ShowPaginationApiParam::from)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<ShowPaginationApiParam>builder()
.data(data)
.hasNext(response.hasNext())
.cursor(cursor)
.build()
);
}
Expand All @@ -69,15 +76,19 @@ public ResponseEntity<PaginationApiResponse<ShowSearchPaginationApiParam>> searc
@Valid @ParameterObject ShowSearchPaginationApiRequest request
) {
var response = showService.searchShow(request.toServiceRequest());

var data = response.data().stream()
.map(ShowSearchPaginationApiParam::from)
.toList();

CursorApiResponse cursor = Optional.ofNullable(CursorApiResponse.getLastElement(data))
.map(element -> CursorApiResponse.toCursorId(element.id()))
.orElse(CursorApiResponse.noneCursor());

return ResponseEntity.ok(
PaginationApiResponse.<ShowSearchPaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.cursor(cursor)
.build()
);
}
Expand Down
Loading

0 comments on commit 7addf21

Please sign in to comment.