From 105aaf6badb573dd779cba406e736988bb47576c Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 17:15:24 +0200 Subject: [PATCH] debugged, added OneFridge Recipie --- .../Controller/SpoonacularController.java | 11 ++++- .../implementation/ApiServiceImpl.java | 49 ++++++++++++++----- .../whattocook/repositories/ApiService.java | 1 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index 9e944e7..90fd959 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -6,6 +6,7 @@ import org.springframework.http.HttpEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import whattocook.repositories.ApiService; import whattocook.repositories.ItemRepository; import whattocook.implementation.ApiServiceImpl; @@ -14,11 +15,12 @@ import java.util.LinkedList; @Controller public class SpoonacularController { - private int nextRecepies; + private int nextRecepies=10; + private int nextRecepiesForOneRandom=20; @Autowired private ItemRepository itemRepository; @Autowired - private ApiServiceImpl service; + private ApiService service; @GetMapping("api/forFridge") public HttpEntity getForFridge() throws IOException, InterruptedException { @@ -31,6 +33,11 @@ public class SpoonacularController { //when user has food preferences apply instead of linked list. } + @GetMapping("api/oneFridge" ) + public HttpEntity getOneFridge() throws IOException, InterruptedException { + return new HttpEntity<>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); + } + 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 8b32574..39a28a2 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -3,7 +3,6 @@ package whattocook.implementation; import org.springframework.stereotype.Service; import whattocook.models.Item; -import whattocook.models.Unit; import whattocook.repositories.ApiService; @@ -14,17 +13,20 @@ import java.net.http.HttpClient; import java.net.URI; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; +import java.util.Random; @Service public class ApiServiceImpl implements ApiService { private final String KEY = "85cc006d508b447a88e659cd748899db"; private final String RANKING = "2"; private final boolean IGNOREPANTRY = true; + Random rnd=new Random(); public String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException { Iterator itemIterator = items.iterator(); if (!itemIterator.hasNext()) { - return getRandom( new java.util.LinkedList(), number); + return getRandom(new java.util.LinkedList(), number); } else { String ingridients = itemIterator.next().getName(); for (Iterator it = itemIterator; it.hasNext(); ) { @@ -34,7 +36,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://api.spoonacular.com/recipes/findByIngredients?apiKey="+KEY+"&ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number)) + .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()); @@ -42,7 +44,15 @@ public class ApiServiceImpl implements ApiService { } } - public String getRandom( java.util.List tags, int number) throws java.io.IOException, InterruptedException { + @Override + public String getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException { + String recepies = getForIngridients(items, number); + List recepieList = splitToList(recepies); + + return recepieList.get(rnd.nextInt(20)); + } + + 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)) @@ -56,7 +66,7 @@ public class ApiServiceImpl implements ApiService { tagString += "," + tags.get(i); } HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number+ "&tags=" + tagString)) + .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number + "&tags=" + tagString)) .method("GET", HttpRequest.BodyPublishers.noBody()) .build(); HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); @@ -64,15 +74,28 @@ public class ApiServiceImpl implements ApiService { } } - public static void main(String[] args) throws IOException, InterruptedException { - ApiServiceImpl impl = new ApiServiceImpl(); - 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)); + private List splitToList(String recipies) { + int startOfRecipie = 1; + int bracketCounter = 0; + List recipieList = new LinkedList<>(); + for (int i = 0; i < recipies.length(); i++) { + if (recipies.charAt(i) == '{') { + bracketCounter++; + if (bracketCounter == 1) { - System.out.println(impl.getForIngridients(tags,10 )); + startOfRecipie = i; + } + } else if (recipies.charAt(i) == '}') { + bracketCounter--; + if (bracketCounter == 0) { + + recipieList.add(recipies.substring(startOfRecipie, i+1 )); + + } + } + + } + return recipieList; } } diff --git a/backend/src/main/whattocook/repositories/ApiService.java b/backend/src/main/whattocook/repositories/ApiService.java index 11c9739..8188b67 100644 --- a/backend/src/main/whattocook/repositories/ApiService.java +++ b/backend/src/main/whattocook/repositories/ApiService.java @@ -4,6 +4,7 @@ import whattocook.models.Item; public interface ApiService { String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException; + String getOneForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException; String getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException; }