diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java new file mode 100644 index 0000000..bfbf3f7 --- /dev/null +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -0,0 +1,49 @@ +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.web.bind.annotation.GetMapping; + +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; + +@RestController +@RequestMapping("/recipes") +public class SpoonacularController { + private int nextRecepies=10; + private int nextRecepiesForOneRandom=20; + @Autowired + private ItemRepository itemRepository; + @Autowired + private SpoonacularApiService service; + + @GetMapping("/forFridge") + public HttpEntity getForFridge() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getForIngridients(itemRepository.findAll(), nextRecepies)); + } + + @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") + public HttpEntity getOneFridge() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); + } + + public void setNextRecepies(int nextRecepies) { + this.nextRecepies = nextRecepies; + } +} diff --git a/backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java b/backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java new file mode 100644 index 0000000..557f2cc --- /dev/null +++ b/backend/src/main/whattocook/implementation/SpoonacularApiServiceImpl.java @@ -0,0 +1,86 @@ +package whattocook.implementation; + + +import org.springframework.stereotype.Service; +import whattocook.models.Item; +import whattocook.services.SpoonacularApiService; +import org.json.*; + +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.Random; + +@Service +public class SpoonacularApiServiceImpl implements SpoonacularApiService { + private final String KEY = "85cc006d508b447a88e659cd748899db"; + private final String RANKING = "2"; + private final boolean IGNOREPANTRY = true; + Random rnd=new Random(); + + 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); + } 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://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()); + + JSONArray array=new JSONArray(response.body()); + return array; + + + } + } + + @Override + public JSONObject getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException, JSONException { + JSONArray array= getForIngridients(items, number); + + + return array.getJSONObject(rnd.nextInt(20)); + } + + 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()); + + JSONArray array=new JSONArray(response.body()); + return array; + + } 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://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()); + JSONArray array=new JSONArray(response.body()); + return array; + } + } + + + + +} diff --git a/backend/src/main/whattocook/services/SpoonacularApiService.java b/backend/src/main/whattocook/services/SpoonacularApiService.java new file mode 100644 index 0000000..8e18814 --- /dev/null +++ b/backend/src/main/whattocook/services/SpoonacularApiService.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 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; + + JSONArray getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException, JSONException; +}