Skip to content

Commit

Permalink
Merge pull request #6 from kim3ho1/feature/#5
Browse files Browse the repository at this point in the history
Refactor: RecipeResponse
  • Loading branch information
seheonnn authored Jan 11, 2024
2 parents 235dac2 + 882502d commit e15f4a6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class SecurityConfig {

private final String[] swaggerUrls = {"/swagger-ui/**", "/v3/**"};
private final String[] authUrls = {"/", "/login/kakao"};
private final String[] authUrls = {"/", "/login/kakao", "/recipe/**"};
private final String[] allowedUrls = Stream.concat(Arrays.stream(swaggerUrls), Arrays.stream(authUrls))
.toArray(String[]::new);

Expand Down Expand Up @@ -106,4 +106,4 @@ CorsConfigurationSource corsConfigurationSource() {
return config;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.kim3ho1.yourprotein.domain.Recipe;
import com.kim3ho1.yourprotein.dto.RecipeResponseDto;
import com.kim3ho1.yourprotein.service.RecipeService;

import lombok.RequiredArgsConstructor;
Expand All @@ -31,13 +32,13 @@ public String crawlingRecipe() {

// 단백질량 기반 레시피 추천
@GetMapping("/recommend")
public ResponseEntity<List<Recipe>> searchRecommendedRecipe(@Param("protein") String protein) {
public ResponseEntity<List<RecipeResponseDto.RecipeDetailResponseDto>> searchRecommendedRecipe(@Param("protein") double protein) {
return ResponseEntity.ok(recipeService.getRecommendedRecipe(protein));
}

// 레시피 검색
@GetMapping("/search")
public ResponseEntity<List<Recipe>> searchRecipes(@Param("keyword") String keyword) {
public ResponseEntity<List<RecipeResponseDto.RecipeDetailResponseDto>> searchRecipes(@Param("keyword") String keyword) {
return ResponseEntity.ok(recipeService.searchRecipes(keyword));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/kim3ho1/yourprotein/domain/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Recipe {
private String details; // 재료
private String kcal;
private String carbo;
private String protein;
private double protein;
private String fat;
private String na;
private String kind; // 요리 종류
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/kim3ho1/yourprotein/dto/RecipeResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.kim3ho1.yourprotein.dto;

import java.util.List;

import com.kim3ho1.yourprotein.domain.Recipe;

import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

public class RecipeResponseDto {
@Data
@AllArgsConstructor
@Builder
public static class RecipeDetailResponseDto {
private Long id;

private String recipeName;
private String details; // 재료
private String kcal;
private String carbo;
private double protein;
private String fat;
private String na;
private String kind; // 요리 종류
private List<String> images;

private List<String> manual;

}
public static RecipeDetailResponseDto from(Recipe recipe) {
return RecipeResponseDto.RecipeDetailResponseDto.builder()
.id(recipe.getId())
.recipeName(recipe.getRecipeName())
.details(recipe.getDetails())
.kcal(recipe.getKcal())
.carbo(recipe.getCarbo())
.protein(recipe.getProtein())
.fat(recipe.getFat())
.na(recipe.getNa())
.kind(recipe.getKind())
.images(List.of(recipe.getImages().split("\n")))
.manual(List.of(recipe.getManual().split("\n"))).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import com.kim3ho1.yourprotein.domain.Recipe;
@Repository
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
List<Recipe> searchAllByProtein(String protein);
@Query(value = "select * from recipe where protein <= :protein order by protein desc limit 5 ; ", nativeQuery = true)
List<Recipe> searchAllByProtein(@Param("protein") double protein);

@Query(value = "select * from recipe where recipe_name like %:keyword% ; ", nativeQuery = true)
List<Recipe> searchAllByRecipeName(@Param("keyword") String keyword);
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/kim3ho1/yourprotein/service/RecipeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kim3ho1.yourprotein.domain.Food;
import com.kim3ho1.yourprotein.domain.Recipe;
import com.kim3ho1.yourprotein.dto.RecipeResponseDto;
import com.kim3ho1.yourprotein.repository.RecipeRepository;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void crawlingRecipe() {
.details(recipe.get("RCP_PARTS_DTLS").asText())
.kcal(recipe.get("INFO_ENG").asText())
.carbo(recipe.get("INFO_CAR").asText())
.protein(recipe.get("INFO_PRO").asText())
.protein(recipe.get("INFO_PRO").asDouble())
.fat(recipe.get("INFO_FAT").asText())
.na(recipe.get("INFO_NA").asText())
.kind(recipe.get("RCP_PAT2").asText())
Expand All @@ -101,13 +103,28 @@ public void crawlingRecipe() {
}
}

public List<Recipe> getRecommendedRecipe(String protein) {
return recipeRepository.searchAllByProtein(protein);
public List<RecipeResponseDto.RecipeDetailResponseDto> getRecommendedRecipe(double protein) {
List<Recipe> recipes = recipeRepository.searchAllByProtein(protein);
List<RecipeResponseDto.RecipeDetailResponseDto> lists = new ArrayList<>();
recipes.stream().map(
recipe -> {
return lists.add(RecipeResponseDto.from(recipe));
}

).collect(Collectors.toList());
return lists;
}

public List<Recipe> searchRecipes(String keyword) {
public List<RecipeResponseDto.RecipeDetailResponseDto> searchRecipes(String keyword) {
List<Recipe> recipes = recipeRepository.searchAllByRecipeName(keyword);
return recipes;
List<RecipeResponseDto.RecipeDetailResponseDto> lists = new ArrayList<>();
recipes.stream().map(
recipe -> {
return lists.add(RecipeResponseDto.from(recipe));
}

).collect(Collectors.toList());
return lists;
}

public Recipe getRecipe(Long recipeId) {
Expand Down

0 comments on commit e15f4a6

Please sign in to comment.