debugged, added OneFridge Recipie
This commit is contained in:
@@ -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<String> 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<String> getOneFridge() throws IOException, InterruptedException {
|
||||
return new HttpEntity<>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom));
|
||||
}
|
||||
|
||||
public void setNextRecepies(int nextRecepies) {
|
||||
this.nextRecepies = nextRecepies;
|
||||
}
|
||||
|
||||
@@ -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<Item> items, int number) throws java.io.IOException, InterruptedException {
|
||||
Iterator<Item> itemIterator = items.iterator();
|
||||
if (!itemIterator.hasNext()) {
|
||||
return getRandom( new java.util.LinkedList<String>(), number);
|
||||
return getRandom(new java.util.LinkedList<String>(), number);
|
||||
} else {
|
||||
String ingridients = itemIterator.next().getName();
|
||||
for (Iterator<Item> 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<String> 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<String> tags, int number) throws java.io.IOException, InterruptedException {
|
||||
@Override
|
||||
public String getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException {
|
||||
String recepies = getForIngridients(items, number);
|
||||
List<String> recepieList = splitToList(recepies);
|
||||
|
||||
return recepieList.get(rnd.nextInt(20));
|
||||
}
|
||||
|
||||
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))
|
||||
@@ -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<String> 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<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));
|
||||
private List<String> splitToList(String recipies) {
|
||||
int startOfRecipie = 1;
|
||||
int bracketCounter = 0;
|
||||
List<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import whattocook.models.Item;
|
||||
|
||||
public interface ApiService {
|
||||
String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
|
||||
String getOneForIngridients(Iterable<Item> items, int number) 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