Skip to content

Commit

Permalink
Merge pull request #99 from Recipe-Project/feature/add_ingredients_test
Browse files Browse the repository at this point in the history
Feature/add ingredients test
  • Loading branch information
joona95 authored Aug 8, 2024
2 parents f30dab1 + dee52bf commit f09e5f8
Show file tree
Hide file tree
Showing 22 changed files with 685 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.recipe.app.src.fridgeBasket.application.FridgeBasketService;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketCountResponse;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketIngredientIdsRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketIngredientRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketsResponse;
import com.recipe.app.src.user.domain.SecurityUser;
Expand All @@ -13,7 +12,14 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"냉장고 바구니 Controller"})
Expand Down Expand Up @@ -41,20 +47,6 @@ public void postFridgeBasketByIngredientId(@ApiIgnore final Authentication authe
fridgeBasketService.createFridgeBasketsByIngredientId(user, request);
}

@ApiOperation(value = "재료 직접 입력하여 냉장고 바구니 채우기 API")
@PostMapping("/direct")
public void postFridgeBasketWithIngredientSave(@ApiIgnore final Authentication authentication,
@ApiParam(value = "재료 직접 입력 정보", required = true)
@RequestBody FridgeBasketIngredientRequest request) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

fridgeBasketService.createFridgeBasketWithIngredientSave(user, request);
}

@ApiOperation(value = "냉장고 바구니 조회 API")
@GetMapping
public FridgeBasketsResponse getFridgeBaskets(@ApiIgnore final Authentication authentication) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.recipe.app.src.fridgeBasket.application;

import com.google.common.base.Preconditions;
import com.recipe.app.src.etc.application.BadWordService;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketCountResponse;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketIngredientIdsRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketIngredientRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketsResponse;
import com.recipe.app.src.fridgeBasket.domain.FridgeBasket;
Expand All @@ -17,7 +15,6 @@
import com.recipe.app.src.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -67,36 +64,6 @@ public void createFridgeBasketsByIngredientId(User user, FridgeBasketIngredientI
fridgeBasketRepository.saveAll(fridgeBaskets);
}

@Transactional
public void createFridgeBasketWithIngredientSave(User user, FridgeBasketIngredientRequest request) {

Preconditions.checkArgument(StringUtils.hasText(request.getIngredientName()), "재료명을 입력해주세요.");
Objects.requireNonNull(request.getIngredientCategoryId(), "재료 카테고리 아이디를 입력해주세요.");
badWordService.checkBadWords(request.getIngredientName());

IngredientCategory ingredientCategory = ingredientCategoryService.findById(request.getIngredientCategoryId());
Ingredient ingredient = ingredientService.findByUserIdAndIngredientNameAndIngredientIconIdAndIngredientCategoryId(
user.getUserId(),
request.getIngredientName(),
request.getIngredientIconId(),
ingredientCategory.getIngredientCategoryId())
.orElseGet(() -> Ingredient.builder()
.userId(user.getUserId())
.ingredientName(request.getIngredientName())
.ingredientIconId(request.getIngredientIconId())
.ingredientCategoryId(ingredientCategory.getIngredientCategoryId())
.build());
ingredientService.createIngredient(ingredient);

FridgeBasket fridgeBasket = fridgeBasketRepository.findByIngredientIdAndUserId(ingredient.getIngredientId(), user.getUserId())
.orElseGet(() -> FridgeBasket.builder()
.userId(user.getUserId())
.ingredientId(ingredient.getIngredientId())
.build());
fridgeBasket.plusQuantity(1);
fridgeBasketRepository.save(fridgeBasket);
}

@Transactional
public void deleteFridgeBasket(User user, Long fridgeBasketId) {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public IngredientsResponse getMyIngredients(@ApiIgnore final Authentication auth

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

return ingredientFacadeService.findMyIngredients(user);
return ingredientFacadeService.findIngredientsByUser(user);
}

@ApiOperation(value = "나만의 재료 등록 API")
Expand All @@ -73,7 +73,7 @@ public IngredientCreateResponse postIngredient(@ApiIgnore final Authentication a

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

return ingredientService.createIngredient(user.getUserId(), request);
return ingredientService.create(user.getUserId(), request);
}

@ApiOperation(value = "나만의 재료 삭제 API")
Expand All @@ -85,6 +85,6 @@ public void deleteIngredient(@ApiIgnore final Authentication authentication, @Pa

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

ingredientFacadeService.deleteMyIngredient(user, ingredientId);
ingredientFacadeService.deleteIngredient(user, ingredientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IngredientsResponse findIngredientsByKeyword(User user, String keyword) {
}

@Transactional(readOnly = true)
public IngredientsResponse findMyIngredients(User user) {
public IngredientsResponse findIngredientsByUser(User user) {

long fridgeBasketCount = fridgeBasketService.countByUserId(user.getUserId());
List<IngredientCategory> categories = ingredientCategoryService.findAll();
Expand All @@ -49,10 +49,10 @@ public IngredientsResponse findMyIngredients(User user) {
}

@Transactional
public void deleteMyIngredient(User user, Long ingredientId) {
public void deleteIngredient(User user, Long ingredientId) {

fridgeService.deleteByUserIdAndIngredientId(user.getUserId(), ingredientId);
fridgeBasketService.deleteByUserIdAndIngredientId(user.getUserId(), ingredientId);
ingredientService.deleteIngredient(user, ingredientId);
ingredientService.delete(user, ingredientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import com.recipe.app.src.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

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

@Service
public class IngredientService {
Expand All @@ -29,10 +27,6 @@ public IngredientService(IngredientRepository ingredientRepository, IngredientCa
@Transactional(readOnly = true)
public List<Ingredient> findByKeyword(Long userId, String keyword) {

if (!StringUtils.hasText(keyword)) {
return ingredientRepository.findDefaultIngredients(userId);
}

return ingredientRepository.findDefaultIngredientsByKeyword(userId, keyword);
}

Expand All @@ -42,45 +36,23 @@ public List<Ingredient> findByIngredientIds(Collection<Long> ingredientIds) {
return ingredientRepository.findByIngredientIdIn(ingredientIds);
}

@Transactional(readOnly = true)
public Optional<Ingredient> findByUserIdAndIngredientNameAndIngredientIconIdAndIngredientCategoryId(Long userId, String ingredientName, Long ingredientIconId, Long ingredientCategoryId) {

return ingredientRepository.findByUserIdAndIngredientNameAndIngredientIconIdAndIngredientCategoryId(userId, ingredientName, ingredientIconId, ingredientCategoryId);
}

@Transactional
public void createIngredient(Ingredient ingredient) {
ingredientRepository.save(ingredient);
}

@Transactional
public IngredientCreateResponse createIngredient(Long userId, IngredientRequest request) {
public IngredientCreateResponse create(Long userId, IngredientRequest request) {

IngredientCategory ingredientCategory = ingredientCategoryService.findById(request.getIngredientCategoryId());

Ingredient ingredient = ingredientRepository.findByUserIdAndIngredientNameAndIngredientIconIdAndIngredientCategoryId(
userId,
request.getIngredientName(),
request.getIngredientIconId(),
ingredientCategory.getIngredientCategoryId())
.orElseGet(() -> Ingredient.builder()
.userId(userId)
.ingredientName(request.getIngredientName())
.ingredientIconId(request.getIngredientIconId())
.ingredientCategoryId(ingredientCategory.getIngredientCategoryId())
.build());

ingredientRepository.save(ingredient);
.orElseGet(() -> ingredientRepository.save(request.toEntity(userId)));

return new IngredientCreateResponse(ingredient.getIngredientId());
}

@Transactional
public void createIngredients(Collection<Ingredient> ingredients) {
ingredientRepository.saveAll(ingredients);
}

@Transactional
public void deleteIngredientsByUserId(long userId) {
public void deleteAllByUserId(long userId) {

List<Ingredient> ingredients = ingredientRepository.findByUserId(userId);

Expand All @@ -96,13 +68,10 @@ public Ingredient findByIngredientId(Long ingredientId) {
}

@Transactional
public void deleteIngredient(User user, Long ingredientId) {

Ingredient ingredient = ingredientRepository.findByIngredientIdAndUserId(ingredientId, user.getUserId()).orElseThrow(() -> {
throw new NotFoundIngredientException();
});
public void delete(User user, Long ingredientId) {

ingredientRepository.delete(ingredient);
ingredientRepository.findByIngredientIdAndUserId(ingredientId, user.getUserId())
.ifPresent(ingredientRepository::delete);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.recipe.app.src.ingredient.application.dto;

import com.recipe.app.src.ingredient.domain.Ingredient;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -15,4 +17,21 @@ public class IngredientRequest {
private Long ingredientIconId;
@Schema(description = "재료 카테고리 고유 번호")
private Long ingredientCategoryId;

@Builder
public IngredientRequest(String ingredientName, Long ingredientIconId, Long ingredientCategoryId) {
this.ingredientName = ingredientName;
this.ingredientIconId = ingredientIconId;
this.ingredientCategoryId = ingredientCategoryId;
}

public Ingredient toEntity(Long userId) {

return Ingredient.builder()
.ingredientName(ingredientName)
.ingredientIconId(ingredientIconId)
.ingredientCategoryId(ingredientCategoryId)
.userId(userId)
.build();
}
}
42 changes: 0 additions & 42 deletions src/main/java/com/recipe/app/src/ingredient/domain/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -118,44 +116,4 @@ public List<String> getSimilarIngredientName() {
return List.of("포도씨유");
return new ArrayList<>();
}

public boolean existInIngredients(Collection<Ingredient> ingredients) {

List<String> ingredientNames = ingredients.stream()
.map(Ingredient::getIngredientName)
.collect(Collectors.toList());

return (ingredientName.equals("새우") && ingredientNames.contains("대하"))
|| (ingredientName.equals("대하") && ingredientNames.contains("새우"))
|| (ingredientName.equals("계란") && ingredientNames.contains("달걀"))
|| (ingredientName.equals("달걀") && ingredientNames.contains("계란"))
|| (ingredientName.equals("소고기") && ingredientNames.contains("쇠고기"))
|| (ingredientName.equals("쇠고기") && ingredientNames.contains("소고기"))
|| (ingredientName.equals("후추") && ingredientNames.contains("후춧가루"))
|| (ingredientName.equals("후춧가루") && ingredientNames.contains("후추"))
|| (ingredientName.equals("간마늘") && ingredientNames.contains("다진마늘"))
|| (ingredientName.equals("다진마늘") && ingredientNames.contains("간마늘"))
|| (ingredientName.equals("새싹채소") && ingredientNames.contains("어린잎채소"))
|| (ingredientName.equals("어린잎채소") && ingredientNames.contains("새싹채소"))
|| (ingredientName.equals("새싹채소") && ingredientNames.contains("무순"))
|| (ingredientName.equals("무순") && ingredientNames.contains("새싹채소"))
|| (ingredientName.equals("조개") && ingredientNames.contains("조갯살"))
|| (ingredientName.equals("조갯살") && ingredientNames.contains("조개"))
|| (ingredientName.equals("조개") && ingredientNames.contains("바지락"))
|| (ingredientName.equals("바지락") && ingredientNames.contains("조개"))
|| (ingredientName.equals("케찹") && ingredientNames.contains("케첩"))
|| (ingredientName.equals("케첩") && ingredientNames.contains("케찹"))
|| (ingredientName.equals("소면") && ingredientNames.contains("국수"))
|| (ingredientName.equals("국수") && ingredientNames.contains("소면"))
|| (ingredientName.equals("김치") && ingredientNames.contains("김칫잎"))
|| (ingredientName.equals("김칫잎") && ingredientNames.contains("김치"))
|| (ingredientName.equals("고춧가루") && ingredientNames.contains("고추가루"))
|| (ingredientName.equals("고추가루") && ingredientNames.contains("고춧가루"))
|| (ingredientName.equals("올리브유") && ingredientNames.contains("올리브오일"))
|| (ingredientName.equals("올리브오일") && ingredientNames.contains("올리브유"))
|| (ingredientName.equals("파스타") && ingredientNames.contains("스파게티"))
|| (ingredientName.equals("스파게티") && ingredientNames.contains("파스타"))
|| (ingredientName.equals("포도씨유") && ingredientNames.contains("식용유"))
|| (ingredientName.equals("식용유") && ingredientNames.contains("포도씨유"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
public interface IngredientCustomRepository {

List<Ingredient> findDefaultIngredientsByKeyword(Long userId, String keyword);

List<Ingredient> findDefaultIngredients(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.recipe.app.src.ingredient.infra;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.recipe.app.src.common.infra.BaseRepositoryImpl;
import com.recipe.app.src.ingredient.domain.Ingredient;
import jakarta.persistence.EntityManager;

import java.util.List;
import java.util.function.Function;

import static com.recipe.app.src.ingredient.domain.QIngredient.ingredient;

Expand All @@ -24,24 +26,13 @@ public List<Ingredient> findDefaultIngredientsByKeyword(Long userId, String keyw
.and(ingredient.userId.isNull().or(ingredient.userId.eq(userId))))
.or(ingredient.ingredientCategoryId.eq(7L)
.and(ingredient.userId.eq(userId))),
ingredient.ingredientName.contains(keyword)
ifNotNull(ingredient.ingredientName::contains, keyword)
)
.orderBy(ingredient.ingredientId.asc())
.fetch();
}

@Override
public List<Ingredient> findDefaultIngredients(Long userId) {

return queryFactory
.selectFrom(ingredient)
.where(
(ingredient.ingredientCategoryId.ne(7L)
.and(ingredient.userId.isNull().or(ingredient.userId.eq(userId))))
.or(ingredient.ingredientCategoryId.eq(7L)
.and(ingredient.userId.eq(userId)))
)
.orderBy(ingredient.ingredientId.asc())
.fetch();
private <T> BooleanExpression ifNotNull(Function<T, BooleanExpression> function, T object) {
return object != null ? function.apply(object) : null;
}
}
Loading

0 comments on commit f09e5f8

Please sign in to comment.