Skip to content

Commit

Permalink
refactor: 코드 리팩토링 #34
Browse files Browse the repository at this point in the history
refactor: 코드 리팩토링 #34
  • Loading branch information
yj-leez authored Apr 10, 2024
2 parents 4661a83 + d141ecb commit db191e0
Show file tree
Hide file tree
Showing 51 changed files with 683 additions and 423 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'com.h2database:h2'

/* swagger */
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
Expand All @@ -57,6 +58,9 @@ dependencies {

/* redis */
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis'

// H2 Database
implementation 'com.h2database:h2'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import static org.springframework.util.StringUtils.*;
import org.springframework.web.bind.annotation.RestController;
import server.acode.domain.family.dto.request.FragranceFilterCond;
import server.acode.domain.family.dto.response.DisplayBrand;
import server.acode.domain.family.dto.response.DisplayFamily;
import server.acode.domain.family.dto.response.DisplayIngredient;
import server.acode.domain.family.dto.response.BrandDetailsDto;
import server.acode.domain.family.dto.response.FamilyDetailsDto;
import server.acode.domain.family.dto.response.IngredientDetailsDto;
import server.acode.domain.family.dto.response.PageableResponse;
import server.acode.domain.family.service.DisplayService;
import server.acode.global.common.PageRequest;
Expand All @@ -31,31 +31,31 @@ public class DisplayController {
"계열 두 개 검색 시에는 두 계열 사이 공백 한 칸 (url 상으로는 %20) 넣어주세요. " +
"페이지는 파라미터 없을 시 기본 page = 1, size = 10입니다.")
@GetMapping("/display")
public PageableResponse displayV1(FragranceFilterCond cond, PageRequest pageRequest){
public PageableResponse displayFragranceV1(FragranceFilterCond cond, PageRequest pageRequest){

// param에 따라 계열별/브랜드별 or 추천향료별 분기
PageableResponse response = hasText(cond.getIngredient())
? displayService.searchFragranceListByIngredient(cond.getIngredient(), pageRequest)
: displayService.searchFragranceList(cond, pageRequest);
? displayService.searchFragranceByIngredient(cond.getIngredient(), pageRequest)
: displayService.searchFragranceByBrandAndFamily(cond, pageRequest);

return response;
}

@Operation(summary = "브랜드 설명")
@GetMapping("/display/brand/{brand}")
public DisplayBrand displayBrandV1(@PathVariable("brand") String brand){
public BrandDetailsDto displayBrandV1(@PathVariable("brand") String brand){
return displayService.getBrandContent(brand);
}

@Operation(summary = "계열 설명")
@GetMapping("/display/family/{family}")
public DisplayFamily displayFamilyV1(@PathVariable("family") String family){
public FamilyDetailsDto displayFamilyV1(@PathVariable("family") String family){
return displayService.getFamilyContent(family);
}

@Operation(summary = "향료 설명")
@GetMapping("/display/ingredient/{ingredient}")
public DisplayIngredient displayIngredientV1(@PathVariable("ingredient") String ingredient){
public IngredientDetailsDto displayIngredientV1(@PathVariable("ingredient") String ingredient){
return displayService.getIngredientContent(ingredient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import server.acode.domain.family.dto.response.HomeFragrance;
import server.acode.domain.family.dto.response.HomeFragranceDto;
import server.acode.domain.family.service.HomeService;
import server.acode.domain.ingredient.dto.response.IngredientOfTheDay;

Expand All @@ -25,8 +25,8 @@ public class HomeController {
@Operation(summary = "계열별 향수 최대 6개",
description = "아직 포스터 이미지가 준비되지 않아 우디에만 테스트용으로 넣어놨습니다 우디로 테스트 해주세요")
@GetMapping("/home")
public List<HomeFragrance> searchV1(@RequestParam String family){
return homeService.search(family);
public List<HomeFragranceDto> searchMainFragranceV1(@RequestParam String family){
return homeService.searchMainFragrance(family);
}

@Operation(summary = "오늘의 추천 향료")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public class FragranceFilterCond {
private String brand;
private String family;
private String additionalFamily;
private String ingredient;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
import lombok.Getter;
import server.acode.domain.fragrance.entity.Brand;

import java.util.Arrays;
import java.util.List;

@Getter
public class DisplayBrand {
public class BrandDetailsDto {
private String korName;
private String engName;
private String summary;
private String background;

private DisplayBrand(String korName, String engName, String summary, String background){
private BrandDetailsDto(String korName, String engName, String summary, String background){
this.korName = korName;
this.engName = engName;
this.summary = summary;
this.background = background;
}

public static DisplayBrand from(Brand brand){
return new DisplayBrand(brand.getKorName(),
public static BrandDetailsDto from(Brand brand){
return new BrandDetailsDto(brand.getKorName(),
brand.getEngName(),
brand.getSummary(),
brand.getBackgroundImg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import java.util.List;

@Getter
public class DisplayFamily {
public class FamilyDetailsDto {
private String korName;
private String engName;
private List<String> keyword;
private String summary;
private String background;
private String icon;

private DisplayFamily(String korName, String engName, String keyword, String summary, String background, String icon){
private FamilyDetailsDto(String korName, String engName, String keyword, String summary, String background, String icon){
this.korName = korName;
this.engName = engName;
this.keyword = Arrays.asList(keyword.split(", "));
Expand All @@ -24,8 +24,8 @@ private DisplayFamily(String korName, String engName, String keyword, String sum
this.icon = icon;
}

public static DisplayFamily from(Family family){
return new DisplayFamily(family.getKorName(),
public static FamilyDetailsDto from(Family family){
return new FamilyDetailsDto(family.getKorName(),
family.getEngName(),
family.getKeyword(),
family.getSummary(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import server.acode.domain.fragrance.entity.Concentration;

@Data
public class DisplayFragrance {
public class FragranceCatalogDto {

private Long fragranceId;
private String brandName; // 브랜드 이름
Expand All @@ -14,7 +14,7 @@ public class DisplayFragrance {
private String concentration;

@QueryProjection
public DisplayFragrance (Long fragranceId, String brandName, String fragranceName, String thumbnail, Concentration concentration){
public FragranceCatalogDto(Long fragranceId, String brandName, String fragranceName, String thumbnail, Concentration concentration){
this.fragranceId = fragranceId;
this.brandName = brandName;
this.fragranceName = fragranceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import java.util.List;

@Data
public class HomeFragrance {
public class HomeFragranceDto {
private Long fragranceId;
private String fragranceName;
private String brandName;
private List<String> style;
private String poster;

@QueryProjection
public HomeFragrance(Long fragranceId, String fragranceName, String brandName, String style, String poster){
public HomeFragranceDto(Long fragranceId, String fragranceName, String brandName, String style, String poster){
this.fragranceId = fragranceId;
this.fragranceName = fragranceName;
this.brandName = brandName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package server.acode.domain.family.dto.response;

import lombok.Getter;
import server.acode.domain.family.entity.Family;
import server.acode.domain.ingredient.entity.Ingredient;
import server.acode.domain.ingredient.entity.IngredientType;

import java.util.Arrays;
import java.util.List;

@Getter
public class DisplayIngredient {
public class IngredientDetailsDto {
private String korName;
private String engName;
private String acode;
Expand All @@ -19,8 +18,8 @@ public class DisplayIngredient {
private String ingredientType;
private String icon;

private DisplayIngredient(String korName, String engName, String acode, String keyword,
String summary, String background, String ingredientType,String icon){
private IngredientDetailsDto(String korName, String engName, String acode, String keyword,
String summary, String background, String ingredientType, String icon){
this.korName = korName;
this.engName = engName;
this.acode = acode;
Expand All @@ -31,14 +30,14 @@ private DisplayIngredient(String korName, String engName, String acode, String k
this.icon = icon;
}

public static DisplayIngredient from(Ingredient ingredient, IngredientType type){
return new DisplayIngredient(ingredient.getKorName(),
public static IngredientDetailsDto from(Ingredient ingredient){
return new IngredientDetailsDto(ingredient.getKorName(),
ingredient.getEngName(),
ingredient.getAcode(),
ingredient.getKeyword(),
ingredient.getSummary(),
ingredient.getBackgroundImg(),
type.getKorName(),
type.getIcon());
ingredient.getIngredientType().getKorName(),
ingredient.getIngredientType().getIcon());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import server.acode.domain.family.entity.Family;

import java.util.List;
import java.util.Optional;

@Repository
public interface FamilyRepository extends JpaRepository<Family, Long>{
boolean existsByKorName(String korName);
Family findByKorName(String korName);
Optional<Family> findByKorName(String korName);

List<Family> findByIdIn(List<Long> familyIdList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.springframework.stereotype.Repository;
import server.acode.domain.family.dto.SimilarFragranceOrCond;
import server.acode.domain.family.dto.request.FragranceFilterCond;
import server.acode.domain.family.dto.response.DisplayFragrance;
import server.acode.domain.family.dto.response.HomeFragrance;
import server.acode.domain.family.dto.response.FragranceCatalogDto;
import server.acode.domain.family.dto.response.HomeFragranceDto;
import server.acode.domain.fragrance.dto.request.SearchCond;
import server.acode.domain.fragrance.dto.response.ExtractFragrance;
import server.acode.domain.fragrance.dto.response.FamilyCountDto;
Expand All @@ -16,9 +16,9 @@

@Repository
public interface FragranceFamilyRepositoryCustom {
List<HomeFragrance> search(String familyName);
List<HomeFragranceDto> search(String familyName);

Page<DisplayFragrance> searchByFilter(FragranceFilterCond cond, String additionalFamily, Pageable pageable);
Page<FragranceCatalogDto> searchByBrandAndFamily(FragranceFilterCond cond, Pageable pageable);

List<Long> searchFamilyByFragranceId(Long fragranceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import org.springframework.stereotype.Repository;
import server.acode.domain.family.dto.SimilarFragranceOrCond;
import server.acode.domain.family.dto.request.FragranceFilterCond;
import server.acode.domain.family.dto.response.DisplayFragrance;
import server.acode.domain.family.dto.response.HomeFragrance;
import server.acode.domain.family.dto.response.QDisplayFragrance;
import server.acode.domain.family.dto.response.QHomeFragrance;
import server.acode.domain.family.dto.response.FragranceCatalogDto;
import server.acode.domain.family.dto.response.HomeFragranceDto;
import server.acode.domain.family.dto.response.QFragranceCatalogDto;
import server.acode.domain.family.dto.response.QHomeFragranceDto;
import server.acode.domain.family.entity.QFragranceFamily;
import server.acode.domain.fragrance.dto.request.SearchCond;
import server.acode.domain.fragrance.dto.response.*;
Expand All @@ -39,9 +39,9 @@ public FragranceFamilyRepositoryImpl(EntityManager em) {
}

@Override
public List<HomeFragrance> search(String familyName) {
public List<HomeFragranceDto> search(String familyName) {
return queryFactory
.select(new QHomeFragrance(
.select(new QHomeFragranceDto(
fragrance.id.as("fragranceId"),
fragrance.name.as("fragranceName"),
brand.korName.as("korBrand"),
Expand All @@ -62,11 +62,11 @@ public List<HomeFragrance> search(String familyName) {
}

@Override
public Page<DisplayFragrance> searchByFilter(FragranceFilterCond cond, String additionalFamily, Pageable pageable) {
public Page<FragranceCatalogDto> searchByBrandAndFamily(FragranceFilterCond cond, Pageable pageable) {

//TODO 카운트 쿼리 분리
QueryResults<DisplayFragrance> results = queryFactory
.select(new QDisplayFragrance(
QueryResults<FragranceCatalogDto> results = queryFactory
.select(new QFragranceCatalogDto(
fragrance.id.as("fragranceId"),
brand.korName.as("brandName"),
fragrance.name.as("fragranceName"),
Expand All @@ -79,15 +79,15 @@ public Page<DisplayFragrance> searchByFilter(FragranceFilterCond cond, String ad
.join(fragrance.brand, brand)
.where(brandNameEq(cond.getBrand()),
familyNameEq(cond.getFamily()),
additionalFamilyNameEq(additionalFamily)
additionalFamilyNameEq(cond.getAdditionalFamily())
)
.groupBy(fragrance.id)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchResults();


List<DisplayFragrance> content = results.getResults();
List<FragranceCatalogDto> content = results.getResults();
long total = results.getTotal();

return new PageImpl<>(content, pageable, total);
Expand Down
Loading

0 comments on commit db191e0

Please sign in to comment.