diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java new file mode 100644 index 0000000..1496912 --- /dev/null +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -0,0 +1,26 @@ +package whattocook.Controller; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +import whattocook.repositories.ItemRepository; +import whattocook.implementation.ApiServiceImpl; + +import java.io.IOException; + +@Controller +public class SpoonacularController { + private final int nextPages = 10; + @Autowired + private ItemRepository itemRepository; + @Autowired + private ApiServiceImpl service; + + @GetMapping("api/forFridge") + public HttpEntity getForFridge() throws IOException, InterruptedException { + return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), 10)); + } +} diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/ApiServiceImpl.java new file mode 100644 index 0000000..05307d8 --- /dev/null +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -0,0 +1,74 @@ +package whattocook.implementation; + + +import org.springframework.stereotype.Service; +import whattocook.models.Item; +import whattocook.repositories.ApiService; + + +import java.io.IOException; +import java.net.http.HttpResponse; +import java.net.http.HttpRequest; +import java.net.http.HttpClient; +import java.net.URI; +import java.util.Iterator; +import java.util.LinkedList; + +@Service +public class ApiServiceImpl implements ApiService { + private final String KEY = ""; + private final String RANKING = "2"; + private final boolean IGNOREPANTRY = true; + + public String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException { + Iterator itemIterator = items.iterator(); + if (!itemIterator.hasNext()) { + return getRandom(number, new java.util.LinkedList()); + } else { + String ingridients = itemIterator.next().getName(); + for (Iterator it = itemIterator; it.hasNext(); ) { + Item curryItem = it.next(); + + + ingridients += "," + curryItem.getName(); + } + java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder() + .uri(java.net.URI.create("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number)) + .header("X-RapidAPI-Key", KEY) + .header("X-RapidAPI-Host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com") + .method("GET", java.net.http.HttpRequest.BodyPublishers.noBody()) + .build(); + java.net.http.HttpResponse response = java.net.http.HttpClient.newHttpClient().send(request, java.net.http.HttpResponse.BodyHandlers.ofString()); + return response.body(); + } + } + + public String getRandom(int number, java.util.List tags) throws java.io.IOException, InterruptedException { + if (tags.isEmpty()) { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/random?apiKey=" + KEY + "?number=" + number)) + .method("GET", HttpRequest.BodyPublishers.noBody()) + .build(); + HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + return response.body(); + } else { + String tagString = tags.get(0); + for (int i = 1; i < tags.size(); i++) { + tagString += "," + tags.get(i); + } + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/random?number=" + number + "&tags=" + tagString)) + .header("x-api-key", KEY) + .method("GET", HttpRequest.BodyPublishers.noBody()) + .build(); + HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + return response.body(); + } + } + + public static void main(String[] args) throws IOException, InterruptedException { + ApiServiceImpl impl = new ApiServiceImpl(); + System.out.println(impl.getRandom(10, new LinkedList<>())); + } + +} diff --git a/backend/src/main/whattocook/repositories/ApiService.java b/backend/src/main/whattocook/repositories/ApiService.java new file mode 100644 index 0000000..7d989f6 --- /dev/null +++ b/backend/src/main/whattocook/repositories/ApiService.java @@ -0,0 +1,9 @@ +package whattocook.repositories; + +import whattocook.models.Item; + +public interface ApiService { + String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException; + + String getRandom(int number, java.util.List tags) throws java.io.IOException, InterruptedException; +} diff --git a/backend/src/main/whattocook/services/SpoonacularService.java b/backend/src/main/whattocook/services/SpoonacularService.java new file mode 100644 index 0000000..df989f2 --- /dev/null +++ b/backend/src/main/whattocook/services/SpoonacularService.java @@ -0,0 +1,5 @@ +package whattocook.services; + +public interface SpoonacularService { + String getByFridge(); +}