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

코드 리팩토링 #42

Merged
merged 4 commits into from
Oct 5, 2023
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
Expand Up @@ -10,11 +10,11 @@

import java.time.LocalDateTime;

@Getter
@Entity
@NoArgsConstructor
@Getter
@Table(name = "portfolio_tb")
@OnDelete(action= OnDeleteAction.CASCADE)
@Entity
public class Portfolio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.kakao.sunsuwedding.portfolio;

import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioDTO;
import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioListItemDTO;
import com.kakao.sunsuwedding.portfolio.dto.response.PriceDTO;
import com.kakao.sunsuwedding.portfolio.dto.response.PriceItemDTO;
import com.kakao.sunsuwedding.portfolio.price.PriceCalculator;
import com.kakao.sunsuwedding.portfolio.price.PriceItem;

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

public class PortfolioDTOConverter {
public static PortfolioDTO toPortfolioDTO(Portfolio portfolio, List<String> images, List<PriceItem> priceItems) {
List<PriceItemDTO> priceItemDTOS = toPriceItemDTOS(priceItems);
public static PortfolioResponse.findById toPortfolioDTO(Portfolio portfolio, List<String> images, List<PriceItem> priceItems) {
List<PortfolioResponse.PriceItemDTO> priceItemDTOS = toPriceItemDTOS(priceItems);

Long totalPrice = PriceCalculator.execute(priceItemDTOS);
PriceDTO priceDTO = new PriceDTO(totalPrice, priceItemDTOS);
PortfolioResponse.PriceDTO priceDTO = new PortfolioResponse.PriceDTO(totalPrice, priceItemDTOS);

return toPortfolioDTO(portfolio, images, priceDTO);
}

private static PortfolioDTO toPortfolioDTO(Portfolio portfolio, List<String> images, PriceDTO priceDTO) {
return new PortfolioDTO(
private static PortfolioResponse.findById toPortfolioDTO(Portfolio portfolio, List<String> images, PortfolioResponse.PriceDTO priceDTO) {
return new PortfolioResponse.findById(
portfolio.getId(),
images,
portfolio.getTitle(),
Expand All @@ -35,12 +31,12 @@ private static PortfolioDTO toPortfolioDTO(Portfolio portfolio, List<String> ima
);
}

public static List<PortfolioListItemDTO> toListItemDTO(List<Portfolio> portfolios, List<String> images) {
public static List<PortfolioResponse.findAllBy> toListItemDTO(List<Portfolio> portfolios, List<String> images) {
return IntStream
.range(0, portfolios.size())
.mapToObj(i -> {
Portfolio portfolio = portfolios.get(i);
return new PortfolioListItemDTO(
return new PortfolioResponse.findAllBy(
portfolio.getId(),
images.get(i),
portfolio.getTitle(),
Expand All @@ -53,10 +49,10 @@ public static List<PortfolioListItemDTO> toListItemDTO(List<Portfolio> portfolio
.toList();
}

public static List<PriceItemDTO> toPriceItemDTOS(List<PriceItem> priceItems) {
public static List<PortfolioResponse.PriceItemDTO> toPriceItemDTOS(List<PriceItem> priceItems) {
return priceItems
.stream()
.map(priceItem -> new PriceItemDTO(priceItem.getItemTitle(), priceItem.getItemPrice()))
.map(priceItem -> new PortfolioResponse.PriceItemDTO(priceItem.getItemTitle(), priceItem.getItemPrice()))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
package com.kakao.sunsuwedding.portfolio;

import java.util.List;

public class PortfolioResponse {
public record findAllBy(
Long id,
String image,
String title,
String plannerName,
Long price,
String location,
Long contractCount
) {
}

public record findById(
Long id,
List<String> images,
String title,
String plannerName,
Long contractCount,
PriceDTO priceInfo,
String location,
String description,
String career,
String partnerCompany
) {
}

public record PriceDTO(
Long totalPrice,
List<PriceItemDTO> itemPrices
) {
}

public record PriceItemDTO(
String itemTitle,
Long itemPrice
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.kakao.sunsuwedding._core.security.CustomUserDetails;
import com.kakao.sunsuwedding._core.utils.ApiUtils;
import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioDTO;
import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioListItemDTO;
import com.kakao.sunsuwedding.portfolio.image.ImageItemService;
import com.kakao.sunsuwedding.user.planner.Planner;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -39,13 +37,13 @@ public ResponseEntity<?> addPortfolios(@RequestPart PortfolioRequest.addDTO requ
@GetMapping(value = "/portfolios")
public ResponseEntity<?> getPortfolios(@RequestParam @Min(0) int page) {
PageRequest pageRequest = PageRequest.of(page, PAGE_SIZE);
List<PortfolioListItemDTO> items = portfolioService.getPortfolios(pageRequest);
List<PortfolioResponse.findAllBy> items = portfolioService.getPortfolios(pageRequest);
return ResponseEntity.ok().body(ApiUtils.success(items));
}

@GetMapping("/portfolios/{id}")
public ResponseEntity<?> getPortfolioInDetail(@PathVariable @Min(1) Long id) {
PortfolioDTO portfolio = portfolioService.getPortfolioById(id);
PortfolioResponse.findById portfolio = portfolioService.getPortfolioById(id);
return ResponseEntity.ok().body(ApiUtils.success(portfolio));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.kakao.sunsuwedding._core.errors.exception.Exception400;
import com.kakao.sunsuwedding._core.errors.exception.Exception403;
import com.kakao.sunsuwedding._core.errors.exception.Exception404;
import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioDTO;
import com.kakao.sunsuwedding.portfolio.dto.response.PortfolioListItemDTO;
import com.kakao.sunsuwedding.portfolio.image.ImageEncoder;
import com.kakao.sunsuwedding.portfolio.image.ImageItem;
import com.kakao.sunsuwedding.portfolio.image.ImageItemJPARepository;
Expand Down Expand Up @@ -82,7 +80,7 @@ public Pair<Portfolio, Planner> addPortfolio(PortfolioRequest.addDTO request, in
return Pair.of(portfolio, planner);
}

public List<PortfolioListItemDTO> getPortfolios(PageRequest pageRequest) {
public List<PortfolioResponse.findAllBy> getPortfolios(PageRequest pageRequest) {
List<Portfolio> portfolios = portfolioJPARepository.findAll(pageRequest).getContent();

List<String> images = imageItemJPARepository.findAllByThumbnailAndPortfolioIn(true, portfolios)
Expand All @@ -93,7 +91,7 @@ public List<PortfolioListItemDTO> getPortfolios(PageRequest pageRequest) {
return PortfolioDTOConverter.toListItemDTO(portfolios, images);
}

public PortfolioDTO getPortfolioById(Long id) {
public PortfolioResponse.findById getPortfolioById(Long id) {
List<ImageItem> imageItems = imageItemJPARepository.findByPortfolioId(id);
if (imageItems.isEmpty()) {
throw new Exception404(BaseException.PORTFOLIO_NOT_FOUND.getMessage());
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kakao.sunsuwedding._core.errors.BaseException;
import com.kakao.sunsuwedding._core.errors.exception.Exception404;
import com.kakao.sunsuwedding._core.errors.exception.Exception500;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

Expand All @@ -19,9 +20,7 @@ public static String encode(ImageItem imageItem) {
return Base64.getEncoder().encodeToString(resource.getContentAsByteArray());
}
catch (IOException exception) {
exception.printStackTrace();
throw new Exception500("이미지 인코딩 과정에서 오류가 발생했습니다.");
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "imageitem_tb")
@NoArgsConstructor
@NamedEntityGraphs({
@NamedEntityGraph(
Expand All @@ -19,8 +20,7 @@
subgraphs = @NamedSubgraph(name = "portfolioSubgraph", attributeNodes = @NamedAttributeNode("planner"))
)
})
@Table(name = "imageitem_tb")
@Entity
@Getter
public class ImageItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.kakao.sunsuwedding.portfolio.price;

import com.kakao.sunsuwedding.portfolio.dto.response.PriceItemDTO;
import com.kakao.sunsuwedding.portfolio.PortfolioResponse;

import java.util.List;

public class PriceCalculator {
public static Long execute(List<PriceItemDTO> priceItemDTOS) {
return priceItemDTOS.stream().mapToLong(PriceItemDTO::itemPrice).sum();
public static Long execute(List<PortfolioResponse.PriceItemDTO> priceItemDTOS) {
return priceItemDTOS.stream().mapToLong(PortfolioResponse.PriceItemDTO::itemPrice).sum();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "priceitem_tb")
@NoArgsConstructor
@NamedEntityGraphs({
@NamedEntityGraph(
Expand All @@ -19,8 +20,7 @@
subgraphs = @NamedSubgraph(name = "portfolioSubgraph", attributeNodes = @NamedAttributeNode("planner"))
)
})
@Table(name = "priceitem_tb")
@Entity
@Getter
public class PriceItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Loading