Capstone: A Persistent Lending Tool · practice
Return an Active Loan
A return must change an active loan exactly once. If the ID is missing or already returned, the database boundary reports False; the
After its successful checkout, the demo calls:
main(["return", str(database_path), "501", "2026-08-27"])
Add return_command(args). Argparse passes it args.database, integer args.loan_id, and args.returned_on. Require the database path first. Trim the label with trimmed(args.returned_on, "RETURNED_ON"), but keep it as text. A
Prepare the existing database, open it with open_database, and call:
changed = return_loan(connection, args.loan_id, returned_on)
Close the application connection in finally. return_loan updates only a row whose loan ID matches and whose returned_on is still SQL NULL. It commits the new label and returns True for that one change. A missing or already-returned ID produces False and does not alter another row.
Inspect changed only after finally has closed the connection. If it is false, pass ValueError("No active loan has that ID.") to report_error. That writes Error: No active loan has that ID. to stderr and returns 1, with nothing on stdout. An unrelated return_loan must still propagate after the connection closes.
If changed is true, print Loan returned: ID only after the close and return 0. Add the exact branch if args.command == "return": return return_command(args) in main. Do not issue an UPDATE in lending.py, search by item ID, or catch an unrelated programming or database error.
Press Run to see the result after a fresh connection reopens the file:
== return ==
Status: 0
stdout: Loan returned: 501
stderr: <empty>
Returned label after reopening: 2026-08-27
A thorough check returns one active loan, repeats the command with a different label, and tries an absent ID. The second call must leave the first label untouched. It also uses whitespace and SQL-looking text, checks missing-path behavior, and confirms the application connection closes on both success and refusal.
Task
Add or complete return_command(args) and add this branch in main:
if args.command == "return":
return return_command(args)
Refuse a missing database, trim RETURNED_ON, prepare and open the catalog, and call return_loan once. Close the application connection in finally. If the result is false, report No active loan has that ID. with status 1; otherwise print Loan returned: ID and return 0.