basic api functuinality added, still needs debugging
This commit is contained in:
@@ -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<String> 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<String> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Item> items, int number) throws java.io.IOException, InterruptedException {
|
||||
Iterator<Item> itemIterator = items.iterator();
|
||||
if (!itemIterator.hasNext()) {
|
||||
return getRandom(number, new java.util.LinkedList<String>());
|
||||
return getRandom( new java.util.LinkedList<String>(), number);
|
||||
} else {
|
||||
String ingridients = itemIterator.next().getName();
|
||||
for (Iterator<Item> 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<String> 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<String> tags) throws java.io.IOException, InterruptedException {
|
||||
public String getRandom( java.util.List<String> 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<Item > 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 ));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,5 +5,5 @@ import whattocook.models.Item;
|
||||
public interface ApiService {
|
||||
String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
|
||||
|
||||
String getRandom(int number, java.util.List<String> tags) throws java.io.IOException, InterruptedException;
|
||||
String getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user