From 5e86c05ca8a0b71c2721bb150ff736e9e1ae2afb Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 13:05:00 +0200 Subject: [PATCH 1/7] basic api functuinality added, still needs debugging --- .../Controller/SpoonacularController.java | 26 +++++++ .../implementation/ApiServiceImpl.java | 74 +++++++++++++++++++ .../whattocook/repositories/ApiService.java | 9 +++ .../services/SpoonacularService.java | 5 ++ 4 files changed, 114 insertions(+) create mode 100644 backend/src/main/whattocook/Controller/SpoonacularController.java create mode 100644 backend/src/main/whattocook/implementation/ApiServiceImpl.java create mode 100644 backend/src/main/whattocook/repositories/ApiService.java create mode 100644 backend/src/main/whattocook/services/SpoonacularService.java 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(); +} From 1f8922788cec5e1de85ae0f4bfd69d915e4f7672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Bu=C3=9Fmann?= Date: Fri, 17 Jun 2022 15:22:50 +0200 Subject: [PATCH 2/7] updated api query address --- .../src/main/whattocook/implementation/ApiServiceImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/ApiServiceImpl.java index 05307d8..da2fb7b 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -46,7 +46,7 @@ public class ApiServiceImpl implements ApiService { 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)) + .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number)) .method("GET", HttpRequest.BodyPublishers.noBody()) .build(); HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); @@ -57,8 +57,7 @@ public class ApiServiceImpl implements ApiService { 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) + .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()); From 724e765cd65620f95853c5ddfb2117162cc0b5f5 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 16:23:04 +0200 Subject: [PATCH 3/7] basic api functuinality added, still needs debugging --- .../Controller/SpoonacularController.java | 15 +++++++++++++-- .../implementation/ApiServiceImpl.java | 19 ++++++++++++------- .../whattocook/repositories/ApiService.java | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) 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; } From 105aaf6badb573dd779cba406e736988bb47576c Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 17:15:24 +0200 Subject: [PATCH 4/7] 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; } From 8b472a6d612aa65513ff70c1eebbba8acad3f222 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 17:50:21 +0200 Subject: [PATCH 5/7] changed string to JSON objects/arrrays --- .../Controller/SpoonacularController.java | 18 +++--- .../implementation/ApiServiceImpl.java | 59 ++++++++----------- .../whattocook/repositories/ApiService.java | 10 ---- .../main/whattocook/services/ApiService.java | 13 ++++ .../services/SpoonacularService.java | 5 -- 5 files changed, 48 insertions(+), 57 deletions(-) delete mode 100644 backend/src/main/whattocook/repositories/ApiService.java create mode 100644 backend/src/main/whattocook/services/ApiService.java delete mode 100644 backend/src/main/whattocook/services/SpoonacularService.java diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index 90fd959..3a22222 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -1,14 +1,16 @@ package whattocook.Controller; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; 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.ApiService; +import whattocook.services.ApiService; import whattocook.repositories.ItemRepository; -import whattocook.implementation.ApiServiceImpl; import java.io.IOException; import java.util.LinkedList; @@ -23,19 +25,19 @@ public class SpoonacularController { private ApiService service; @GetMapping("api/forFridge") - public HttpEntity getForFridge() throws IOException, InterruptedException { - return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies)); + public HttpEntity getForFridge() throws IOException, InterruptedException, JSONException { + 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)); + public HttpEntity getRandom() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getRandom(new LinkedList<>(), nextRecepies)); //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 HttpEntity getOneFridge() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); } public void setNextRecepies(int nextRecepies) { diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/ApiServiceImpl.java index 39a28a2..3a3e24e 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -3,8 +3,8 @@ package whattocook.implementation; import org.springframework.stereotype.Service; import whattocook.models.Item; -import whattocook.repositories.ApiService; - +import whattocook.services.ApiService; +import org.json.*; import java.io.IOException; import java.net.http.HttpResponse; @@ -12,8 +12,6 @@ import java.net.http.HttpRequest; 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 @@ -23,7 +21,7 @@ public class ApiServiceImpl implements ApiService { private final boolean IGNOREPANTRY = true; Random rnd=new Random(); - public String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException { + public JSONArray getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException { Iterator itemIterator = items.iterator(); if (!itemIterator.hasNext()) { return getRandom(new java.util.LinkedList(), number); @@ -40,26 +38,33 @@ public class ApiServiceImpl implements ApiService { .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(); + + JSONArray array=new JSONArray(response.body()); + return array; + + } } @Override - public String getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException { - String recepies = getForIngridients(items, number); - List recepieList = splitToList(recepies); + public JSONObject getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException, JSONException { + JSONArray array= getForIngridients(items, number); - return recepieList.get(rnd.nextInt(20)); + + return array.getJSONObject(rnd.nextInt(20)); } - public String getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException { + public JSONArray getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException, JSONException { if (tags.isEmpty()) { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.spoonacular.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(); + + JSONArray array=new JSONArray(response.body()); + return array; + } else { String tagString = tags.get(0); for (int i = 1; i < tags.size(); i++) { @@ -70,32 +75,18 @@ public class ApiServiceImpl implements ApiService { .method("GET", HttpRequest.BodyPublishers.noBody()) .build(); HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); - return response.body(); + JSONArray array=new JSONArray(response.body()); + return array; } } - 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) { - startOfRecipie = i; - } - } else if (recipies.charAt(i) == '}') { - bracketCounter--; - if (bracketCounter == 0) { - recipieList.add(recipies.substring(startOfRecipie, i+1 )); - - } - } - - } - return recipieList; + public static void main(String[] args) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + "85cc006d508b447a88e659cd748899db" + "&number=" + 10)) + .method("GET", HttpRequest.BodyPublishers.noBody()) + .build(); + HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); } - } diff --git a/backend/src/main/whattocook/repositories/ApiService.java b/backend/src/main/whattocook/repositories/ApiService.java deleted file mode 100644 index 8188b67..0000000 --- a/backend/src/main/whattocook/repositories/ApiService.java +++ /dev/null @@ -1,10 +0,0 @@ -package whattocook.repositories; - -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; -} diff --git a/backend/src/main/whattocook/services/ApiService.java b/backend/src/main/whattocook/services/ApiService.java new file mode 100644 index 0000000..b8af462 --- /dev/null +++ b/backend/src/main/whattocook/services/ApiService.java @@ -0,0 +1,13 @@ +package whattocook.services; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import whattocook.models.Item; + +public interface ApiService { + JSONArray getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; + JSONObject getOneForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; + + JSONArray getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException, JSONException; +} diff --git a/backend/src/main/whattocook/services/SpoonacularService.java b/backend/src/main/whattocook/services/SpoonacularService.java deleted file mode 100644 index df989f2..0000000 --- a/backend/src/main/whattocook/services/SpoonacularService.java +++ /dev/null @@ -1,5 +0,0 @@ -package whattocook.services; - -public interface SpoonacularService { - String getByFridge(); -} From 93943149ac51f5254ed7041314927d755cb27d06 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 20:07:00 +0200 Subject: [PATCH 6/7] made endpoints better --- .../Controller/SpoonacularController.java | 17 ++++++++++------- ...Impl.java => SpoonacularApiServiceImpl.java} | 12 +++--------- ...iService.java => SpoonacularApiService.java} | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) rename backend/src/main/whattocook/implementation/{ApiServiceImpl.java => SpoonacularApiServiceImpl.java} (86%) rename backend/src/main/whattocook/services/{ApiService.java => SpoonacularApiService.java} (93%) diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index 3a22222..e91c4fb 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -6,36 +6,39 @@ import org.json.JSONException; import org.json.JSONObject; 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.services.ApiService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import whattocook.services.SpoonacularApiService; import whattocook.repositories.ItemRepository; import java.io.IOException; import java.util.LinkedList; -@Controller +@RestController +@RequestMapping("/recipes") public class SpoonacularController { private int nextRecepies=10; private int nextRecepiesForOneRandom=20; @Autowired private ItemRepository itemRepository; @Autowired - private ApiService service; + private SpoonacularApiService service; - @GetMapping("api/forFridge") + @GetMapping("forFridge") public HttpEntity getForFridge() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getForIngridients(itemRepository.findAll(), nextRecepies)); } - @GetMapping("api/random") + @GetMapping("random") public HttpEntity getRandom() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getRandom(new LinkedList<>(), nextRecepies)); //when user has food preferences apply instead of linked list. } - @GetMapping("api/oneFridge" ) + @GetMapping("oneFridge") public HttpEntity getOneFridge() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); } diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java similarity index 86% rename from backend/src/main/whattocook/implementation/ApiServiceImpl.java rename to backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java index 3a3e24e..557f2cc 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java @@ -3,7 +3,7 @@ package whattocook.implementation; import org.springframework.stereotype.Service; import whattocook.models.Item; -import whattocook.services.ApiService; +import whattocook.services.SpoonacularApiService; import org.json.*; import java.io.IOException; @@ -15,7 +15,7 @@ import java.util.Iterator; import java.util.Random; @Service -public class ApiServiceImpl implements ApiService { +public class SpoonacularApiServiceImpl implements SpoonacularApiService { private final String KEY = "85cc006d508b447a88e659cd748899db"; private final String RANKING = "2"; private final boolean IGNOREPANTRY = true; @@ -82,11 +82,5 @@ public class ApiServiceImpl implements ApiService { - public static void main(String[] args) throws IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + "85cc006d508b447a88e659cd748899db" + "&number=" + 10)) - .method("GET", HttpRequest.BodyPublishers.noBody()) - .build(); - HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); - } + } diff --git a/backend/src/main/whattocook/services/ApiService.java b/backend/src/main/whattocook/services/SpoonacularApiService.java similarity index 93% rename from backend/src/main/whattocook/services/ApiService.java rename to backend/src/main/whattocook/services/SpoonacularApiService.java index b8af462..8e18814 100644 --- a/backend/src/main/whattocook/services/ApiService.java +++ b/backend/src/main/whattocook/services/SpoonacularApiService.java @@ -5,7 +5,7 @@ import org.json.JSONException; import org.json.JSONObject; import whattocook.models.Item; -public interface ApiService { +public interface SpoonacularApiService { JSONArray getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; JSONObject getOneForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; From 22b84c356514dd7144121ed81711f97085eb79c8 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 20:08:24 +0200 Subject: [PATCH 7/7] made endpoints better --- .../main/whattocook/Controller/SpoonacularController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index e91c4fb..bfbf3f7 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -27,18 +27,18 @@ public class SpoonacularController { @Autowired private SpoonacularApiService service; - @GetMapping("forFridge") + @GetMapping("/forFridge") public HttpEntity getForFridge() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getForIngridients(itemRepository.findAll(), nextRecepies)); } - @GetMapping("random") + @GetMapping("/random") public HttpEntity getRandom() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getRandom(new LinkedList<>(), nextRecepies)); //when user has food preferences apply instead of linked list. } - @GetMapping("oneFridge") + @GetMapping("/oneFridge") public HttpEntity getOneFridge() throws IOException, InterruptedException, JSONException { return new HttpEntity(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); }