Add db functions to get all recipes / base foods
This commit is contained in:
parent
fd909079a3
commit
750be015bb
@ -103,3 +103,17 @@ func (db *DB) GetFoodByID(id FoodID) (ret Food, err error) {
|
|||||||
`, id)
|
`, id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetAllBaseFoods() []Food {
|
||||||
|
var ret []Food
|
||||||
|
err := db.DB.Select(&ret, `
|
||||||
|
select rowid, name, cals, carbs, protein, fat, sugar, alcohol, water, potassium, calcium, sodium,
|
||||||
|
magnesium, phosphorus, iron, zinc, mass, price, density, cook_ratio
|
||||||
|
from foods
|
||||||
|
where rowid not in (select computed_food_id from recipes)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
@ -70,3 +70,15 @@ func TestFoodSaveAndLoad(t *testing.T) {
|
|||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should list all the base foods (i.e., ones that aren't recipes)
|
||||||
|
func TestListAllBaseFoods(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
db := get_test_db()
|
||||||
|
|
||||||
|
base_foods := db.GetAllBaseFoods()
|
||||||
|
assert.True(len(base_foods) >= 100)
|
||||||
|
for _, f := range base_foods {
|
||||||
|
assert.NotContains([]FoodID{10000, 10001}, f.ID, f) // Computed foods have ID >= 10000 in seed data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -132,6 +132,15 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetAllRecipes() []Recipe {
|
||||||
|
var ret []Recipe
|
||||||
|
err := db.DB.Select(&ret, `select rowid, name, blurb, instructions, computed_food_id from recipes`)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
func (r Recipe) ComputeFood() Food {
|
func (r Recipe) ComputeFood() Food {
|
||||||
// If r.ComputedFoodID is 0, so should be the ID of returned Food
|
// If r.ComputedFoodID is 0, so should be the ID of returned Food
|
||||||
ret := Food{ID: r.ComputedFoodID, Name: r.Name}
|
ret := Food{ID: r.ComputedFoodID, Name: r.Name}
|
||||||
|
@ -140,3 +140,12 @@ func TestRecipeSaveComputedFood(t *testing.T) {
|
|||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should list all the recipes
|
||||||
|
func TestListAllRecipes(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
db := get_test_db()
|
||||||
|
|
||||||
|
recipes := db.GetAllRecipes()
|
||||||
|
assert.True(len(recipes) >= 2)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user