Food, User

This commit is contained in:
2023-01-27 15:14:36 +01:00
parent b97fe83f40
commit a0eed3ce51
10 changed files with 366 additions and 100 deletions

View File

@@ -6,21 +6,21 @@ package com.bib.essensbestellungsverwaltung;
import java.util.ArrayList;
import java.util.List;
public class FoodMgr {
/**
* inserts a food int to the database and creates the food_restriction entries
* @param foodData name, description, isdessert, food_typeid
* @param allergyData allergyids
* @return id of food or -1
*/
public static long createFood(String[] foodData, String[] allergyData){
public static long createFood(Food food){
String[] foodH = {"name","description","isDessert","food_typeid"};
String[] food_restrictionH = {"foodid","allergyid"};
long id = Database.insert("food",foodH,foodData);
if(allergyData.length > 0){
String[] foodD = {food.getName(),food.getDescription(),(food.isDessert() ? "1" : "0"), String.valueOf(food.getFoodType().getId())};
long id = Database.insert("food",foodH,foodD);
if(food.getAllergies().size() > 0){
String sId = String.valueOf(id);
for (String allergyId : allergyData) {
String[] food_restrictionD = {sId,allergyId};
for (Allergy allergy : food.getAllergies()) {
String[] food_restrictionD = {sId, String.valueOf(allergy.getId())};
Database.insert("food_restriction",food_restrictionH, food_restrictionD);
}
}
@@ -42,10 +42,16 @@ public class FoodMgr {
* @param isDessert true for only desserts false for non desserts
* @return a list of all non desserts or all desserts
*/
public static List<String> getFood(boolean isDessert){
public static List<Food> getFood(boolean isDessert){
String[] foodH = {"isDessert"};
String[] foodD = {(isDessert ? "1" : "0")};
return Database.select("food",foodH,foodD);
List<String> entries = Database.select("food",foodH,foodD);
List<Food> foods = new ArrayList<>();
for (String entry : entries) {
String[] parts = entry.split(":");
foods.add(getFoodById(Long.parseLong(parts[0])));
}
return foods;
}
/**
@@ -53,10 +59,16 @@ public class FoodMgr {
* @param isDessert true for only desserts false for non desserts
* @return a list of all vegan non desserts or all vegan desserts
*/
public static List<String> getVeganFood(boolean isDessert){
public static List<Food> getVeganFood(boolean isDessert){
String[] foodH = {"isDessert","food_typeid"};
String[] foodD = {(isDessert ? "1" : "0"),"1"};
return Database.select("food",foodH,foodD);
List<String> entries = Database.select("food",foodH,foodD);
List<Food> foods = new ArrayList<>();
for (String entry : entries) {
String[] parts = entry.split(":");
foods.add(getFoodById(Long.parseLong(parts[0])));
}
return foods;
}
/**
@@ -70,8 +82,42 @@ public class FoodMgr {
return Database.select("food_plan",food_planH,food_planD);
}
public static List<String> getFoodById(long id){
return Database.getEntryById("food",id);
public static Food getFoodById(long id){
List<String> entry = Database.getEntryById("food",id);
String[] parts = entry.get(0).split(":");
String name = parts[1];
String description = parts[2];
boolean isDessert;
isDessert = parts[3].equals("0");
FoodType foodType = getFoodTypeById(Long.parseLong(parts[4]));
List<Allergy> allergies = getAllergies(id);
return new Food(id,name,description,isDessert,foodType,allergies);
}
public static FoodType getFoodTypeById(long id){
List<String> entry = Database.getEntryById("foodtype",id);
String[] typeParts = entry.get(0).split(":");
return new FoodType(Long.parseLong(typeParts[0]),typeParts[1]);
}
public static Allergy getAllergyById(long id){
String[] allergyH = {"id"};
String[] allergyD = {String.valueOf(id)};
List<String> allergies = Database.select("allergy",allergyH,allergyD);
String[] allergyParts = allergies.get(0).split(":");
return new Allergy(id,allergyParts[1],allergyParts[2]);
}
public static List<Allergy> getAllergies(long foodId){
List<Allergy> allergies = new ArrayList<>();
String[] restrictionsH = {"foodid"};
String[] restrictionsD = {String.valueOf(foodId)};
List<String> restrictions = Database.select("food_restrictions",restrictionsH,restrictionsD);
for (String restriction : restrictions) {
String[] partsRestrictions = restriction.split(":");
allergies.add(getAllergyById(Long.parseLong(partsRestrictions[2])));
}
return allergies;
}
/**
@@ -100,11 +146,10 @@ public class FoodMgr {
List<String> food_plan = getFood_plan(date);
String[] food_planParts = food_plan.get(0).split(":");
for(int i = 2; i < 2+4; i++){
List<String> food = getFoodById(Long.parseLong(food_planParts[i]));
String[] foodParts = food.get(0).split(":");
String foodName = foodParts[1];
Food food = getFoodById(Long.parseLong(food_planParts[i]));
String foodName = food.getName();
String[] food_selectionH = {"food_planid","foodid"};
String[] food_selectionD = {food_planParts[0],foodParts[0]};
String[] food_selectionD = {food_planParts[0], String.valueOf(food.getId())};
int count = Database.count("food_selection",food_selectionH,food_selectionD);
orders.add(foodName+":"+count);
}