Skip to content

Commit

Permalink
[refactor] #221 - 메인페이지에서 캐러셀 응답 시 캐러셀 번호로 정렬해서 주도록 변경 및 메인 페이지 조회 코드…
Browse files Browse the repository at this point in the history
… 최적화 (#222)

* [#221] rename: DTO 이름 변경 및 import문 최신화

* [#221] chore(AdminApi): 통일성을 위해 mapping 어노테이션 삭제

* [#221] rename(HomeFindRequest): 요청 DTO 네이밍 구체화

* [#221] rename(HomeFindAllResponse): 응답 DTO 네이밍 구체화

* [#221] refactor(PerformanceService): getHomePerformanceList, getPromotions 메서드 이동 및 코드 포맷팅 적용

* [#221] feat(HomeService): 공연 및 홍보 조회 서비스 로직 최적화

* [#221] feat(ScheduleService): 공연의 회차 중 가장 dueDate가 적은 회차의 dueDate를 반환하는 메서드 구현

* [#221] refactor(HomePromotionDetail): 정적 팩토리 메서드 내에서 초기화해서 return 하도록 변경

* [#221] refactor(HomePerformanceDetail): 정적 팩토리 메서드 내에서 초기화해서 return 하도록 변경

* [#221] feat(HomeApi): Home API 스웨거 명세서 작성

* [#221] refactor(HomeController): 로직 최적화

* [#221] fix(HomeService): 음수는 내림차순, 양수는 오름차순으로 정렬하도록 수정
  • Loading branch information
hoonyworld authored Oct 2, 2024
1 parent 2846432 commit 2dbb3a4
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 310 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/beat/admin/adapter/in/api/AdminApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.beat.admin.application.dto.response.CarouselFindAllResponse;
import com.beat.admin.application.dto.response.UserFindAllResponse;
import com.beat.admin.application.dto.request.CarouselProcessRequest;
import com.beat.admin.application.dto.response.CarouselProcessAllResponse;
import com.beat.admin.application.dto.request.CarouselHandleRequest;
import com.beat.admin.application.dto.response.CarouselHandleAllResponse;
import com.beat.global.auth.annotation.CurrentMember;
import com.beat.global.common.dto.ErrorResponse;
import com.beat.global.common.dto.SuccessResponse;
Expand All @@ -16,7 +16,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

Expand Down Expand Up @@ -128,9 +127,8 @@ ResponseEntity<SuccessResponse<CarouselFindAllResponse>> readAllCarouselImages(
)
}
)
@PutMapping("/carousels")
ResponseEntity<SuccessResponse<CarouselProcessAllResponse>> processCarouselImages(
ResponseEntity<SuccessResponse<CarouselHandleAllResponse>> processCarouselImages(
@CurrentMember Long memberId,
@RequestBody CarouselProcessRequest request
@RequestBody CarouselHandleRequest request
);
}
10 changes: 5 additions & 5 deletions src/main/java/com/beat/admin/adapter/in/api/AdminController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.beat.admin.adapter.in.api;

import com.beat.admin.application.dto.response.CarouselFindAllResponse;
import com.beat.admin.application.dto.request.CarouselProcessRequest;
import com.beat.admin.application.dto.response.CarouselProcessAllResponse;
import com.beat.admin.application.dto.request.CarouselHandleRequest;
import com.beat.admin.application.dto.response.CarouselHandleAllResponse;
import com.beat.admin.exception.AdminSuccessCode;
import com.beat.admin.application.dto.response.UserFindAllResponse;
import com.beat.admin.facade.AdminFacade;
Expand Down Expand Up @@ -69,10 +69,10 @@ public ResponseEntity<SuccessResponse<CarouselFindAllResponse>> readAllCarouselI

@Override
@PutMapping("/carousels")
public ResponseEntity<SuccessResponse<CarouselProcessAllResponse>> processCarouselImages(
public ResponseEntity<SuccessResponse<CarouselHandleAllResponse>> processCarouselImages(
@CurrentMember Long memberId,
@RequestBody CarouselProcessRequest request) {
CarouselProcessAllResponse response = adminFacade.checkMemberAndProcessAllPromotionsSortedByCarouselNumber(memberId, request);
@RequestBody CarouselHandleRequest request) {
CarouselHandleAllResponse response = adminFacade.checkMemberAndProcessAllPromotionsSortedByCarouselNumber(memberId, request);
return ResponseEntity.status(HttpStatus.OK)
.body(SuccessResponse.of(AdminSuccessCode.UPDATE_ALL_CAROUSEL_PROMOTIONS_SUCCESS, response));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/beat/admin/application/AdminService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.beat.admin.application;

import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionModifyRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionModifyRequest;
import com.beat.admin.port.in.AdminUseCase;
import com.beat.domain.performance.domain.Performance;
import com.beat.domain.performance.port.in.PerformanceUseCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.beat.domain.promotion.domain.CarouselNumber;

public record CarouselProcessRequest(
public record CarouselHandleRequest(
List<PromotionHandleRequest> carousels
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.beat.admin.application.dto.request;

import static com.beat.admin.application.dto.request.CarouselProcessRequest.*;
import static com.beat.admin.application.dto.request.CarouselHandleRequest.*;

import com.beat.domain.promotion.domain.CarouselNumber;
import com.fasterxml.jackson.annotation.JsonSubTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import com.beat.domain.promotion.domain.Promotion;

public record CarouselProcessAllResponse(
public record CarouselHandleAllResponse(
List<PromotionResponse> modifiedPromotions
) {

public static CarouselProcessAllResponse from(List<Promotion> promotions) {
public static CarouselHandleAllResponse from(List<Promotion> promotions) {
List<PromotionResponse> modifiedPromotions = promotions.stream()
.map(PromotionResponse::from)
.collect(Collectors.toList());
return new CarouselProcessAllResponse(modifiedPromotions);
return new CarouselHandleAllResponse(modifiedPromotions);
}

public record PromotionResponse(
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/beat/admin/facade/AdminFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.beat.admin.application.dto.request.PromotionHandleRequest;
import com.beat.admin.application.dto.response.CarouselFindAllResponse;
import com.beat.admin.application.dto.response.UserFindAllResponse;
import com.beat.admin.application.dto.request.CarouselProcessRequest;
import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionModifyRequest;
import com.beat.admin.application.dto.response.CarouselProcessAllResponse;
import com.beat.admin.application.dto.request.CarouselHandleRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionModifyRequest;
import com.beat.admin.application.dto.response.CarouselHandleAllResponse;
import com.beat.admin.port.in.AdminUseCase;
import com.beat.domain.member.port.in.MemberUseCase;
import com.beat.domain.promotion.domain.CarouselNumber;
Expand Down Expand Up @@ -64,8 +64,8 @@ public CarouselFindAllResponse checkMemberAndFindAllPromotionsSortedByCarouselNu
return CarouselFindAllResponse.from(promotions);
}

public CarouselProcessAllResponse checkMemberAndProcessAllPromotionsSortedByCarouselNumber(Long memberId,
CarouselProcessRequest request) {
public CarouselHandleAllResponse checkMemberAndProcessAllPromotionsSortedByCarouselNumber(Long memberId,
CarouselHandleRequest request) {

memberUseCase.findMemberById(memberId);

Expand All @@ -85,10 +85,10 @@ public CarouselProcessAllResponse checkMemberAndProcessAllPromotionsSortedByCaro
List<Promotion> sortedPromotions = adminUsecase.processPromotionsAndSortByCarouselNumber(modifyRequests,
generateRequests, deleteCarouselNumbers, overlappingCarouselNumbers);

return CarouselProcessAllResponse.from(sortedPromotions);
return CarouselHandleAllResponse.from(sortedPromotions);
}

private void categorizePromotionRequests(CarouselProcessRequest request,
private void categorizePromotionRequests(CarouselHandleRequest request,
List<PromotionModifyRequest> modifyRequests, List<PromotionGenerateRequest> generateRequests,
Set<CarouselNumber> requestCarouselNumbers) {

Expand All @@ -111,15 +111,15 @@ private List<CarouselNumber> findDeleteCarouselNumbers(Set<CarouselNumber> reque
}

private List<CarouselNumber> findOverlappingCarouselNumbers(Set<CarouselNumber> requestCarouselNumbers,
List<CarouselNumber> allExistingCarouselNumbers, CarouselProcessRequest request) {
List<CarouselNumber> allExistingCarouselNumbers, CarouselHandleRequest request) {
return allExistingCarouselNumbers.stream()
.filter(requestCarouselNumbers::contains)
.filter(existingCarouselNumber -> {
Promotion existingPromotion = promotionUseCase.findPromotionByCarouselNumber(existingCarouselNumber);
return request.carousels()
.stream()
.filter(req -> req instanceof CarouselProcessRequest.PromotionModifyRequest)
.map(req -> (CarouselProcessRequest.PromotionModifyRequest)req)
.filter(req -> req instanceof CarouselHandleRequest.PromotionModifyRequest)
.map(req -> (CarouselHandleRequest.PromotionModifyRequest)req)
.noneMatch(req -> req.promotionId() != null && req.promotionId().equals(existingPromotion.getId()));
})
.toList();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/beat/admin/port/in/AdminUseCase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.beat.admin.port.in;

import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselProcessRequest.PromotionModifyRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionGenerateRequest;
import com.beat.admin.application.dto.request.CarouselHandleRequest.PromotionModifyRequest;
import com.beat.domain.promotion.domain.CarouselNumber;
import com.beat.domain.promotion.domain.Promotion;

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/beat/domain/performance/api/HomeApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.beat.domain.performance.api;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;

import com.beat.domain.performance.application.dto.home.HomeFindAllResponse;
import com.beat.domain.performance.domain.Genre;
import com.beat.global.common.dto.ErrorResponse;
import com.beat.global.common.dto.SuccessResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "Home", description = "홈 화면에서 공연 및 홍보목록 조회 API")
public interface HomeApi {

@Operation(summary = "전체 공연 및 홍보 목록 조회", description = "홈 화면에서 전체 공연 목록 및 홍보 목록을 조회하는 GET API")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "홈 화면 공연 목록 조회가 성공적으로 완료되었습니다.",
content = @Content(schema = @Schema(implementation = SuccessResponse.class))
)
}
)
ResponseEntity<SuccessResponse<HomeFindAllResponse>> getHomePerformanceList(
@RequestParam(required = false) Genre genre);
}
39 changes: 20 additions & 19 deletions src/main/java/com/beat/domain/performance/api/HomeController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.beat.domain.performance.api;

import com.beat.domain.performance.application.PerformanceService;
import com.beat.domain.performance.application.dto.home.HomeRequest;
import com.beat.domain.performance.application.dto.home.HomeResponse;
import com.beat.domain.performance.application.HomeService;
import com.beat.domain.performance.application.dto.home.HomeFindRequest;
import com.beat.domain.performance.application.dto.home.HomeFindAllResponse;
import com.beat.domain.performance.domain.Genre;
import com.beat.domain.performance.exception.PerformanceSuccessCode;
import com.beat.global.common.dto.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;

import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,19 +18,19 @@
@RestController
@RequestMapping("/api/main")
@RequiredArgsConstructor
public class HomeController {

private final PerformanceService performanceService;

@Operation(summary = "전체공연목록, 홍보목록 조회 API", description = "홈화면에서 전체공연목록, 홍보목록을 조회하는 GET API입니다.")
@GetMapping
public ResponseEntity<SuccessResponse<HomeResponse>> getHomePerformanceList(@RequestParam(required = false) String genre) {
HomeRequest homeRequest;
if (genre != null) {
homeRequest = new HomeRequest(Genre.valueOf(genre));
} else {
homeRequest = new HomeRequest(null);
} HomeResponse homeResponse = performanceService.getHomePerformanceList(homeRequest);
return ResponseEntity.ok(SuccessResponse.of(PerformanceSuccessCode.HOME_PERFORMANCE_RETRIEVE_SUCCESS, homeResponse));
}
public class HomeController implements HomeApi {

private final HomeService homeService;

@Override
@GetMapping
public ResponseEntity<SuccessResponse<HomeFindAllResponse>> getHomePerformanceList(
@RequestParam(required = false) Genre genre) {

HomeFindRequest request = new HomeFindRequest(genre);

HomeFindAllResponse response = homeService.findHomePerformanceList(request);
return ResponseEntity.ok(
SuccessResponse.of(PerformanceSuccessCode.HOME_PERFORMANCE_RETRIEVE_SUCCESS, response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.beat.domain.performance.application;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beat.domain.performance.application.dto.home.HomeFindAllResponse;
import com.beat.domain.performance.application.dto.home.HomeFindRequest;
import com.beat.domain.performance.application.dto.home.HomePerformanceDetail;
import com.beat.domain.performance.application.dto.home.HomePromotionDetail;
import com.beat.domain.performance.dao.PerformanceRepository;
import com.beat.domain.performance.domain.Genre;
import com.beat.domain.performance.domain.Performance;
import com.beat.domain.promotion.domain.Promotion;
import com.beat.domain.promotion.port.in.PromotionUseCase;
import com.beat.domain.schedule.application.ScheduleService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class HomeService {

private final ScheduleService scheduleService;
private final PromotionUseCase promotionUseCase;

private final PerformanceRepository performanceRepository;

@Transactional(readOnly = true)
public HomeFindAllResponse findHomePerformanceList(HomeFindRequest homeFindRequest) {

List<Performance> performances = findPerformancesByGenre(homeFindRequest);
List<HomePromotionDetail> promotions = findAllPromotionsSortedByCarouselNumber();

if (performances.isEmpty()) {
return HomeFindAllResponse.of(promotions, new ArrayList<>());
}

List<HomePerformanceDetail> sortedPerformances = performances.stream()
.map(performance -> {
int minDueDate = scheduleService.getMinDueDateForPerformance(performance.getId());
return HomePerformanceDetail.of(performance, minDueDate);
})
.sorted(Comparator.<HomePerformanceDetail>comparingInt(detail -> detail.dueDate() < 0 ? 1 : 0)
.thenComparingInt(detail -> Math.abs(detail.dueDate())))
.toList();

return HomeFindAllResponse.of(promotions, sortedPerformances);
}

private List<Performance> findPerformancesByGenre(HomeFindRequest homeFindRequest) {
Genre genre = homeFindRequest.genre();

if (genre != null) {
return performanceRepository.findByGenre(genre);
}

return performanceRepository.findAll();
}

private List<HomePromotionDetail> findAllPromotionsSortedByCarouselNumber() {
return promotionUseCase.findAllPromotions()
.stream()
.sorted(Comparator.comparing(Promotion::getCarouselNumber, Comparator.comparingInt(Enum::ordinal)))
.map(HomePromotionDetail::from)
.toList();
}
}
Loading

0 comments on commit 2dbb3a4

Please sign in to comment.