Skip to content

Commit

Permalink
feat: 포트폴리오 조회 기능 수정 (#368)
Browse files Browse the repository at this point in the history
* feat: 포트폴리오 조회 기능 수정

* feat: 포트폴리오 조회 기능 수정 [core-domain]

* feat: toDto -> from으로 수정
  • Loading branch information
dbwp031 authored Jul 26, 2024
1 parent 58a041a commit e788b1d
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -18,6 +22,7 @@

import com.sponus.coredomain.domain.common.ApiResponse;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coredomain.domain.portfolio.repository.conditions.PortfolioSearchParam;
import com.sponus.coreinfrasecurity.annotation.AuthOrganization;
import com.sponus.sponusbe.domain.portfolio.dto.PortfolioCreateRequest;
import com.sponus.sponusbe.domain.portfolio.dto.PortfolioCreateResponse;
Expand Down Expand Up @@ -49,9 +54,10 @@ public ApiResponse<PortfolioCreateResponse> createPortfolio(
return ApiResponse.onSuccess(response);
}

@GetMapping("/{portfolioId}")
public ApiResponse<PortfolioGetResponse> getPortfolio(@PathVariable Long portfolioId) {
PortfolioGetResponse response = portfolioService.getPortfolio(portfolioId);
@GetMapping
public ApiResponse<Page<PortfolioGetResponse>> getPortfolios(@PageableDefault() Pageable pageable, @ModelAttribute
PortfolioSearchParam portfolioSearchParam) {
Page<PortfolioGetResponse> response = portfolioService.getPortfolios(portfolioSearchParam, pageable);
return ApiResponse.onSuccess(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package com.sponus.sponusbe.domain.portfolio.dto;

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

import com.sponus.coredomain.domain.portfolio.Portfolio;

public record PortfolioGetResponse(
Long portfolioId,
Long clubId,
LocalDate startDate,
LocalDate endDate,
String description,
List<PortfolioImageGetResponse> portfolioImageGetResponses
) {
public static PortfolioGetResponse from(Portfolio portfolio,
List<PortfolioImageGetResponse> portfolioImageGetResponses) {
return new PortfolioGetResponse(
portfolio.getId(),
portfolio.getClub().getId(),
portfolio.getStartDate(),
portfolio.getEndDate(),
portfolio.getDescription(),
portfolioImageGetResponses
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -17,7 +19,9 @@
import com.sponus.coredomain.domain.organization.repository.ClubRepository;
import com.sponus.coredomain.domain.portfolio.Portfolio;
import com.sponus.coredomain.domain.portfolio.PortfolioImage;
import com.sponus.coredomain.domain.portfolio.repository.PortfolioCustomRepository;
import com.sponus.coredomain.domain.portfolio.repository.PortfolioRepository;
import com.sponus.coredomain.domain.portfolio.repository.conditions.PortfolioSearchParam;
import com.sponus.coreinfras3.S3Service;
import com.sponus.sponusbe.domain.organization.exception.OrganizationException;
import com.sponus.sponusbe.domain.portfolio.dto.PortfolioCreateRequest;
Expand All @@ -37,6 +41,7 @@
@Service
public class PortfolioService {
private final PortfolioRepository portfolioRepository;
private final PortfolioCustomRepository portfolioCustomRepository;
private final ClubRepository clubRepository;
private final S3Service s3Service;

Expand Down Expand Up @@ -94,15 +99,14 @@ public PortfolioCreateResponse createPortfolio(
);
}

public PortfolioGetResponse getPortfolio(long portfolioId) {
Portfolio portfolio = portfolioRepository.findById(portfolioId)
.orElseThrow(() -> new PortfolioException(PORTFOLIO_NOT_FOUND));

List<PortfolioImageGetResponse> portfolioImageGetResponses = portfolio.getPortfolioImages().stream()
.map(image -> new PortfolioImageGetResponse(image.getId(), image.getUrl(), image.getOrder()))
.toList();

return new PortfolioGetResponse(portfolioId, portfolioImageGetResponses);
public Page<PortfolioGetResponse> getPortfolios(PortfolioSearchParam portfolioSearchParam, Pageable pageable) {
Page<Portfolio> queryResult = portfolioCustomRepository.findAllByConditions(portfolioSearchParam, pageable);
return queryResult.map(portfolio -> {
List<PortfolioImageGetResponse> portfolioImageGetResponses = portfolio.getPortfolioImages().stream()
.map(image -> new PortfolioImageGetResponse(image.getId(), image.getUrl(), image.getOrder()))
.toList();
return PortfolioGetResponse.from(portfolio, portfolioImageGetResponses);
});
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sponus.coredomain.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.querydsl.jpa.impl.JPAQueryFactory;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

@Configuration
public class QuerydslConfig {
@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Portfolio {

@Setter
@Builder.Default
@OneToMany(cascade = {CascadeType.ALL})
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
private List<PortfolioImage> portfolioImages = new ArrayList<>();

public void addPortfolioImage(PortfolioImage portfolioImage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sponus.coredomain.domain.portfolio.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.sponus.coredomain.domain.portfolio.Portfolio;
import com.sponus.coredomain.domain.portfolio.repository.conditions.PortfolioSearchParam;

public interface PortfolioCustomRepository {
Page<Portfolio> findAllByConditions(PortfolioSearchParam portfolioSearchParam, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.sponus.coredomain.domain.portfolio.repository;

import static com.sponus.coredomain.domain.portfolio.QPortfolio.*;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.sponus.coredomain.domain.portfolio.Portfolio;
import com.sponus.coredomain.domain.portfolio.repository.conditions.PortfolioSearchParam;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class PortfolioRepositoryImpl implements PortfolioCustomRepository {
private final JPAQueryFactory queryFactory;

@Override
public Page<Portfolio> findAllByConditions(PortfolioSearchParam portfolioSearchParam,
Pageable pageable) {
JPAQuery<Long> countQuery = queryFactory
.select(portfolio.count())
.from(portfolio);

List<Portfolio> portfolios = queryFactory
.select(portfolio)
.from(portfolio)
.where(
isClubId(portfolioSearchParam.clubId()),
isPortfolioId(portfolioSearchParam.portfolioId())
)
.orderBy(portfolio.id.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

return PageableExecutionUtils.getPage(portfolios, pageable, countQuery::fetchOne);
}

private BooleanExpression isClubId(Long clubId) {
return clubId != null ? portfolio.club.id.eq(clubId) : null;
}

private BooleanExpression isPortfolioId(Long portfolioId) {
return portfolioId != null ? portfolio.club.id.eq(portfolioId) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sponus.coredomain.domain.portfolio.repository.conditions;

public record PortfolioSearchParam(
Long portfolioId,
Long clubId
) {
}

0 comments on commit e788b1d

Please sign in to comment.