Skip to content

Commit

Permalink
refactor: PrimaryIngredient, Functionality 엔티티 분리 (#167)
Browse files Browse the repository at this point in the history
* refactor: 동적 조회, 정적 조회 분리

* refactor: data.sql 실행 방지

* refactor: PetFoodController 인수 테스트 리팩터링

* refactor: 테스트 코드 리팩터링 및 사용하지 않는 코드 제거

* chore: MemberRepositoryTest 주석 제거

* refactor: 연관관계 편의 메소드 PetFood 안에서 처리

* refactor: Review 도메인 이름 복수형으로 변경

* refactor: reformatting code

* chore: AutoConfigureTestDatabase 제거

* chore: 사용하지 않는 테스트 제거

* chore: ddl auto 옵션 변경

* refactor: BrandFixture 메소드 네이밍 수정

* fix: 이상 반응 추가

* fix: 영양기준 필터링 수정

* fix: 이상반응 추가

* feat: 테스트 추가

* chore: 데이터 수정 및 리뷰 추가

* chore: 사용하지 않는 Import 제거
  • Loading branch information
wonyongChoi05 authored Aug 9, 2023
1 parent 02d7381 commit f6a302e
Show file tree
Hide file tree
Showing 67 changed files with 687 additions and 841 deletions.
4 changes: 4 additions & 0 deletions backend/http/get-filter-metadata.http
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
GET http://localhost:8080/pet-foods/filters

###

GET http://localhost:8080/pet-foods/filters/test
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package zipgo.auth.application;

import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand All @@ -19,6 +16,9 @@
import zipgo.auth.application.dto.OAuthMemberResponse;
import zipgo.auth.exception.AuthException;

import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;

@Component
@RequiredArgsConstructor
public class KakaoOAuthClient implements OAuthClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class KakaoMemberResponse implements OAuthMemberResponse {
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public static class KakaoAccount {

private Profile profile;
private String email;

}

@Getter
Expand All @@ -34,9 +36,11 @@ public static class KakaoAccount {
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Profile {

private String nickname;
@JsonProperty("thumbnail_image_url")
private String picture;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package zipgo.auth.presentation;

import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import jakarta.servlet.http.HttpServletRequest;
import zipgo.auth.exception.AuthException;

import static org.springframework.http.HttpHeaders.AUTHORIZATION;

public class BearerTokenExtractor {

private static final String BEARER_TYPE = "Bearer ";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zipgo.auth.presentation.dto;

public record AuthDto (
public record AuthDto(
Long id
) {

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

import zipgo.member.domain.Member;

public record TokenResponse (
public record TokenResponse(
String accessToken,
AuthResponse authResponse
) {
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main/java/zipgo/auth/util/JwtProvider.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package zipgo.auth.util;


import static io.jsonwebtoken.security.Keys.hmacShaKeyFor;
import static java.nio.charset.StandardCharsets.UTF_8;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jws;
Expand All @@ -17,6 +14,9 @@
import zipgo.auth.exception.AuthException;
import zipgo.common.config.JwtCredentials;

import static io.jsonwebtoken.security.Keys.hmacShaKeyFor;
import static java.nio.charset.StandardCharsets.UTF_8;

@Component
public class JwtProvider {

Expand Down
10 changes: 5 additions & 5 deletions backend/src/main/java/zipgo/brand/domain/Brand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package zipgo.brand.domain;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -17,8 +14,11 @@
import lombok.NoArgsConstructor;
import zipgo.petfood.domain.PetFood;

@Getter
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
Expand All @@ -30,7 +30,7 @@ public class Brand {
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(nullable = false)
@Column(unique = true, nullable = false)
private String name;

@Column(length = 2048)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package zipgo.common;

import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpHeaders;
Expand All @@ -20,6 +16,10 @@
import zipgo.petfood.presentation.dto.ErrorResponse;
import zipgo.review.exception.ReviewException;

import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@Log4j2
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
Expand Down
3 changes: 1 addition & 2 deletions backend/src/main/java/zipgo/common/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zipgo.common.config;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand All @@ -10,8 +11,6 @@
import zipgo.auth.presentation.JwtArgumentResolver;
import zipgo.auth.util.JwtProvider;

import java.util.List;

import static org.springframework.http.HttpHeaders.LOCATION;

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zipgo.member.exception;

public class MemberException extends RuntimeException {

public MemberException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,107 +1,37 @@
package zipgo.petfood.application;

import static java.util.Arrays.asList;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.petfood.domain.HasStandard;
import zipgo.petfood.domain.PetFood;
import zipgo.petfood.domain.repository.PetFoodQueryRepository;
import zipgo.petfood.domain.repository.PetFoodRepository;
import zipgo.petfood.presentation.dto.FilterResponse;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PetFoodQueryService {

private final BrandRepository brandRepository;
private final PetFoodRepository petFoodRepository;
private final PetFoodQueryRepository petFoodQueryRepository;

public List<PetFood> getPetFoods(
public List<PetFood> getPetFoodsByFilters(
List<String> brandsName,
List<String> nutritionStandards,
List<String> primaryIngredientList,
List<String> functionalityList
) {
return petFoodRepository.findAll().stream()
.filter(petFood -> isValidStandard(nutritionStandards, petFood.getHasStandard())
&& isContainBrandsName(brandsName, petFood.getBrand().getName())
&& isContainMainIngredients(petFood.getPrimaryIngredients(), primaryIngredientList)
&& isContainFunctionalities(petFood.getFunctionality(), functionalityList))
.collect(Collectors.toList());
}

private boolean isContainBrandsName(List<String> brandsName, String brandName) {
if (brandsName.isEmpty()) {
return true;
}
return brandsName.contains(brandName);
}

private boolean isValidStandard(List<String> nutrientStandards, HasStandard hasStandard) {
if (nutrientStandards.isEmpty()) {
return true;
}
boolean isValid = true;
for (String nutrientStandard : nutrientStandards) {
if (nutrientStandard.equals("유럽") && !hasStandard.getEurope()) {
isValid = false;
}
if (nutrientStandard.equals("미국") && !hasStandard.getUnitedStates()) {
isValid = false;
}
}
return isValid;
}

private boolean isContainMainIngredients(List<String> primaryIngredients, List<String> primaryIngredientList) {
if (primaryIngredientList.isEmpty()) {
return true;
}
return primaryIngredientList.stream()
.anyMatch(it -> primaryIngredients.contains(it));
}

private boolean isContainFunctionalities(List<String> functionalities, List<String> functionalityList) {
if (functionalityList.isEmpty()) {
return true;
}
return functionalities.stream()
.anyMatch(it -> functionalities.contains(it));
}

public List<PetFood> getPetFoodByDynamicValue(String keyword, String brand, String primaryIngredients) {
return petFoodQueryRepository.findPetFoods(keyword, brand, primaryIngredients);
return petFoodQueryRepository.findPetFoods(brandsName, nutritionStandards, primaryIngredientList,
functionalityList);
}

public PetFood getPetFoodBy(Long id) {
return petFoodRepository.getById(id);
}

public FilterResponse getMetadataForFilter() {
List<Brand> brands = brandRepository.findAll();

List<String> primaryIngredients = petFoodRepository.findAllPrimaryIngredients().stream()
.map(primaryIngredient -> asList(primaryIngredient.split(",")))
.flatMap(Collection::stream)
.distinct()
.toList();

List<String> functionalities = petFoodRepository.findAllFunctionalities().stream()
.map(functionality -> asList(functionality.split(",")))
.flatMap(Collection::stream)
.distinct()
.toList();

return FilterResponse.from(brands, primaryIngredients, functionalities);
}
// public FilterResponse getMetadataForFilter() {
// return null;
// }

}
37 changes: 25 additions & 12 deletions backend/src/main/java/zipgo/petfood/domain/Functionality.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package zipgo.petfood.domain;

import jakarta.persistence.Convert;
import jakarta.persistence.Embeddable;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@Builder
@Embeddable
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
public class Functionality {

@Default
@Convert(converter = StringArrayConverter.class)
private List<String> functionality = new ArrayList<>();
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pet_food_id")
private PetFood petFood;

public void changePetFood(PetFood petFood) {
this.petFood = petFood;
this.petFood.addFunctionality(this);
}

}
33 changes: 0 additions & 33 deletions backend/src/main/java/zipgo/petfood/domain/Keyword.java

This file was deleted.

Loading

0 comments on commit f6a302e

Please sign in to comment.