Add tables for shopping list and planner

This commit is contained in:
Greg Burri 2025-01-21 19:27:06 +01:00
parent 0d3c63013e
commit d9449de02b
8 changed files with 99 additions and 76 deletions

View file

@ -151,19 +151,72 @@ CREATE TABLE [Ingredient] (
[step_id] INTEGER NOT NULL,
[name] TEXT NOT NULL DEFAULT '',
[comment] TEXT NOT NULL DEFAULT '',
[quantity_value] REAL,
[quantity_unit] TEXT NOT NULL DEFAULT '',
[comment] TEXT NOT NULL DEFAULT '',
FOREIGN KEY([step_id]) REFERENCES [Step]([id]) ON DELETE CASCADE
) STRICT;
CREATE INDEX [Ingredient_order_index] ON [Ingredient]([order]);
-- Table not strict because [value] can story any type of data.
CREATE TABLE [Settings] (
[name] TEXT NOT NULL PRIMARY KEY,
[value] TEXT NOT NULL
CREATE TABLE [RecipeScheduled] (
[id] INTEGER PRIMARY KEY,
[user_id] INTEGER NOT NULL,
[recipe_id] INTEGER NOT NULL,
[date] TEXT NOT NULL,
[servings] INTEGER, -- If NULL use [recipe].[servings].
FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE CASCADE,
FOREIGN KEY([recipe_id]) REFERENCES [Recipe]([id]) ON DELETE CASCADE
);
INSERT INTO [Settings] ([name], [value]) VALUES ('new_user_registration_enabled', TRUE);
CREATE TABLE [ShoppingEntry] (
[id] INTEGER PRIMARY KEY,
[user_id] INTEGER NOT NULL,
-- The linkded ingredient can be deleted or a custom entry can be manually added.
-- In both cases [name], [quantity_value] and [quantity_unit] are used to display
-- the entry instead of [Ingredient] data.
[ingredient_id] INTEGER,
[is_checked] INTEGER NOT NULL DEFAULT FALSE,
[name] TEXT NOT NULL DEFAULT '',
[quantity_value] REAL,
[quantity_unit] TEXT NOT NULL DEFAULT '',
[servings] INTEGER,
FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE CASCADE,
FOREIGN KEY([ingredient_id]) REFERENCES [Ingredient]([id]) ON DELETE SET NULL
);
-- When an ingredient is deleted, its values are copied to any shopping entry
-- that referenced it.
CREATE TRIGGER [Ingredient_trigger_delete]
BEFORE DELETE
ON [Ingredient]
BEGIN
UPDATE [ShoppingEntry]
SET
[name] = OLD.[name],
[quantity_value] = OLD.[quantity_value],
[quantity_unit] = OLD.[quantity_unit],
[servings] = (
SELECT [servings]
FROM [Recipe]
INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
INNER JOIN [Step] ON [Step].[group_id] = [Group].[id]
WHERE [Step].[id] = OLD.[step_id]
)
WHERE [ingredient_id] = OLD.[id];
END;
CREATE TABLE [Settings] (
[name] TEXT NOT NULL PRIMARY KEY,
-- Value can by anything that can be read from a text by
-- implementing the trait 'std::str::FromStr'.
[value] TEXT NOT NULL
) STRICT;
INSERT INTO [Settings] ([name], [value]) VALUES ('new_user_registration_enabled', 'true');