first draft implemented

Still struggling at some ingredients which deviating names
This commit is contained in:
2022-07-16 22:20:32 +02:00
parent d9053ebaa4
commit 711a8e5c21
8 changed files with 35 additions and 18 deletions

View File

@@ -13,6 +13,7 @@ import whattocook.models.Item;
import whattocook.services.ItemService;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@@ -24,10 +25,11 @@ public class IngredientController {
ItemService itemService;
@DeleteMapping("/ingredient/removeRecipeIngredients")
public void removeRecipeIngredients(@RequestParam int id) throws IOException, JSONException {
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.findByName(ingredient.getName());
Optional<Item> possibleItem = itemService.findByNameIgnoreCaseContaining(ingredient.getName());
if (possibleItem.isPresent()){
Item item = possibleItem.get();
double newItemQuantity;
@@ -42,12 +44,15 @@ public class IngredientController {
newItemQuantity = item.getQuantity() - ingridientAmount;
}
if (newItemQuantity > 0){
item.setQuantity(newItemQuantity);
item.setQuantity(newItemQuantity);
itemService.save(item);
} else {
itemService.deleteById(item.getId());
}
changedItemList.add(item);
}
}
return changedItemList;
}

View File

@@ -34,6 +34,11 @@ public class ItemServiceImpl implements ItemService {
return itemRepository.findByName(name);
}
@Override
public Optional<Item> findByNameIgnoreCaseContaining(String name) {
return itemRepository.findByNameIgnoreCaseContaining(name);
}
@Override
public Iterable<Item> findAll() {
return itemRepository.findAll();

View File

@@ -1,6 +1,6 @@
package whattocook.models;
public enum Unit {
GRAMMS,
MILLILETERS
g,
ml
}

View File

@@ -9,4 +9,5 @@ import java.util.Optional;
@Repository
public interface ItemRepository extends CrudRepository<Item, Long> {
Optional<Item> findByName(String name);
Optional<Item> findByNameIgnoreCaseContaining(String name);
}

View File

@@ -9,5 +9,6 @@ public interface ItemService {
void deleteById(Long id);
Optional<Item> findById(long id);
Optional<Item> findByName(String name);
Optional<Item> findByNameIgnoreCaseContaining(String name);
Iterable<Item> findAll();
}