diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index 1496912..9e944e7 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -10,10 +10,11 @@ import whattocook.repositories.ItemRepository; import whattocook.implementation.ApiServiceImpl; import java.io.IOException; +import java.util.LinkedList; @Controller public class SpoonacularController { - private final int nextPages = 10; + private int nextRecepies; @Autowired private ItemRepository itemRepository; @Autowired @@ -21,6 +22,16 @@ public class SpoonacularController { @GetMapping("api/forFridge") public HttpEntity getForFridge() throws IOException, InterruptedException { - return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), 10)); + return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies)); + } + + @GetMapping("api/random") + public HttpEntity getRandom() throws IOException, InterruptedException { + return new HttpEntity<>(service.getRandom(new LinkedList<>(), nextRecepies)); + //when user has food preferences apply instead of linked list. + } + + public void setNextRecepies(int nextRecepies) { + this.nextRecepies = nextRecepies; } } diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/ApiServiceImpl.java index da2fb7b..8b32574 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -3,6 +3,7 @@ package whattocook.implementation; import org.springframework.stereotype.Service; import whattocook.models.Item; +import whattocook.models.Unit; import whattocook.repositories.ApiService; @@ -16,14 +17,14 @@ import java.util.LinkedList; @Service public class ApiServiceImpl implements ApiService { - private final String KEY = ""; + private final String KEY = "85cc006d508b447a88e659cd748899db"; 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()); + return getRandom( new java.util.LinkedList(), number); } else { String ingridients = itemIterator.next().getName(); for (Iterator it = itemIterator; it.hasNext(); ) { @@ -33,9 +34,7 @@ public class ApiServiceImpl implements ApiService { 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") + .uri(java.net.URI.create("https://api.spoonacular.com/recipes/findByIngredients?apiKey="+KEY+"&ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number)) .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()); @@ -43,7 +42,7 @@ public class ApiServiceImpl implements ApiService { } } - public String getRandom(int number, java.util.List tags) throws java.io.IOException, InterruptedException { + public String getRandom( java.util.List tags, int number) throws java.io.IOException, InterruptedException { if (tags.isEmpty()) { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number)) @@ -67,7 +66,13 @@ public class ApiServiceImpl implements ApiService { public static void main(String[] args) throws IOException, InterruptedException { ApiServiceImpl impl = new ApiServiceImpl(); - System.out.println(impl.getRandom(10, new LinkedList<>())); + LinkedList tags= new LinkedList<>(); + tags.add(new Item("tortelini", Unit.GRAMMS,75)); + tags.add(new Item("garlic", Unit.GRAMMS,75)); + tags.add(new Item("eggplant", Unit.GRAMMS,75)); + tags.add(new Item("zuccini", Unit.GRAMMS,75)); + + System.out.println(impl.getForIngridients(tags,10 )); } } diff --git a/backend/src/main/whattocook/repositories/ApiService.java b/backend/src/main/whattocook/repositories/ApiService.java index 7d989f6..11c9739 100644 --- a/backend/src/main/whattocook/repositories/ApiService.java +++ b/backend/src/main/whattocook/repositories/ApiService.java @@ -5,5 +5,5 @@ 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; + String getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException; }