Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 장르 전체 목록 조회 기능 구현 #132

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.genre.controller;

import com.example.genre.controller.dto.param.GenrePaginationApiParam;
import com.example.genre.controller.dto.param.GenreSubscriptionPaginationApiParam;
import com.example.genre.controller.dto.param.GenreUnsubscriptionPaginationApiParam;
import com.example.genre.controller.dto.request.GenrePaginationApiRequest;
import com.example.genre.controller.dto.request.GenreSubscriptionApiRequest;
import com.example.genre.controller.dto.request.GenreSubscriptionPaginationApiRequest;
import com.example.genre.controller.dto.request.GenreUnsubscriptionApiRequest;
Expand Down Expand Up @@ -32,13 +34,33 @@ public class GenreController {

private final GenreService genreService;

@GetMapping
@Operation(summary = "장르 전체 목록 조회")
public ResponseEntity<PaginationApiResponse<GenrePaginationApiParam>> getGenres(
@AuthenticationPrincipal AuthenticatedUser user,
@ParameterObject GenrePaginationApiRequest request
) {
var response = genreService.findGenres(request.toServiceRequest(user));
var data = response.data().stream()
.map(GenrePaginationApiParam::new)
.toList();

return ResponseEntity.ok(
PaginationApiResponse.<GenrePaginationApiParam>builder()
.hasNext(response.hasNext())
.data(data)
.build()
);
}

@GetMapping("/unsubscriptions")
@Operation(summary = "구독하지 않은 장르 목록 조회")
public ResponseEntity<PaginationApiResponse<GenreUnsubscriptionPaginationApiParam>> getUnsubscribedGenres(
@AuthenticationPrincipal AuthenticatedUser user,
@ParameterObject GenreUnsubscriptionPaginationApiRequest request
) {
var response = genreService.findGenreUnSubscriptions(request.toServiceRequest(user.userId()));
var response = genreService.findGenreUnSubscriptions(
request.toServiceRequest(user.userId()));
var data = response.data().stream()
.map(GenreUnsubscriptionPaginationApiParam::new)
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.genre.controller.dto.param;

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

public record GenrePaginationApiParam(

@Schema(description = "장르 ID")
UUID id,

@Schema(description = "장르 이름")
String name,

@Schema(description = "장르 구독 여부")
boolean isSubscribed
) {

public GenrePaginationApiParam(GenrePaginationServiceParam param) {
this(
param.id(),
param.name(),
param.isSubscribed()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.example.genre.controller.dto.request;

import com.example.genre.service.dto.request.GenrePaginationServiceRequest;
import com.example.vo.SubscriptionStatusApiType;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.UUID;
import org.example.pagination.vo.SortDirection;
import org.springdoc.core.annotations.ParameterObject;
import org.example.security.dto.AuthenticatedUser;

@ParameterObject
public record GenrePaginationApiRequest(

@Parameter(description = "정렬 방향")
SortDirection sortDirection,

@Parameter(description = "이전 페이지네이션 마지막 데이터의 ID / 최초 조회라면 null")
UUID cursor
UUID cursor,

@Parameter(description = "조회하는 데이터 개수", required = true)
int size
) {

public GenrePaginationServiceRequest toServiceRequest(AuthenticatedUser user) {
UUID userId = user == null ? null : user.userId();

return GenrePaginationServiceRequest.builder()
.type(SubscriptionStatusApiType.DEFAULTED)
.cursor(cursor)
.size(size)
.userId(userId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.genre.service;

import com.example.genre.service.dto.param.GenrePaginationServiceParam;
import com.example.genre.service.dto.param.GenreSubscriptionPaginationServiceParam;
import com.example.genre.service.dto.param.GenreUnsubscriptionPaginationServiceParam;
import com.example.genre.service.dto.request.GenrePaginationServiceRequest;
import com.example.genre.service.dto.request.GenreSubscriptionPaginationServiceRequest;
import com.example.genre.service.dto.request.GenreSubscriptionServiceRequest;
import com.example.genre.service.dto.request.GenreUnsubscriptionPaginationServiceRequest;
Expand Down Expand Up @@ -98,6 +100,21 @@ public GenreUnsubscriptionServiceResponse unsubscribe(
.build();
}

public PaginationServiceResponse<GenrePaginationServiceParam> findGenres(GenrePaginationServiceRequest request) {
List<UUID> subscriptionGenreIds = request.userId() == null
? List.of()
: getSubscriptionGenreIds(request.userId());

GenrePaginationDomainResponse response = genreUseCase.findGenreWithCursorPagination(
request.toDomainRequest());

List<GenrePaginationServiceParam> data = response.data().stream()
.map(genre -> GenrePaginationServiceParam.of(genre, subscriptionGenreIds))
.toList();

return PaginationServiceResponse.of(data, response.hasNext());
}

public PaginationServiceResponse<GenreSubscriptionPaginationServiceParam> findGenreSubscriptions(
GenreSubscriptionPaginationServiceRequest request
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.genre.service.dto.param;

import java.util.List;
import java.util.UUID;
import lombok.Builder;
import org.example.dto.genre.response.GenreDomainResponse;

@Builder
public record GenrePaginationServiceParam(
UUID id,
String name,
boolean isSubscribed
) {

public static GenrePaginationServiceParam of(
GenreDomainResponse genre,
List<UUID> subscriptionGenreIds
) {
boolean isSubscribed = subscriptionGenreIds.contains(genre.id());

return GenrePaginationServiceParam.builder()
.id(genre.id())
.name(genre.name())
.isSubscribed(isSubscribed)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.genre.service.dto.request;

import com.example.vo.SubscriptionStatusApiType;
import java.util.List;
import java.util.UUID;
import lombok.Builder;
import org.example.dto.genre.request.GenrePaginationDomainRequest;

@Builder
public record GenrePaginationServiceRequest(
SubscriptionStatusApiType type,
UUID cursor,
int size,
UUID userId
) {
public GenrePaginationDomainRequest toDomainRequest() {
return GenrePaginationDomainRequest.builder()
.subscriptionStatus(type.toDomainType())
.cursor(cursor)
.size(size)
.genreIds(List.of())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

public enum SubscriptionStatusApiType {
SUBSCRIBED,
UNSUBSCRIBED;
UNSUBSCRIBED,
DEFAULTED;

public SubscriptionStatus toDomainType() {
return switch (this) {
case SUBSCRIBED -> SubscriptionStatus.SUBSCRIBED;
case UNSUBSCRIBED -> SubscriptionStatus.UNSUBSCRIBED;
case DEFAULTED -> SubscriptionStatus.DEFAULTED;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@ private BooleanBuilder getWhereClauseInCursorPagination(
BooleanBuilder whereClause = new BooleanBuilder();
whereClause.and(getDefaultPredicateInCursorPagination(cursor));

if (status.equals(SubscriptionStatus.SUBSCRIBED)) {
return whereClause.and(genre.id.in(genreIds));
switch (status) {
case SUBSCRIBED -> whereClause.and(genre.id.in(genreIds));
case UNSUBSCRIBED -> whereClause.and(genre.id.notIn(genreIds));
default -> {
return whereClause;
}
}
return whereClause.and(genre.id.notIn(genreIds));

return whereClause;
}

private Predicate getDefaultPredicateInCursorPagination(UUID cursor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum SubscriptionStatus {
SUBSCRIBED,
UNSUBSCRIBED
UNSUBSCRIBED,
DEFAULTED
}
Loading