Skip to content

Commit

Permalink
Merge pull request #28 from eccentricOrange:eccentricOrange/issue27
Browse files Browse the repository at this point in the history
Finish editing schema
  • Loading branch information
eccentricOrange authored May 14, 2022
2 parents 9d6eafb + 533bead commit 2ca77a2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 111 deletions.
16 changes: 3 additions & 13 deletions data/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@ CREATE TABLE IF NOT EXISTS papers (
CONSTRAINT unique_paper_name UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS papers_days (
CREATE TABLE IF NOT EXISTS cost_and_delivery_data (
paper_day_id INTEGER PRIMARY KEY AUTOINCREMENT,
paper_id INTEGER NOT NULL REFERENCES papers(paper_id),
day_id INTEGER NOT NULL,
cost REAL NOT NULL,
delivered INTEGER NOT NULL,
CONSTRAINT unique_paper_day UNIQUE (paper_id, day_id)
);

CREATE TABLE IF NOT EXISTS papers_days_delivered (
papers_days_delivered_id INTEGER PRIMARY KEY AUTOINCREMENT,
paper_day_id INTEGER NOT NULL REFERENCES papers_days(paper_day_id),
delivered INTEGER NOT NULL CHECK (delivered IN (0, 1))
);

CREATE TABLE IF NOT EXISTS papers_days_cost (
papers_days_cost_id INTEGER PRIMARY KEY AUTOINCREMENT,
paper_day_id INTEGER NOT NULL REFERENCES papers_days(paper_day_id),
cost REAL
);

CREATE TABLE IF NOT EXISTS undelivered_strings (
string_id INTEGER PRIMARY KEY AUTOINCREMENT,
year INTEGER NOT NULL CHECK (year >= 0),
Expand Down
38 changes: 22 additions & 16 deletions data/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ VALUES
('paper2'),
('paper3');

INSERT INTO papers_days (paper_id, day_id)
INSERT INTO cost_and_delivery_data (paper_id, day_id, cost, delivered)
VALUES
(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
(3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6);

INSERT INTO papers_days_delivered (paper_day_id, delivered)
VALUES
(01, 0), (02, 1), (03, 0), (04, 0), (05, 0), (06, 1), (07, 1),
(08, 0), (09, 0), (10, 0), (11, 0), (12, 1), (13, 0), (14, 1),
(15, 1), (16, 1), (17, 0), (18, 0), (19, 1), (20, 1), (21, 1);

INSERT INTO papers_days_cost (paper_day_id, cost)
VALUES
(01, 0.0), (02, 6.4), (03, 0.0), (04, 0.0), (05, 0.0), (06, 7.9), (07, 4.0),
(08, 0.0), (09, 0.0), (10, 0.0), (11, 0.0), (12, 3.4), (13, 0.0), (14, 8.4),
(15, 2.4), (16, 4.6), (17, 0.0), (18, 0.0), (19, 3.4), (20, 4.6), (21, 6.0);
(1, 0, 0.0, 0),
(1, 1, 6.4, 1),
(1, 2, 0.0, 0),
(1, 3, 0.0, 0),
(1, 4, 0.0, 0),
(1, 5, 7.9, 1),
(1, 6, 4.0, 1),
(2, 0, 0.0, 0),
(2, 1, 0.0, 0),
(2, 2, 0.0, 0),
(2, 3, 0.0, 0),
(2, 4, 3.4, 1),
(2, 5, 0.0, 0),
(2, 6, 8.4, 1),
(3, 0, 2.4, 1),
(3, 1, 4.6, 1),
(3, 2, 0.0, 0),
(3, 3, 0.0, 0),
(3, 4, 3.4, 1),
(3, 5, 4.6, 1),
(3, 6, 6.0, 1);

INSERT INTO undelivered_strings (year, month, paper_id, string)
VALUES
Expand Down
1 change: 0 additions & 1 deletion npbc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ def addudl(args: ArgNamespace) -> None:

# attempt to add the strings to the database
try:
print(f"{month=} {year=} {args.paperid=} {args.strings=}")
npbc_core.add_undelivered_string(month, year, args.paperid, *args.strings)

# if the paper doesn't exist, print an error message
Expand Down
112 changes: 31 additions & 81 deletions npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,15 @@ def get_cost_and_delivery_data(paper_id: int, connection: Connection) -> list[tu
"""get the cost and delivery data for a given paper from the DB"""

query = """
SELECT papers_days_delivered.delivered, papers_days_cost.cost
FROM papers_days
INNER JOIN papers_days_delivered
ON papers_days.paper_day_id = papers_days_delivered.paper_day_id
INNER JOIN papers_days_cost
ON papers_days.paper_day_id = papers_days_cost.paper_day_id
WHERE papers_days.paper_id = ?
ORDER BY papers_days.day_id;
SELECT delivered, cost FROM cost_and_delivery_data
WHERE paper_id = ?
ORDER BY day_id;
"""

return [
(bool(delivered), cost)
for delivered, cost in connection.execute(query, (paper_id, )).fetchall()
]
return list(map(
lambda row: (bool(row[0]), row[1]),
connection.execute(query, (paper_id,)).fetchall()
))


def calculate_cost_of_one_paper(
Expand Down Expand Up @@ -370,7 +365,7 @@ def format_output(costs: dict[int, float], total: float, month: int, year: int)
yield f"For {date_type(year=year, month=month, day=1).strftime(r'%B %Y')},\n"

# output the total cost of all papers
yield f"*TOTAL*: {total}"
yield f"*TOTAL*: {total:.2f}"

# output the cost of each paper with its name
with connect(DATABASE_PATH) as connection:
Expand All @@ -380,7 +375,7 @@ def format_output(costs: dict[int, float], total: float, month: int, year: int)
}

for paper_id, cost in costs.items():
yield f"{papers[paper_id]}: {cost}"
yield f"{papers[paper_id]}: {cost:.2f}"

connection.close()

Expand All @@ -405,27 +400,11 @@ def add_new_paper(name: str, days_delivered: list[bool], days_cost: list[float])
(name,)
).fetchone()[0]

# create days for the paper
paper_days = {
day_id: connection.execute(
"INSERT INTO papers_days (paper_id, day_id) VALUES (?, ?) RETURNING papers_days.paper_day_id;",
(paper_id, day_id)
).fetchone()[0]
for day_id, _ in enumerate(days_delivered)
}

# create cost entries for each day
for day_id, cost in enumerate(days_cost):
connection.execute(
"INSERT INTO papers_days_cost (paper_day_id, cost) VALUES (?, ?);",
(paper_days[day_id], cost)
)

# create delivered entries for each day
for day_id, delivered in enumerate(days_delivered):
# create cost and delivered entries for each day
for day_id, (delivered, cost) in enumerate(zip(days_delivered, days_cost)):
connection.execute(
"INSERT INTO papers_days_delivered (paper_day_id, delivered) VALUES (?, ?);",
(paper_days[day_id], delivered)
"INSERT INTO cost_and_delivery_data (paper_id, day_id, delivered, cost) VALUES (?, ?, ?, ?);",
(paper_id, day_id, delivered, cost)
)

connection.close()
Expand Down Expand Up @@ -457,31 +436,21 @@ def edit_existing_paper(
(name, paper_id)
)

# get the days for the paper
if (days_delivered is not None) or (days_cost is not None):
paper_days = {
row[0]: row[1]
for row in connection.execute(
"SELECT day_id, paper_day_id FROM papers_days WHERE paper_id = ?;",
(paper_id,)
# update the costs of each day
if days_cost is not None:
for day_id, cost in enumerate(days_cost):
connection.execute(
"UPDATE cost_and_delivery_data SET cost = ? WHERE paper_id = ? AND day_id = ?;",
(cost, paper_id, day_id)
)

# update the delivered status of each day
if days_delivered is not None:
for day_id, delivered in enumerate(days_delivered):
connection.execute(
"UPDATE cost_and_delivery_data SET delivered = ? WHERE paper_id = ? AND day_id = ?;",
(delivered, paper_id, day_id)
)
}

# update the delivered data for the paper
if days_delivered is not None:
for day_id, delivered in enumerate(days_delivered):
connection.execute(
"UPDATE papers_days_delivered SET delivered = ? WHERE paper_day_id = ?;",
(delivered, paper_days[day_id])
)

# update the days for the paper
if days_cost is not None:
for day_id, cost in enumerate(days_cost):
connection.execute(
"UPDATE papers_days_cost SET cost = ? WHERE paper_day_id = ?;",
(cost, paper_days[day_id])
)

connection.close()

Expand All @@ -506,27 +475,9 @@ def delete_existing_paper(paper_id: int) -> None:
(paper_id,)
)

# get the days for the paper
paper_days = [
row[0]
for row in connection.execute("SELECT paper_day_id FROM papers_days WHERE paper_id = ?;", (paper_id,))
]

# delete the costs and delivery data for the paper
for paper_day_id in paper_days:
connection.execute(
"DELETE FROM papers_days_cost WHERE paper_day_id = ?;",
(paper_day_id,)
)

connection.execute(
"DELETE FROM papers_days_delivered WHERE paper_day_id = ?;",
(paper_day_id,)
)

# delete the days for the paper
connection.execute(
"DELETE FROM papers_days WHERE paper_id = ?;",
"DELETE FROM cost_and_delivery_data WHERE paper_id = ?;",
(paper_id,)
)

Expand Down Expand Up @@ -658,11 +609,10 @@ def get_papers() -> list[tuple[int, str, int, int, float]]:
raw_data = []

query = """
SELECT papers.paper_id, papers.name, papers_days.day_id, papers_days_delivered.delivered, papers_days_cost.cost
SELECT papers.paper_id, papers.name, cost_and_delivery_data.day_id, cost_and_delivery_data.delivered, cost_and_delivery_data.cost
FROM papers
INNER JOIN papers_days ON papers.paper_id = papers_days.paper_id
INNER JOIN papers_days_delivered ON papers_days.paper_day_id = papers_days_delivered.paper_day_id
INNER JOIN papers_days_cost ON papers_days.paper_day_id = papers_days_cost.paper_day_id;
INNER JOIN cost_and_delivery_data ON papers.paper_id = cost_and_delivery_data.paper_id
ORDER BY papers.paper_id, cost_and_delivery_data.day_id;
"""

with connect(DATABASE_PATH) as connection:
Expand Down

0 comments on commit 2ca77a2

Please sign in to comment.