diff --git a/pkg/db/food.go b/pkg/db/food.go index 70993fc..d6cd82f 100644 --- a/pkg/db/food.go +++ b/pkg/db/food.go @@ -103,3 +103,17 @@ func (db *DB) GetFoodByID(id FoodID) (ret Food, err error) { `, id) 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 +} diff --git a/pkg/db/food_test.go b/pkg/db/food_test.go index b831ece..23b19eb 100644 --- a/pkg/db/food_test.go +++ b/pkg/db/food_test.go @@ -70,3 +70,15 @@ func TestFoodSaveAndLoad(t *testing.T) { 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 + } +} diff --git a/pkg/db/recipe.go b/pkg/db/recipe.go index 999f03d..e97162d 100644 --- a/pkg/db/recipe.go +++ b/pkg/db/recipe.go @@ -132,6 +132,15 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) { 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 { // If r.ComputedFoodID is 0, so should be the ID of returned Food ret := Food{ID: r.ComputedFoodID, Name: r.Name} diff --git a/pkg/db/recipe_test.go b/pkg/db/recipe_test.go index d965568..c69f885 100644 --- a/pkg/db/recipe_test.go +++ b/pkg/db/recipe_test.go @@ -140,3 +140,12 @@ func TestRecipeSaveComputedFood(t *testing.T) { 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) +}