Merge pull request #40 from cato447/remove-used-ingredients
Implement first draft to remove used ingredients
This commit is contained in:
39
backend/src/main/spoonaccular/AmountConversion.java
Normal file
39
backend/src/main/spoonaccular/AmountConversion.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package spoonaccular;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import io.github.cdimascio.dotenv.Dotenv;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import spoonaccular.models.amount_conversion.ConvertedAmount;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class AmountConversion {
|
||||||
|
|
||||||
|
private static final OkHttpClient client = new OkHttpClient();
|
||||||
|
private static final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
|
||||||
|
|
||||||
|
private AmountConversion(){
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double convertAmount(String ingrdientName, Double sourceAmount, String sourceUnit, String targetUnit) throws IOException {
|
||||||
|
Request request = APIAuthentication.addAuthHeaders(
|
||||||
|
new Request.Builder()
|
||||||
|
.url("https://" + dotenv.get("X-RapidAPI-Host") +
|
||||||
|
"/recipes/convert?ingredientName=" + ingrdientName +
|
||||||
|
"&targetUnit=" + targetUnit +
|
||||||
|
"&sourceUnit=" + sourceUnit +
|
||||||
|
"&sourceAmount=" + sourceAmount)
|
||||||
|
).build();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
String responseString = response.body().string();
|
||||||
|
try {
|
||||||
|
return new ObjectMapper().readValue(responseString, ConvertedAmount.class).getTargetAmount();
|
||||||
|
} catch (Exception e){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,10 @@ import io.github.cdimascio.dotenv.Dotenv;
|
|||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
import org.springframework.stereotype.Component;
|
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.ExtendedRecipeByIngredient;
|
||||||
import spoonaccular.models.recipe_by_ingredient.RecipeByIngredient;
|
import spoonaccular.models.recipe_by_ingredient.RecipeByIngredient;
|
||||||
import spoonaccular.models.recipe_information.Recipe;
|
import spoonaccular.models.recipe_information.Recipe;
|
||||||
@@ -37,6 +40,16 @@ public class RecipeInformation {
|
|||||||
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>() {});
|
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 {
|
private static Response queryInformationBulk(String idsString) throws IOException {
|
||||||
Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
|
Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
|
||||||
.url("https://" + dotenv.get("X-RapidAPI-Host") +
|
.url("https://" + dotenv.get("X-RapidAPI-Host") +
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package spoonaccular.models.amount_conversion;
|
||||||
|
|
||||||
|
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({
|
||||||
|
"sourceAmount",
|
||||||
|
"sourceUnit",
|
||||||
|
"targetAmount",
|
||||||
|
"targetUnit",
|
||||||
|
"answer",
|
||||||
|
"type"
|
||||||
|
})
|
||||||
|
@Generated("jsonschema2pojo")
|
||||||
|
public class ConvertedAmount {
|
||||||
|
|
||||||
|
@JsonProperty("sourceAmount")
|
||||||
|
private Double sourceAmount;
|
||||||
|
@JsonProperty("sourceUnit")
|
||||||
|
private String sourceUnit;
|
||||||
|
@JsonProperty("targetAmount")
|
||||||
|
private Double targetAmount;
|
||||||
|
@JsonProperty("targetUnit")
|
||||||
|
private String targetUnit;
|
||||||
|
@JsonProperty("answer")
|
||||||
|
private String answer;
|
||||||
|
@JsonProperty("type")
|
||||||
|
private String type;
|
||||||
|
@JsonIgnore
|
||||||
|
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
@JsonProperty("sourceAmount")
|
||||||
|
public Double getSourceAmount() {
|
||||||
|
return sourceAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("sourceAmount")
|
||||||
|
public void setSourceAmount(Double sourceAmount) {
|
||||||
|
this.sourceAmount = sourceAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("sourceUnit")
|
||||||
|
public String getSourceUnit() {
|
||||||
|
return sourceUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("sourceUnit")
|
||||||
|
public void setSourceUnit(String sourceUnit) {
|
||||||
|
this.sourceUnit = sourceUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("targetAmount")
|
||||||
|
public Double getTargetAmount() {
|
||||||
|
return targetAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("targetAmount")
|
||||||
|
public void setTargetAmount(Double targetAmount) {
|
||||||
|
this.targetAmount = targetAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("targetUnit")
|
||||||
|
public String getTargetUnit() {
|
||||||
|
return targetUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("targetUnit")
|
||||||
|
public void setTargetUnit(String targetUnit) {
|
||||||
|
this.targetUnit = targetUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("answer")
|
||||||
|
public String getAnswer() {
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("answer")
|
||||||
|
public void setAnswer(String answer) {
|
||||||
|
this.answer = answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("type")
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("type")
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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({
|
||||||
|
"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,59 @@
|
|||||||
|
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.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@RestController()
|
||||||
|
@BasePathAwareController()
|
||||||
|
public class IngredientController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ItemService itemService;
|
||||||
|
|
||||||
|
@DeleteMapping("/ingredient/removeRecipeIngredients")
|
||||||
|
public List<Item> removeRecipeIngredients(@RequestParam int id) throws IOException, JSONException {
|
||||||
|
List<Ingredient> ingredientList = RecipeInformation.getIngredientList(id);
|
||||||
|
List<Item> changedItemList = new LinkedList<>();
|
||||||
|
for (Ingredient ingredient : ingredientList){
|
||||||
|
Optional<Item> possibleItem = itemService.findByNameIgnoreCaseContaining(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);
|
||||||
|
itemService.save(item);
|
||||||
|
} else {
|
||||||
|
itemService.deleteById(item.getId());
|
||||||
|
}
|
||||||
|
changedItemList.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return changedItemList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -34,6 +34,11 @@ public class ItemServiceImpl implements ItemService {
|
|||||||
return itemRepository.findByName(name);
|
return itemRepository.findByName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Item> findByNameIgnoreCaseContaining(String name) {
|
||||||
|
return itemRepository.findByNameIgnoreCaseContaining(name);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Item> findAll() {
|
public Iterable<Item> findAll() {
|
||||||
return itemRepository.findAll();
|
return itemRepository.findAll();
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ public class Item {
|
|||||||
|
|
||||||
private Unit unit;
|
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.name = name;
|
||||||
this.unit = unit;
|
this.unit = unit;
|
||||||
this.quantity = quantity;
|
this.quantity = quantity;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package whattocook.models;
|
package whattocook.models;
|
||||||
|
|
||||||
public enum Unit {
|
public enum Unit {
|
||||||
GRAMMS,
|
g,
|
||||||
MILLILETERS
|
ml
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface ItemRepository extends CrudRepository<Item, Long> {
|
public interface ItemRepository extends CrudRepository<Item, Long> {
|
||||||
Optional<Item> findByName(String name);
|
Optional<Item> findByName(String name);
|
||||||
|
Optional<Item> findByNameIgnoreCaseContaining(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ public interface ItemService {
|
|||||||
void deleteById(Long id);
|
void deleteById(Long id);
|
||||||
Optional<Item> findById(long id);
|
Optional<Item> findById(long id);
|
||||||
Optional<Item> findByName(String name);
|
Optional<Item> findByName(String name);
|
||||||
|
Optional<Item> findByNameIgnoreCaseContaining(String name);
|
||||||
Iterable<Item> findAll();
|
Iterable<Item> findAll();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ final class ItemTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void saveTest() {
|
public void saveTest() {
|
||||||
Item item = new Item("kartoffel", Unit.GRAMMS, 5000);
|
Item item = new Item("kartoffel", Unit.g, 5000);
|
||||||
itemRepository.save(item);
|
itemRepository.save(item);
|
||||||
assertTrue(itemRepository.existsById(item.getId()));
|
assertTrue(itemRepository.existsById(item.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findByIDTest() {
|
public void findByIDTest() {
|
||||||
Item item = new Item("tortillias", Unit.GRAMMS, 5000);
|
Item item = new Item("tortillias", Unit.g, 5000);
|
||||||
itemRepository.save(item);
|
itemRepository.save(item);
|
||||||
assertEquals(item, itemRepository.findById(item.getId()).get());
|
assertEquals(item, itemRepository.findById(item.getId()).get());
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ final class ItemTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findByNameTest(){
|
public void findByNameTest(){
|
||||||
Item item = new Item("tortillias", Unit.GRAMMS, 5000);
|
Item item = new Item("tortillias", Unit.g, 5000);
|
||||||
itemRepository.save(item);
|
itemRepository.save(item);
|
||||||
assertEquals(item, itemRepository.findByName(item.getName()).get());
|
assertEquals(item, itemRepository.findByName(item.getName()).get());
|
||||||
}
|
}
|
||||||
@@ -57,11 +57,11 @@ final class ItemTests {
|
|||||||
@Test
|
@Test
|
||||||
public void findAllTest() {
|
public void findAllTest() {
|
||||||
List<Item> savedItems = new ArrayList();
|
List<Item> savedItems = new ArrayList();
|
||||||
savedItems.add( new Item("nachos", Unit.GRAMMS, 5000));
|
savedItems.add( new Item("nachos", Unit.g, 5000));
|
||||||
savedItems.add( new Item("wurst", Unit.GRAMMS, 5000));
|
savedItems.add( new Item("wurst", Unit.g, 5000));
|
||||||
savedItems.add( new Item("schinken", Unit.GRAMMS, 5000));
|
savedItems.add( new Item("schinken", Unit.g, 5000));
|
||||||
savedItems.add( new Item("brokkoli", Unit.GRAMMS, 5000));
|
savedItems.add( new Item("brokkoli", Unit.g, 5000));
|
||||||
savedItems.add( new Item("eiscreme", Unit.GRAMMS, 5000));
|
savedItems.add( new Item("eiscreme", Unit.g, 5000));
|
||||||
|
|
||||||
itemRepository.saveAll(savedItems);
|
itemRepository.saveAll(savedItems);
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ final class ItemTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void deleteTest() {
|
public void deleteTest() {
|
||||||
Item item = new Item("elefantenfuß", Unit.GRAMMS, 5000);
|
Item item = new Item("elefantenfuß", Unit.g, 5000);
|
||||||
itemRepository.save(item);
|
itemRepository.save(item);
|
||||||
assertEquals(item, itemRepository.findById(item.getId()).get());
|
assertEquals(item, itemRepository.findById(item.getId()).get());
|
||||||
itemRepository.delete(item);
|
itemRepository.delete(item);
|
||||||
@@ -80,7 +80,7 @@ final class ItemTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateTest() {
|
public void updateTest() {
|
||||||
Item item = new Item("schokoküsse", Unit.GRAMMS, 5000);
|
Item item = new Item("schokoküsse", Unit.g, 5000);
|
||||||
itemRepository.save(item);
|
itemRepository.save(item);
|
||||||
long itemCount = itemRepository.count();
|
long itemCount = itemRepository.count();
|
||||||
item.setQuantity(4574);
|
item.setQuantity(4574);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'org.springframework.boot' version '2.6.7'
|
id 'org.springframework.boot' version '2.7.1'
|
||||||
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
||||||
id 'java'
|
id 'java'
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user