[WIP] BROKEN - trying to remove the used ingredients automagically
This commit is contained in:
@@ -6,7 +6,10 @@ import io.github.cdimascio.dotenv.Dotenv;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
import spoonaccular.models.ingredients_by_id.Ingredient;
|
||||
import spoonaccular.models.recipe_by_ingredient.ExtendedRecipeByIngredient;
|
||||
import spoonaccular.models.recipe_by_ingredient.RecipeByIngredient;
|
||||
import spoonaccular.models.recipe_information.Recipe;
|
||||
@@ -37,6 +40,16 @@ public class RecipeInformation {
|
||||
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>() {});
|
||||
}
|
||||
|
||||
public static List<Ingredient> getIngredientList(int recipeId) throws IOException, JSONException {
|
||||
Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
|
||||
.url("https://" + dotenv.get("X-RapidAPI-Host") +
|
||||
"/recipes/" + recipeId + "/ingredientWidget.json"))
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
JSONObject jsonObject = new JSONObject(response.body().string());
|
||||
return new ObjectMapper().readValue(jsonObject.getJSONArray("ingredients").toString(), new TypeReference<>() {});
|
||||
}
|
||||
|
||||
private static Response queryInformationBulk(String idsString) throws IOException {
|
||||
Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
|
||||
.url("https://" + dotenv.get("X-RapidAPI-Host") +
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
package spoonaccular.models.ingredients_by_id;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Generated;
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({
|
||||
"metric",
|
||||
"us"
|
||||
})
|
||||
@Generated("jsonschema2pojo")
|
||||
public class Amount {
|
||||
|
||||
@JsonProperty("metric")
|
||||
private Metric metric;
|
||||
@JsonProperty("us")
|
||||
private Us us;
|
||||
@JsonIgnore
|
||||
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
|
||||
@JsonProperty("metric")
|
||||
public Metric getMetric() {
|
||||
return metric;
|
||||
}
|
||||
|
||||
@JsonProperty("metric")
|
||||
public void setMetric(Metric metric) {
|
||||
this.metric = metric;
|
||||
}
|
||||
|
||||
@JsonProperty("us")
|
||||
public Us getUs() {
|
||||
return us;
|
||||
}
|
||||
|
||||
@JsonProperty("us")
|
||||
public void setUs(Us us) {
|
||||
this.us = us;
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getAdditionalProperties() {
|
||||
return this.additionalProperties;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setAdditionalProperty(String name, Object value) {
|
||||
this.additionalProperties.put(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
|
||||
package spoonaccular.models.ingredients_by_id;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Generated;
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({
|
||||
"name",
|
||||
"image",
|
||||
"amount"
|
||||
})
|
||||
@Generated("jsonschema2pojo")
|
||||
public class Ingredient {
|
||||
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
@JsonProperty("image")
|
||||
private String image;
|
||||
@JsonProperty("amount")
|
||||
private Amount amount;
|
||||
@JsonIgnore
|
||||
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@JsonProperty("name")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonProperty("image")
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
@JsonProperty("image")
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
@JsonProperty("amount")
|
||||
public Amount getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@JsonProperty("amount")
|
||||
public void setAmount(Amount amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getAdditionalProperties() {
|
||||
return this.additionalProperties;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setAdditionalProperty(String name, Object value) {
|
||||
this.additionalProperties.put(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
package spoonaccular.models.ingredients_by_id;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Generated;
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({
|
||||
"value",
|
||||
"unit"
|
||||
})
|
||||
@Generated("jsonschema2pojo")
|
||||
public class Metric {
|
||||
|
||||
@JsonProperty("value")
|
||||
private Double value;
|
||||
@JsonProperty("unit")
|
||||
private String unit;
|
||||
@JsonIgnore
|
||||
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
|
||||
@JsonProperty("value")
|
||||
public Double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@JsonProperty("value")
|
||||
public void setValue(Double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonProperty("unit")
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@JsonProperty("unit")
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getAdditionalProperties() {
|
||||
return this.additionalProperties;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setAdditionalProperty(String name, Object value) {
|
||||
this.additionalProperties.put(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
package spoonaccular.models.ingredients_by_id;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Generated;
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({
|
||||
"value",
|
||||
"unit"
|
||||
})
|
||||
@Generated("jsonschema2pojo")
|
||||
public class Us {
|
||||
|
||||
@JsonProperty("value")
|
||||
private Double value;
|
||||
@JsonProperty("unit")
|
||||
private String unit;
|
||||
@JsonIgnore
|
||||
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
|
||||
@JsonProperty("value")
|
||||
public Double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@JsonProperty("value")
|
||||
public void setValue(Double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonProperty("unit")
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@JsonProperty("unit")
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getAdditionalProperties() {
|
||||
return this.additionalProperties;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setAdditionalProperty(String name, Object value) {
|
||||
this.additionalProperties.put(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package whattocook.controller;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.rest.webmvc.BasePathAwareController;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import spoonaccular.AmountConversion;
|
||||
import spoonaccular.RecipeInformation;
|
||||
import spoonaccular.models.ingredients_by_id.Ingredient;
|
||||
import whattocook.models.Item;
|
||||
import whattocook.services.ItemService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController()
|
||||
@BasePathAwareController()
|
||||
public class IngredientController {
|
||||
|
||||
@Autowired
|
||||
ItemService itemService;
|
||||
|
||||
@DeleteMapping("/ingredient/removeRecipeIngredients")
|
||||
public void removeRecipeIngredients(@RequestParam int id) throws IOException, JSONException {
|
||||
List<Ingredient> ingredientList = RecipeInformation.getIngredientList(id);
|
||||
for (Ingredient ingredient : ingredientList){
|
||||
Optional<Item> possibleItem = itemService.findByName(ingredient.getName());
|
||||
if (possibleItem.isPresent()){
|
||||
Item item = possibleItem.get();
|
||||
double newItemQuantity;
|
||||
//same unit
|
||||
if (item.getUnit().toString().equals(ingredient.getAmount().getMetric().getUnit())){
|
||||
newItemQuantity = item.getQuantity() - ingredient.getAmount().getMetric().getValue();
|
||||
} else {
|
||||
double ingridientAmount = AmountConversion.convertAmount(ingredient.getName(),
|
||||
ingredient.getAmount().getMetric().getValue(),
|
||||
ingredient.getAmount().getMetric().getUnit(),
|
||||
item.getUnit().toString());
|
||||
newItemQuantity = item.getQuantity() - ingridientAmount;
|
||||
}
|
||||
if (newItemQuantity > 0){
|
||||
item.setQuantity(newItemQuantity);
|
||||
} else {
|
||||
itemService.deleteById(item.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -25,9 +25,9 @@ public class Item {
|
||||
|
||||
private Unit unit;
|
||||
|
||||
private int quantity;
|
||||
private double quantity;
|
||||
|
||||
public Item(String name, Unit unit, int quantity){
|
||||
public Item(String name, Unit unit, double quantity){
|
||||
this.name = name;
|
||||
this.unit = unit;
|
||||
this.quantity = quantity;
|
||||
|
||||
Reference in New Issue
Block a user