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,12 +13,15 @@ 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();
|
||||
@@ -42,6 +44,14 @@ public class ApiServiceImpl implements ApiService {
|
||||
}
|
||||
}
|
||||
|
||||
@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()
|
||||
@@ -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