Make quantity just a single float instead of a rational

This commit is contained in:
Alessio 2024-11-10 18:28:51 -08:00
parent 3bbe9687f2
commit 6df80e75ec
6 changed files with 37 additions and 47 deletions

View File

@ -27,7 +27,7 @@ type Food struct {
Iron float32 `db:"iron"` Iron float32 `db:"iron"`
Zinc float32 `db:"zinc"` Zinc float32 `db:"zinc"`
Mass float32 `db:"mass"` Mass float32 `db:"mass"` // In grams
Price float32 `db:"price"` Price float32 `db:"price"`
Density float32 `db:"density"` Density float32 `db:"density"`
CookRatio float32 `db:"cook_ratio"` CookRatio float32 `db:"cook_ratio"`

View File

@ -11,9 +11,8 @@ type Ingredient struct {
FoodID FoodID `db:"food_id"` FoodID FoodID `db:"food_id"`
RecipeID RecipeID `db:"recipe_id"` RecipeID RecipeID `db:"recipe_id"`
QuantityNumerator int64 `db:"quantity_numerator"` Quantity float32
QuantityDenominator int64 `db:"quantity_denominator"` Units UnitsID `db:"units"`
Units UnitsID `db:"units"`
InRecipeID RecipeID `db:"in_recipe_id"` InRecipeID RecipeID `db:"in_recipe_id"`
ListOrder int64 `db:"list_order"` ListOrder int64 `db:"list_order"`
@ -29,12 +28,11 @@ type Ingredient struct {
func (db *DB) SaveIngredient(i *Ingredient) { func (db *DB) SaveIngredient(i *Ingredient) {
if i.ID == IngredientID(0) { if i.ID == IngredientID(0) {
println("creating---------")
// Do create // Do create
result, err := db.DB.NamedExec(` result, err := db.DB.NamedExec(`
insert into ingredients insert into ingredients
(food_id, recipe_id, quantity_numerator, quantity_denominator, units, in_recipe_id, list_order, is_hidden) (food_id, recipe_id, quantity, units, in_recipe_id, list_order, is_hidden)
values (nullif(:food_id, 0), nullif(:recipe_id, 0), :quantity_numerator, :quantity_denominator, :units, :in_recipe_id, values (nullif(:food_id, 0), nullif(:recipe_id, 0), :quantity, :units, :in_recipe_id,
:list_order, :is_hidden) :list_order, :is_hidden)
`, i) `, i)
if err != nil { if err != nil {
@ -48,14 +46,12 @@ func (db *DB) SaveIngredient(i *Ingredient) {
} }
i.ID = IngredientID(id) i.ID = IngredientID(id)
} else { } else {
println("updating---------")
// Do update // Do update
result, err := db.DB.NamedExec(` result, err := db.DB.NamedExec(`
update ingredients update ingredients
set food_id=nullif(:food_id, 0), set food_id=nullif(:food_id, 0),
recipe_id=nullif(:recipe_id, 0), recipe_id=nullif(:recipe_id, 0),
quantity_numerator=:quantity_numerator, quantity=:quantity,
quantity_denominator=:quantity_denominator,
units=:units, units=:units,
list_order=:list_order, list_order=:list_order,
is_hidden=:is_hidden is_hidden=:is_hidden
@ -88,6 +84,3 @@ func (db *DB) DeleteIngredient(i Ingredient) {
} }
} }
func (i Ingredient) Quantity() float32 {
return float32(i.QuantityNumerator) / float32(i.QuantityDenominator)
}

View File

@ -28,13 +28,12 @@ func TestSaveAndLoadIngredient(t *testing.T) {
// Create an ingredient on the recipe // Create an ingredient on the recipe
ingr := Ingredient{ ingr := Ingredient{
FoodID: food.ID, FoodID: food.ID,
Food: &food, Food: &food,
QuantityNumerator: 3, Quantity: 1.5,
QuantityDenominator: 2, Units: 1, // count
Units: 1, // count InRecipeID: recipe.ID,
InRecipeID: recipe.ID, ListOrder: 0,
ListOrder: 0,
} }
assert.Equal(ingr.ID, IngredientID(0)) assert.Equal(ingr.ID, IngredientID(0))
db.SaveIngredient(&ingr) db.SaveIngredient(&ingr)
@ -50,8 +49,7 @@ func TestSaveAndLoadIngredient(t *testing.T) {
} }
// Modify the ingredient // Modify the ingredient
ingr.QuantityNumerator = 5 ingr.Quantity = 1.25
ingr.QuantityDenominator = 4
ingr.Units = 2 ingr.Units = 2
// Save it and reload the recipe // Save it and reload the recipe

View File

@ -88,7 +88,7 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
// Load the ingredients // Load the ingredients
err = db.DB.Select(&ret.Ingredients, ` err = db.DB.Select(&ret.Ingredients, `
select rowid, ifnull(food_id, 0) food_id, ifnull(recipe_id, 0) recipe_id, quantity_numerator, quantity_denominator, units, select rowid, ifnull(food_id, 0) food_id, ifnull(recipe_id, 0) recipe_id, quantity, units,
in_recipe_id, list_order, is_hidden in_recipe_id, list_order, is_hidden
from ingredients from ingredients
where in_recipe_id = ? where in_recipe_id = ?
@ -123,22 +123,22 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
func (r Recipe) ComputeFood() Food { func (r Recipe) ComputeFood() Food {
ret := Food{} ret := Food{}
for _, ingr := range r.Ingredients { for _, ingr := range r.Ingredients {
ret.Cals += ingr.Quantity() * ingr.Food.Cals ret.Cals += ingr.Quantity * ingr.Food.Cals
ret.Carbs += ingr.Quantity() * ingr.Food.Carbs ret.Carbs += ingr.Quantity * ingr.Food.Carbs
ret.Protein += ingr.Quantity() * ingr.Food.Protein ret.Protein += ingr.Quantity * ingr.Food.Protein
ret.Fat += ingr.Quantity() * ingr.Food.Fat ret.Fat += ingr.Quantity * ingr.Food.Fat
ret.Sugar += ingr.Quantity() * ingr.Food.Sugar ret.Sugar += ingr.Quantity * ingr.Food.Sugar
ret.Alcohol += ingr.Quantity() * ingr.Food.Alcohol ret.Alcohol += ingr.Quantity * ingr.Food.Alcohol
ret.Water += ingr.Quantity() * ingr.Food.Water ret.Water += ingr.Quantity * ingr.Food.Water
ret.Potassium += ingr.Quantity() * ingr.Food.Potassium ret.Potassium += ingr.Quantity * ingr.Food.Potassium
ret.Calcium += ingr.Quantity() * ingr.Food.Calcium ret.Calcium += ingr.Quantity * ingr.Food.Calcium
ret.Sodium += ingr.Quantity() * ingr.Food.Sodium ret.Sodium += ingr.Quantity * ingr.Food.Sodium
ret.Magnesium += ingr.Quantity() * ingr.Food.Magnesium ret.Magnesium += ingr.Quantity * ingr.Food.Magnesium
ret.Phosphorus += ingr.Quantity() * ingr.Food.Phosphorus ret.Phosphorus += ingr.Quantity * ingr.Food.Phosphorus
ret.Iron += ingr.Quantity() * ingr.Food.Iron ret.Iron += ingr.Quantity * ingr.Food.Iron
ret.Zinc += ingr.Quantity() * ingr.Food.Zinc ret.Zinc += ingr.Quantity * ingr.Food.Zinc
ret.Mass += ingr.Quantity() * ingr.Food.Mass ret.Mass += ingr.Quantity * ingr.Food.Mass
ret.Price += ingr.Quantity() * ingr.Food.Price ret.Price += ingr.Quantity * ingr.Food.Price
} }
return ret return ret
} }

View File

@ -49,8 +49,8 @@ func TestRecipeComputeFood(t *testing.T) {
f2 := Food{0, "", 16.5, 15.5, 14.5, 13.5, 12.5, 11.5, 10.5, 9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0, 0} f2 := Food{0, "", 16.5, 15.5, 14.5, 13.5, 12.5, 11.5, 10.5, 9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0, 0}
recipe := Recipe{Ingredients: []Ingredient{ recipe := Recipe{Ingredients: []Ingredient{
{QuantityNumerator: 1, QuantityDenominator: 1, Food: &f1}, {Quantity: 1, Food: &f1},
{QuantityNumerator: 1, QuantityDenominator: 1, Food: &f2}, {Quantity: 1, Food: &f2},
}} }}
computed_food := recipe.ComputeFood() computed_food := recipe.ComputeFood()
assert.Equal(computed_food.Cals, float32(17.5)) assert.Equal(computed_food.Cals, float32(17.5))
@ -71,8 +71,8 @@ func TestRecipeComputeFood(t *testing.T) {
assert.Equal(computed_food.Price, float32(17.5)) assert.Equal(computed_food.Price, float32(17.5))
recipe2 := Recipe{Ingredients: []Ingredient{ recipe2 := Recipe{Ingredients: []Ingredient{
{QuantityNumerator: 3, QuantityDenominator: 2, Food: &f1}, {Quantity: 1.5, Food: &f1},
{QuantityNumerator: 1, QuantityDenominator: 2, Food: &f2}, {Quantity: 0.5, Food: &f2},
}} }}
computed_food2 := recipe2.ComputeFood() computed_food2 := recipe2.ComputeFood()
assert.Equal(computed_food2.Cals, float32(9.75)) assert.Equal(computed_food2.Cals, float32(9.75))

View File

@ -11,7 +11,7 @@ insert into db_version values(0);
create table food_types (rowid integer primary key, create table food_types (rowid integer primary key,
name text not null unique check(length(name) != 0) name text not null unique check(length(name) != 0)
); ) strict;
insert into food_types (name) values insert into food_types (name) values
('grocery'), ('grocery'),
('recipe'), ('recipe'),
@ -51,7 +51,7 @@ create table units (rowid integer primary key,
name text not null unique check(length(name) != 0), name text not null unique check(length(name) != 0),
abbreviation text not null unique check(length(abbreviation) != 0) abbreviation text not null unique check(length(abbreviation) != 0)
-- is_metric integer not null check(is_metric in (0, 1)) -- is_metric integer not null check(is_metric in (0, 1))
); ) strict;
insert into units(rowid, name, abbreviation) values insert into units(rowid, name, abbreviation) values
-- Count -- Count
(1, 'count', 'ct'), (1, 'count', 'ct'),
@ -75,8 +75,7 @@ create table ingredients (rowid integer primary key,
recipe_id integer references recipes(rowid), recipe_id integer references recipes(rowid),
-- Portion size (rational numbers) -- Portion size (rational numbers)
quantity_numerator integer not null default 1, quantity real not null default 1,
quantity_denominator integer not null default 1,
units integer not null default 0, -- Display purposes only units integer not null default 0, -- Display purposes only
in_recipe_id integer references recipes(rowid) on delete cascade not null, in_recipe_id integer references recipes(rowid) on delete cascade not null,