changed string to JSON objects/arrrays
This commit is contained in:
@@ -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<String> getForFridge() throws IOException, InterruptedException {
|
||||
return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies));
|
||||
public HttpEntity<JSONArray> getForFridge() throws IOException, InterruptedException, JSONException {
|
||||
return new HttpEntity<JSONArray>(service.getForIngridients(itemRepository.findAll(), nextRecepies));
|
||||
}
|
||||
|
||||
@GetMapping("api/random")
|
||||
public HttpEntity<String> getRandom() throws IOException, InterruptedException {
|
||||
return new HttpEntity<>(service.getRandom(new LinkedList<>(), nextRecepies));
|
||||
public HttpEntity<JSONArray> getRandom() throws IOException, InterruptedException, JSONException {
|
||||
return new HttpEntity<JSONArray>(service.getRandom(new LinkedList<>(), nextRecepies));
|
||||
//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 HttpEntity<JSONObject> getOneFridge() throws IOException, InterruptedException, JSONException {
|
||||
return new HttpEntity<JSONObject>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom));
|
||||
}
|
||||
|
||||
public void setNextRecepies(int nextRecepies) {
|
||||
|
||||
@@ -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<Item> items, int number) throws java.io.IOException, InterruptedException {
|
||||
public JSONArray getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException {
|
||||
Iterator<Item> itemIterator = items.iterator();
|
||||
if (!itemIterator.hasNext()) {
|
||||
return getRandom(new java.util.LinkedList<String>(), number);
|
||||
@@ -40,26 +38,33 @@ public class ApiServiceImpl implements ApiService {
|
||||
.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());
|
||||
return response.body();
|
||||
|
||||
JSONArray array=new JSONArray(response.body());
|
||||
return array;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException {
|
||||
String recepies = getForIngridients(items, number);
|
||||
List<String> recepieList = splitToList(recepies);
|
||||
public JSONObject getOneForIngridients(Iterable<Item> 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<String> tags, int number) throws java.io.IOException, InterruptedException {
|
||||
public JSONArray getRandom(java.util.List<String> 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<String> 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<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return response.body();
|
||||
JSONArray array=new JSONArray(response.body());
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package whattocook.repositories;
|
||||
|
||||
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;
|
||||
}
|
||||
13
backend/src/main/whattocook/services/ApiService.java
Normal file
13
backend/src/main/whattocook/services/ApiService.java
Normal file
@@ -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<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException;
|
||||
JSONObject getOneForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException;
|
||||
|
||||
JSONArray getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException, JSONException;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package whattocook.services;
|
||||
|
||||
public interface SpoonacularService {
|
||||
String getByFridge();
|
||||
}
|
||||
Reference in New Issue
Block a user