New example: splitwise

This commit is contained in:
lovasoa
2023-07-02 23:09:21 +02:00
parent 2d70e6862d
commit 99be22dae9
7 changed files with 126 additions and 0 deletions
+7
View File
@@ -110,6 +110,13 @@ to the user's browser.
- Create a file called index.sql with the contents from [this example](./index.sql)
- Open https://localhost:8080 in your browser
## Examples
- [Tiny splitwise clone](./examples/splitwise): a shared expense tracker app
- [Corporate Conundrum](./examples/corporate-conundrum/): a board game implemented in SQL
- [SQLPage's own official website and documentation](./examples/official-site/): the website you are currently reading
## Configuration
SQLPage can be configured through either a configuration file placed in `sqlpage/sqlpage.json`
Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

+6
View File
@@ -0,0 +1,6 @@
# SQL-only expense tracker app
This is a small web application that allows you to track shared expenses with your friends.
It is built entirely in SQL using SQLPage.
![screenshot](../../docs/example-splitwise.png)
+48
View File
@@ -0,0 +1,48 @@
-- Write the name of the group in the title of the page
SELECT 'title' as component, name as contents FROM expense_group WHERE id = $id;
-- Handle the form to add a member to the group (we do it at the top of the page to see it right away)
INSERT INTO group_member(group_id, name)
SELECT $id, :new_member_name WHERE :new_member_name IS NOT NULL;
-- List of members of the group
SELECT 'list' as component, 'Membres' as title;
SELECT name AS title FROM group_member WHERE group_id = $id;
-- Form to add a new member to the group
SELECT 'form' as component, 'Ajouter un membre au groupe' as validate;
SELECT 'Nom du membre' AS 'label', 'new_member_name' AS name;
SELECT 'title' as component, 'Dépenses' as contents
-- Form to add an expense
SELECT 'form' as component, 'Ajouter une dépense' as title, 'Ajouter' as validate;
SELECT 'Description' AS name;
SELECT 'Montant' AS name, 'number' AS type;
SELECT 'Dépensé par' AS name, 'select' as type,
json_group_array(json_object("label", name, "value", id)) as options
FROM group_member WHERE group_id = $id;
-- Insert the expense posted by the form into the database
INSERT INTO expense(spent_by, name, amount)
SELECT :"Dépensé par", :Description, :Montant WHERE :Montant IS NOT NULL;
-- List of expenses of the group
SELECT 'card' as component, 'Dépenses' as title;
SELECT expense.name as title,
'Par ' || group_member.name || ', le ' || expense.date as description,
expense.amount || '' as footer,
CASE
WHEN expense.amount > 100 THEN 'red'
WHEN expense.amount > 50 THEN 'orange'
ELSE 'blue'
END AS color
FROM expense
INNER JOIN group_member on expense.spent_by = group_member.id
WHERE group_member.group_id = $id;
-- Show the positive and negative debts of each member
SELECT 'chart' AS component, 'Dette par personne' AS title, 'bar' AS type, TRUE AS horizontal;
SELECT member_name AS label, is_owed AS value FROM individual_debts
WHERE group_id = $id ORDER BY is_owed DESC;
+19
View File
@@ -0,0 +1,19 @@
-- Simple form to create a shared expense account
SELECT 'form' as component,
'Nouveau compte partagé' as title,
'Créer le compte de dépenses partagé !' as validate;
SELECT 'Nom du compte' AS label,
'shared_expense_name' AS name;
-- Insert the shared expense account posted by the form into the database
INSERT INTO expense_group(name)
SELECT :shared_expense_name
WHERE :shared_expense_name IS NOT NULL;
-- List of shared expense accounts
-- (we put it after the insertion because we want to see new accounts right away when they are created)
SELECT 'list' as component;
SELECT name AS title,
'group.sql?id=' || id AS link
FROM expense_group;
@@ -0,0 +1,19 @@
CREATE TABLE expense_group(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
CREATE TABLE group_member(
id INTEGER PRIMARY KEY AUTOINCREMENT,
group_id INTEGER REFERENCES expense_group(id),
name TEXT
);
CREATE TABLE expense(
id INTEGER PRIMARY KEY AUTOINCREMENT,
spent_by INTEGER REFERENCES group_member(id),
-- identifiant du membre qui a fait la dépense
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- date et heure de la dépense
name TEXT,
-- intitulé
amount DECIMAL -- montant en euros
);
@@ -0,0 +1,27 @@
-- A view that shows all the expenses of a group, with at least one line per member
DROP VIEW IF EXISTS members_with_expenses;
CREATE VIEW members_with_expenses AS
SELECT group_member.group_id AS group_id,
group_member.id AS spent_by_id,
COALESCE(expense.amount, 0) AS amount,
group_member.name AS spent_by_name
FROM group_member
LEFT JOIN expense on expense.spent_by = group_member.id;
-- A view that shows the total amount of expense per person of a group
DROP VIEW IF EXISTS average_debt_per_person;
CREATE VIEW average_debt_per_person AS
SELECT group_id,
sum(amount) / count(distinct spent_by_id) AS debt
FROM members_with_expenses
GROUP BY group_id;
-- A view that shows the total amount a person is owed in a group
DROP VIEW IF EXISTS individual_debts;
CREATE VIEW individual_debts AS
SELECT members_with_expenses.group_id AS group_id,
spent_by_id AS member_id,
spent_by_name AS member_name,
sum(members_with_expenses.amount) - average_debt_per_person.debt AS is_owed
FROM members_with_expenses
INNER JOIN average_debt_per_person ON average_debt_per_person.group_id = members_with_expenses.group_id
GROUP BY spent_by_id
ORDER BY is_owed DESC;