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

[ #60

Closed
wants to merge 4 commits into from
Closed

[ #60

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
1 change: 0 additions & 1 deletion Server/banchango/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ COPY ${JAR_FILE} /banchango.jar
#μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰ λͺ…λ Ήμ–΄
#ex) java -jar -Dspring.profiles.active=prod /sejongmate.jar
ENTRYPOINT ["java","-jar","/banchango.jar"]
#
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public OpenAPI openAPI() {
Server server = new Server();
server.setUrl("https://backendu.com");

Server localServer = new Server();
localServer.setUrl("http://localhost:8080");

return new OpenAPI()
.info(info)
.servers(List.of(server));
.servers(List.of(server, localServer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,84 @@
import com.sundaegukbap.banchango.container.repository.ContainerRepository;
import com.sundaegukbap.banchango.ingredient.domain.ContainerIngredient;
import com.sundaegukbap.banchango.ingredient.domain.Ingredient;
import com.sundaegukbap.banchango.ingredient.domain.RecipeMainIngredient;
import com.sundaegukbap.banchango.ingredient.domain.RecipeRequiringIngredient;
import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos;
import com.sundaegukbap.banchango.ingredient.repository.ContainerIngredientRepository;
import com.sundaegukbap.banchango.ingredient.repository.RecipeMainIngredientRepository;
import com.sundaegukbap.banchango.ingredient.repository.RecipeRequiringIngredientRepository;
import com.sundaegukbap.banchango.recipe.domain.Recipe;
import com.sundaegukbap.banchango.user.domain.User;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class IngredientMatcher {

private ContainerRepository containerRepository;
private ContainerIngredientRepository containerIngredientRepository;
private RecipeRequiringIngredientRepository recipeRequiringIngredientRepository;
private RecipeMainIngredientRepository recipeMainIngredientRepository;

public HashMap<String,List> checkIngredientRelation(User user, Recipe recipe){
public HashMap<String, List> checkIngredientRelation(User user, Recipe recipe) {
List<Ingredient> havingIngredients = getAllIngredientsWithUser(user);
List<Ingredient> requiringIngredients = getIngredientsWithRecipe(recipe);

List<Ingredient> need = new ArrayList<>();
List<Ingredient> have = new ArrayList<>();

for(Ingredient ri : requiringIngredients){
for (Ingredient ri : requiringIngredients) {
boolean has = havingIngredients.contains(ri);
if(has)
if (has) {
have.add(ri);
else
} else {
need.add(ri);
}
}

HashMap<String,List> result = new HashMap<>();
HashMap<String, List> result = new HashMap<>();
result.put("need", need);
result.put("have", have);

return result;
}

private List<Ingredient> getAllIngredientsWithUser(User user){
private List<Ingredient> getAllIngredientsWithUser(User user) {
List<Container> containers = containerRepository.findAllByUser(user);
List<ContainerIngredient> containerIngredientList = containerIngredientRepository.findByContainerIn(containers);
List<ContainerIngredient> containerIngredientList = containerIngredientRepository.findByContainerIn(
containers);
List<Ingredient> ingredients = containerIngredientList.stream()
.map(ContainerIngredient::getIngredient)
.collect(Collectors.toList());
.map(ContainerIngredient::getIngredient)
.collect(Collectors.toList());

return ingredients;
}

private List<Ingredient> getIngredientsWithRecipe(Recipe recipe) {
List<RecipeRequiringIngredient> recipeRequiringIngredientList = recipeRequiringIngredientRepository.findAllByRecipe(recipe);
List<RecipeRequiringIngredient> recipeRequiringIngredientList = recipeRequiringIngredientRepository.findAllByRecipe(
recipe);

return recipeRequiringIngredientList.stream()
.map(i -> i.getIngredient())
.collect(Collectors.toList());
.map(i -> i.getIngredient())
.collect(Collectors.toList());
}

public void resolveMainIngredient(Recipe recipe, IngredientDtos ingredients) {
List<RecipeMainIngredient> mainIngredients =
recipeMainIngredientRepository.findByRecipe(recipe);

ingredients.ingredientDtos().forEach(ingredientDto -> {
Long target = ingredientDto.getId();
mainIngredients.forEach(mainIngredient -> {
if (mainIngredient.getIngredient().getId().equals(target)) {
ingredientDto.setIsMain(true);
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.sundaegukbap.banchango.ingredient.dto.dto;

import com.sundaegukbap.banchango.container.domain.Container;
import com.sundaegukbap.banchango.container.dto.dto.ContainerDto;
import com.sundaegukbap.banchango.ingredient.domain.ContainerIngredient;
import com.sundaegukbap.banchango.ingredient.domain.Ingredient;

import java.time.LocalDateTime;

public record ContainerIngredientDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.sundaegukbap.banchango.ingredient.dto.dto;

import com.sundaegukbap.banchango.ingredient.domain.Ingredient;
import lombok.AllArgsConstructor;
import lombok.Getter;

public record IngredientDto(
Long id,
String name,
String kind,
String image
) {
public static IngredientDto of(Ingredient ingredient){
@AllArgsConstructor
@Getter
public class IngredientDto {

private Long id;
private String name;
private String kind;
private String image;
private boolean isMain;

public static IngredientDto of(Ingredient ingredient) {
return new IngredientDto(
ingredient.getId(),
ingredient.getName(),
ingredient.getKind(),
ingredient.getImage()
ingredient.getId(),
ingredient.getName(),
ingredient.getKind(),
ingredient.getImage(),
false
);
}

public void setIsMain(boolean isMain) {
this.isMain = isMain;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.sundaegukbap.banchango.ingredient.dto.dto;

import com.sundaegukbap.banchango.ingredient.domain.Ingredient;

import java.util.List;
import java.util.stream.Collectors;

public record IngredientDtos(
List<IngredientDto> ingredientDtos
) {
public static IngredientDtos of(List<Ingredient> ingredientList){
return new IngredientDtos(ingredientList.stream()
.map(IngredientDto::of)
.collect(Collectors.toList()));
public static IngredientDtos of(List<IngredientDto> ingredientDtos){
return new IngredientDtos(ingredientDtos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sundaegukbap.banchango.ingredient.repository;

import com.sundaegukbap.banchango.ingredient.domain.RecipeMainIngredient;
import com.sundaegukbap.banchango.recipe.domain.Recipe;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RecipeMainIngredientRepository extends JpaRepository<RecipeMainIngredient, Long> {

List<RecipeMainIngredient> findByRecipe(Recipe recipe);

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

import com.sundaegukbap.banchango.ingredient.application.IngredientMatcher;
import com.sundaegukbap.banchango.ingredient.domain.Ingredient;
import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDto;
import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos;
import com.sundaegukbap.banchango.recipe.domain.Recipe;
import com.sundaegukbap.banchango.recipe.domain.UserRecommendedRecipe;
import com.sundaegukbap.banchango.recipe.dto.response.RecommendedRecipeResponse;
Expand Down Expand Up @@ -69,6 +71,17 @@ public RecommendedRecipeResponse resolveRecipeWithUser(User user, Recipe recipe)
List<Ingredient> have = ingredientRelation.get("have");
List<Ingredient> need = ingredientRelation.get("need");

return RecommendedRecipeResponse.of(recipe, have, need);
IngredientDtos haveDtos = IngredientDtos.of(have.stream()
.map(IngredientDto::of)
.toList());

IngredientDtos needDtos = IngredientDtos.of(need.stream()
.map(IngredientDto::of)
.toList());

ingredientMatcher.resolveMainIngredient(recipe, haveDtos);
ingredientMatcher.resolveMainIngredient(recipe, needDtos);

return RecommendedRecipeResponse.of(recipe, haveDtos, needDtos);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.sundaegukbap.banchango.recipe.dto.response;

import com.sundaegukbap.banchango.ingredient.domain.Ingredient;
import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos;
import com.sundaegukbap.banchango.recipe.domain.Recipe;
import com.sundaegukbap.banchango.recipe.dto.dto.RecipeDto;

import java.util.List;

public record RecommendedRecipeResponse(
RecipeDto recipe,
IngredientDtos have,
RecipeDto recipe,
IngredientDtos have,
IngredientDtos need) {

public static RecommendedRecipeResponse of(Recipe recipe, IngredientDtos have,
IngredientDtos need) {
public static RecommendedRecipeResponse of(Recipe recipe, List<Ingredient> have, List<Ingredient> need){
return new RecommendedRecipeResponse(
RecipeDto.of(recipe),
IngredientDtos.of(have),
IngredientDtos.of(need)
RecipeDto.of(recipe),
have,
need
);
}
}
Loading