Make quantity just a single float instead of a rational
This commit is contained in:
parent
3bbe9687f2
commit
6df80e75ec
@ -27,7 +27,7 @@ type Food struct {
|
||||
Iron float32 `db:"iron"`
|
||||
Zinc float32 `db:"zinc"`
|
||||
|
||||
Mass float32 `db:"mass"`
|
||||
Mass float32 `db:"mass"` // In grams
|
||||
Price float32 `db:"price"`
|
||||
Density float32 `db:"density"`
|
||||
CookRatio float32 `db:"cook_ratio"`
|
||||
|
@ -11,9 +11,8 @@ type Ingredient struct {
|
||||
FoodID FoodID `db:"food_id"`
|
||||
RecipeID RecipeID `db:"recipe_id"`
|
||||
|
||||
QuantityNumerator int64 `db:"quantity_numerator"`
|
||||
QuantityDenominator int64 `db:"quantity_denominator"`
|
||||
Units UnitsID `db:"units"`
|
||||
Quantity float32
|
||||
Units UnitsID `db:"units"`
|
||||
|
||||
InRecipeID RecipeID `db:"in_recipe_id"`
|
||||
ListOrder int64 `db:"list_order"`
|
||||
@ -29,12 +28,11 @@ type Ingredient struct {
|
||||
|
||||
func (db *DB) SaveIngredient(i *Ingredient) {
|
||||
if i.ID == IngredientID(0) {
|
||||
println("creating---------")
|
||||
// Do create
|
||||
result, err := db.DB.NamedExec(`
|
||||
insert into ingredients
|
||||
(food_id, recipe_id, quantity_numerator, quantity_denominator, 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,
|
||||
(food_id, recipe_id, quantity, units, in_recipe_id, list_order, is_hidden)
|
||||
values (nullif(:food_id, 0), nullif(:recipe_id, 0), :quantity, :units, :in_recipe_id,
|
||||
:list_order, :is_hidden)
|
||||
`, i)
|
||||
if err != nil {
|
||||
@ -48,14 +46,12 @@ func (db *DB) SaveIngredient(i *Ingredient) {
|
||||
}
|
||||
i.ID = IngredientID(id)
|
||||
} else {
|
||||
println("updating---------")
|
||||
// Do update
|
||||
result, err := db.DB.NamedExec(`
|
||||
update ingredients
|
||||
set food_id=nullif(:food_id, 0),
|
||||
recipe_id=nullif(:recipe_id, 0),
|
||||
quantity_numerator=:quantity_numerator,
|
||||
quantity_denominator=:quantity_denominator,
|
||||
quantity=:quantity,
|
||||
units=:units,
|
||||
list_order=:list_order,
|
||||
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)
|
||||
}
|
||||
|
@ -28,13 +28,12 @@ func TestSaveAndLoadIngredient(t *testing.T) {
|
||||
|
||||
// Create an ingredient on the recipe
|
||||
ingr := Ingredient{
|
||||
FoodID: food.ID,
|
||||
Food: &food,
|
||||
QuantityNumerator: 3,
|
||||
QuantityDenominator: 2,
|
||||
Units: 1, // count
|
||||
InRecipeID: recipe.ID,
|
||||
ListOrder: 0,
|
||||
FoodID: food.ID,
|
||||
Food: &food,
|
||||
Quantity: 1.5,
|
||||
Units: 1, // count
|
||||
InRecipeID: recipe.ID,
|
||||
ListOrder: 0,
|
||||
}
|
||||
assert.Equal(ingr.ID, IngredientID(0))
|
||||
db.SaveIngredient(&ingr)
|
||||
@ -50,8 +49,7 @@ func TestSaveAndLoadIngredient(t *testing.T) {
|
||||
}
|
||||
|
||||
// Modify the ingredient
|
||||
ingr.QuantityNumerator = 5
|
||||
ingr.QuantityDenominator = 4
|
||||
ingr.Quantity = 1.25
|
||||
ingr.Units = 2
|
||||
|
||||
// Save it and reload the recipe
|
||||
|
@ -88,7 +88,7 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
|
||||
|
||||
// Load the 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
|
||||
from ingredients
|
||||
where in_recipe_id = ?
|
||||
@ -123,22 +123,22 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
|
||||
func (r Recipe) ComputeFood() Food {
|
||||
ret := Food{}
|
||||
for _, ingr := range r.Ingredients {
|
||||
ret.Cals += ingr.Quantity() * ingr.Food.Cals
|
||||
ret.Carbs += ingr.Quantity() * ingr.Food.Carbs
|
||||
ret.Protein += ingr.Quantity() * ingr.Food.Protein
|
||||
ret.Fat += ingr.Quantity() * ingr.Food.Fat
|
||||
ret.Sugar += ingr.Quantity() * ingr.Food.Sugar
|
||||
ret.Alcohol += ingr.Quantity() * ingr.Food.Alcohol
|
||||
ret.Water += ingr.Quantity() * ingr.Food.Water
|
||||
ret.Potassium += ingr.Quantity() * ingr.Food.Potassium
|
||||
ret.Calcium += ingr.Quantity() * ingr.Food.Calcium
|
||||
ret.Sodium += ingr.Quantity() * ingr.Food.Sodium
|
||||
ret.Magnesium += ingr.Quantity() * ingr.Food.Magnesium
|
||||
ret.Phosphorus += ingr.Quantity() * ingr.Food.Phosphorus
|
||||
ret.Iron += ingr.Quantity() * ingr.Food.Iron
|
||||
ret.Zinc += ingr.Quantity() * ingr.Food.Zinc
|
||||
ret.Mass += ingr.Quantity() * ingr.Food.Mass
|
||||
ret.Price += ingr.Quantity() * ingr.Food.Price
|
||||
ret.Cals += ingr.Quantity * ingr.Food.Cals
|
||||
ret.Carbs += ingr.Quantity * ingr.Food.Carbs
|
||||
ret.Protein += ingr.Quantity * ingr.Food.Protein
|
||||
ret.Fat += ingr.Quantity * ingr.Food.Fat
|
||||
ret.Sugar += ingr.Quantity * ingr.Food.Sugar
|
||||
ret.Alcohol += ingr.Quantity * ingr.Food.Alcohol
|
||||
ret.Water += ingr.Quantity * ingr.Food.Water
|
||||
ret.Potassium += ingr.Quantity * ingr.Food.Potassium
|
||||
ret.Calcium += ingr.Quantity * ingr.Food.Calcium
|
||||
ret.Sodium += ingr.Quantity * ingr.Food.Sodium
|
||||
ret.Magnesium += ingr.Quantity * ingr.Food.Magnesium
|
||||
ret.Phosphorus += ingr.Quantity * ingr.Food.Phosphorus
|
||||
ret.Iron += ingr.Quantity * ingr.Food.Iron
|
||||
ret.Zinc += ingr.Quantity * ingr.Food.Zinc
|
||||
ret.Mass += ingr.Quantity * ingr.Food.Mass
|
||||
ret.Price += ingr.Quantity * ingr.Food.Price
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
@ -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}
|
||||
|
||||
recipe := Recipe{Ingredients: []Ingredient{
|
||||
{QuantityNumerator: 1, QuantityDenominator: 1, Food: &f1},
|
||||
{QuantityNumerator: 1, QuantityDenominator: 1, Food: &f2},
|
||||
{Quantity: 1, Food: &f1},
|
||||
{Quantity: 1, Food: &f2},
|
||||
}}
|
||||
computed_food := recipe.ComputeFood()
|
||||
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))
|
||||
|
||||
recipe2 := Recipe{Ingredients: []Ingredient{
|
||||
{QuantityNumerator: 3, QuantityDenominator: 2, Food: &f1},
|
||||
{QuantityNumerator: 1, QuantityDenominator: 2, Food: &f2},
|
||||
{Quantity: 1.5, Food: &f1},
|
||||
{Quantity: 0.5, Food: &f2},
|
||||
}}
|
||||
computed_food2 := recipe2.ComputeFood()
|
||||
assert.Equal(computed_food2.Cals, float32(9.75))
|
||||
|
@ -11,7 +11,7 @@ insert into db_version values(0);
|
||||
|
||||
create table food_types (rowid integer primary key,
|
||||
name text not null unique check(length(name) != 0)
|
||||
);
|
||||
) strict;
|
||||
insert into food_types (name) values
|
||||
('grocery'),
|
||||
('recipe'),
|
||||
@ -51,7 +51,7 @@ create table units (rowid integer primary key,
|
||||
name text not null unique check(length(name) != 0),
|
||||
abbreviation text not null unique check(length(abbreviation) != 0)
|
||||
-- is_metric integer not null check(is_metric in (0, 1))
|
||||
);
|
||||
) strict;
|
||||
insert into units(rowid, name, abbreviation) values
|
||||
-- Count
|
||||
(1, 'count', 'ct'),
|
||||
@ -75,8 +75,7 @@ create table ingredients (rowid integer primary key,
|
||||
recipe_id integer references recipes(rowid),
|
||||
|
||||
-- Portion size (rational numbers)
|
||||
quantity_numerator integer not null default 1,
|
||||
quantity_denominator integer not null default 1,
|
||||
quantity real not null default 1,
|
||||
units integer not null default 0, -- Display purposes only
|
||||
|
||||
in_recipe_id integer references recipes(rowid) on delete cascade not null,
|
||||
|
Loading…
x
Reference in New Issue
Block a user