Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.webservice.algorithmchef.client;

import com.webservice.algorithmchef.dto.recipe.CookRcpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Service
public class FoodSafetyApiClient {

private final RestTemplate restTemplate;

public FoodSafetyApiClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Value("${foodsafety.cookrcp.base-url}")
private String baseUrl;

@Value("${foodsafety.cookrcp.service-key}")
private String serviceKey;

@Value("${foodsafety.cookrcp.svc-no}")
private String svcNo;

public int getTotalCount() {
String url = String.format("%s/%s/%s/json/1/1",
baseUrl, serviceKey, svcNo);

CookRcpResponse res = restTemplate.getForObject(url, CookRcpResponse.class);

if (res == null || res.getCOOKRCP01() == null) return 0;

try {
return Integer.parseInt(res.getCOOKRCP01().getTotalCount());
} catch (Exception e) {
return 0;
}
}

public List<CookRcpResponse.Item> fetchRange(int start, int end) {
String url = String.format("%s/%s/%s/json/%d/%d",
baseUrl, serviceKey, svcNo, start, end);

CookRcpResponse res = restTemplate.getForObject(url, CookRcpResponse.class);

return Optional.ofNullable(res)
.map(CookRcpResponse::getCOOKRCP01)
.map(CookRcpResponse.Body::getRow)
.orElse(List.of());
}

public List<CookRcpResponse.Item> fetchAll() {
int total = getTotalCount();
if (total <= 0) return List.of();

int pageSize = 200;
List<CookRcpResponse.Item> all = new ArrayList<>(total);

for (int start = 1; start <= total; start += pageSize) {
int end = Math.min(start + pageSize - 1, total);
all.addAll(fetchRange(start, end));
}

return all;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.webservice.algorithmchef.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.webservice.algorithmchef.controller;

import com.webservice.algorithmchef.service.RecipeImportService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class RecipeImportController {

private final RecipeImportService recipeImportService;

public RecipeImportController(RecipeImportService recipeImportService) {
this.recipeImportService = recipeImportService;
}

@GetMapping("/import/foodsafety")
public String importFromFoodSafety() {
recipeImportService.importAllFromFoodSafety();
return "import started";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.webservice.algorithmchef.dto.recipe;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CookRcpResponse {

@JsonProperty("COOKRCP01")
private Body cookrcp01;

public Body getCOOKRCP01() {
return cookrcp01;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Body {

@JsonProperty("total_count")
private String totalCount;

@JsonProperty("row")
private List<Item> row;

public String getTotalCount() {
return totalCount;
}

public List<Item> getRow() {
// NPE 방지용
return row == null ? List.of() : row;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Item {

@JsonProperty("RCP_NM")
private String name;

@JsonProperty("RCP_SEQ")
private String recipeIdRaw;


@JsonProperty("RCP_PARTS_DTLS")
private String parts;

@JsonProperty("ATT_FILE_NO_MAIN")
private String imageUrl;

@JsonProperty("INFO_ENG")
private String infoEng;

@JsonProperty("HASH_TAG")
private String hashTag;

@JsonProperty("RCP_WAY2")
private String way;

@JsonProperty("RCP_PAT2")
private String category;

@JsonProperty("MANUAL01") private String manual01;
@JsonProperty("MANUAL02") private String manual02;
@JsonProperty("MANUAL03") private String manual03;
@JsonProperty("MANUAL04") private String manual04;
@JsonProperty("MANUAL05") private String manual05;
@JsonProperty("MANUAL06") private String manual06;
@JsonProperty("MANUAL07") private String manual07;
@JsonProperty("MANUAL08") private String manual08;
@JsonProperty("MANUAL09") private String manual09;
@JsonProperty("MANUAL10") private String manual10;
@JsonProperty("MANUAL11") private String manual11;
@JsonProperty("MANUAL12") private String manual12;
@JsonProperty("MANUAL13") private String manual13;
@JsonProperty("MANUAL14") private String manual14;
@JsonProperty("MANUAL15") private String manual15;
@JsonProperty("MANUAL16") private String manual16;
@JsonProperty("MANUAL17") private String manual17;
@JsonProperty("MANUAL18") private String manual18;
@JsonProperty("MANUAL19") private String manual19;
@JsonProperty("MANUAL20") private String manual20;


public String getName() {
return name;
}

public String getParts() {
return parts;
}

public String getDescription() {
return parts;
}

public String getImageUrl() {
return imageUrl;
}

public String getInfoEng() {
return infoEng;
}

public Double getKcal() {
if (infoEng == null) return null;
try {
String n = infoEng.replaceAll("[^0-9.]", "");
return n.isEmpty() ? null : Double.valueOf(n);
} catch (Exception e) {
return null;
}
}

public Double getKcalOrNull() {
return getKcal();
}

public String getHashTag() {
return hashTag;
}

public String getWay() {
return way;
}

public String getCategory() {
return category;
}

public String getType() {
if (category != null && !category.isBlank()) return category;
if (way != null && !way.isBlank()) return way;
return "기타";
}

public String getInstructions() {
List<String> steps = new ArrayList<>();

for (String s : List.of(
manual01, manual02, manual03, manual04, manual05,
manual06, manual07, manual08, manual09, manual10,
manual11, manual12, manual13, manual14, manual15,
manual16, manual17, manual18, manual19, manual20
)) {
if (s == null) continue;

String t = s
.replace("\r\n", "\n")
.replace("\\n", "\n")
.replaceAll("[ \t]+", " ")
.trim();

if (!t.isEmpty()) {
steps.add(t);
}
}

return String.join("\n", steps);
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/webservice/algorithmchef/model/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ public class Recipe {

@Lob
private String description;

@Lob
@Column(nullable = false)
private String instructions;

private String imageUrl;

@Lob
@Column(nullable = false, columnDefinition = "LONGTEXT")
private String instructions;

@Lob
@Column(columnDefinition = "TEXT")
private String neededIngredients;

private String imageUrl;

@Column(nullable = false)
private String type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.webservice.algorithmchef.repository;

import com.webservice.algorithmchef.model.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface RecipeRepository extends JpaRepository<Recipe, Long> {

Optional<Recipe> findByName(String name);

boolean existsByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.webservice.algorithmchef.service;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component("recipeDataInitializer")
public class RecipeDataInitializer implements CommandLineRunner {

private final RecipeImportService importService;

public RecipeDataInitializer(RecipeImportService importService) {
this.importService = importService;
}

@Override
public void run(String... args) throws Exception {
System.out.println("[INIT] import start");
importService.importAllFromFoodSafety();
System.out.println("[INIT] import complete");
}
}
Loading