From dac36db2848770b026ef1f75a00d49022b339b50 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Sat, 19 Oct 2024 13:28:02 +0200 Subject: [PATCH] db: add more tables --- internal/howmuch/adapter/repo/sqlc/models.go | 22 +++++++++++++++++++ internal/howmuch/model/expense.go | 17 +++++++------- internal/howmuch/model/transaction.go | 13 ++++++++--- ...create_transaction_table.postgres.down.sql | 2 ++ ...4_create_transaction_table.postgres.up.sql | 14 ++++++++++++ ...255_create_expense_table.postgres.down.sql | 1 + ...12255_create_expense_table.postgres.up.sql | 14 ++++++++++++ ...1_fk_transaction_expense.postgres.down.sql | 0 ...421_fk_transaction_expense.postgres.up.sql | 2 ++ 9 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 migrations/20241019110734_create_transaction_table.postgres.down.sql create mode 100644 migrations/20241019110734_create_transaction_table.postgres.up.sql create mode 100644 migrations/20241019112255_create_expense_table.postgres.down.sql create mode 100644 migrations/20241019112255_create_expense_table.postgres.up.sql create mode 100644 migrations/20241019112421_fk_transaction_expense.postgres.down.sql create mode 100644 migrations/20241019112421_fk_transaction_expense.postgres.up.sql diff --git a/internal/howmuch/adapter/repo/sqlc/models.go b/internal/howmuch/adapter/repo/sqlc/models.go index 8720c29..23c941b 100644 --- a/internal/howmuch/adapter/repo/sqlc/models.go +++ b/internal/howmuch/adapter/repo/sqlc/models.go @@ -27,6 +27,17 @@ type Event struct { TotalAmount sql.NullInt32 } +type Expense struct { + ID int32 + CreatedAt time.Time + UpdatedAt time.Time + Amount int32 + Currency string + EventID int32 + Name sql.NullInt32 + Place sql.NullInt32 +} + type Participation struct { ID int32 UserID int32 @@ -36,6 +47,17 @@ type Participation struct { UpdatedAt time.Time } +type Transaction struct { + ID int32 + ExpenseID int32 + UserID int32 + Amount int32 + Currency string + IsIncome bool + CreatedAt time.Time + UpdatedAt time.Time +} + type User struct { ID int32 Email string diff --git a/internal/howmuch/model/expense.go b/internal/howmuch/model/expense.go index 1053f29..3988cd5 100644 --- a/internal/howmuch/model/expense.go +++ b/internal/howmuch/model/expense.go @@ -32,8 +32,12 @@ type ExpenseRequest struct { Detail ExpenseDetail `json:"detail"` } +// {{{ Entity + type ExpenseEntity struct { - ID int + ID int + CreatedAt time.Time + UpdatedAt time.Time Amount int Currency string @@ -42,9 +46,6 @@ type ExpenseEntity struct { // ExpenseDetail Name string Place string - - CreatedAt time.Time - UpdatedAt time.Time } type ExpenseDetail struct { @@ -52,8 +53,11 @@ type ExpenseDetail struct { Place string `json:"place"` } +// }}} type Expense struct { - ID int + ID int + CreatedAt time.Time + UpdatedAt time.Time Amount Money @@ -65,7 +69,4 @@ type Expense struct { EventID int Detail ExpenseDetail - - CreatedAt time.Time - UpdatedAt time.Time } diff --git a/internal/howmuch/model/transaction.go b/internal/howmuch/model/transaction.go index f49d493..9b95276 100644 --- a/internal/howmuch/model/transaction.go +++ b/internal/howmuch/model/transaction.go @@ -24,18 +24,25 @@ package model import "time" +// {{{ Entity + type TransactionEntity Transaction -// Transaction is the association between Expenses and Users +// }}} +// {{{ Domain object + type Transaction struct { ID int - ExpenseID Expense + ExpenseID int UserID int Amount int Currency string - IsIncome bool + IsIncome bool // To note that the direction of the money (payment or income) CreatedAt time.Time UpdatedAt time.Time } + +// }}} +// Transaction is the association between Expenses and Users diff --git a/migrations/20241019110734_create_transaction_table.postgres.down.sql b/migrations/20241019110734_create_transaction_table.postgres.down.sql new file mode 100644 index 0000000..1e980ff --- /dev/null +++ b/migrations/20241019110734_create_transaction_table.postgres.down.sql @@ -0,0 +1,2 @@ +CREATE TABLE "transaction"; + diff --git a/migrations/20241019110734_create_transaction_table.postgres.up.sql b/migrations/20241019110734_create_transaction_table.postgres.up.sql new file mode 100644 index 0000000..e6d784d --- /dev/null +++ b/migrations/20241019110734_create_transaction_table.postgres.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE "transaction" ( + "id" serial NOT NULL, + PRIMARY KEY ("id"), + "expense_id" integer NOT NULL, + "user_id" integer NOT NULL, + "amount" integer NOT NULL, + "currency" character varying(255) NOT NULL, + "is_income" boolean NOT NULL DEFAULT FALSE, + "created_at" date NOT NULL, + "updated_at" date NOT NULL +); + +ALTER TABLE "transaction" +ADD FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/migrations/20241019112255_create_expense_table.postgres.down.sql b/migrations/20241019112255_create_expense_table.postgres.down.sql new file mode 100644 index 0000000..54ebe9b --- /dev/null +++ b/migrations/20241019112255_create_expense_table.postgres.down.sql @@ -0,0 +1 @@ +DROP TABLE "expense"; diff --git a/migrations/20241019112255_create_expense_table.postgres.up.sql b/migrations/20241019112255_create_expense_table.postgres.up.sql new file mode 100644 index 0000000..612a4bf --- /dev/null +++ b/migrations/20241019112255_create_expense_table.postgres.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE "expense" ( + "id" serial NOT NULL, + PRIMARY KEY ("id"), + "created_at" date NOT NULL, + "updated_at" date NOT NULL, + "amount" integer NOT NULL, + "currency" character varying NOT NULL, + "event_id" integer NOT NULL, + "name" integer NULL, + "place" integer NULL +); + +ALTER TABLE "expense" +ADD FOREIGN KEY ("event_id") REFERENCES "event" ("id") ON DELETE CASCADE ON UPDATE CASCADE diff --git a/migrations/20241019112421_fk_transaction_expense.postgres.down.sql b/migrations/20241019112421_fk_transaction_expense.postgres.down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/20241019112421_fk_transaction_expense.postgres.up.sql b/migrations/20241019112421_fk_transaction_expense.postgres.up.sql new file mode 100644 index 0000000..c57294f --- /dev/null +++ b/migrations/20241019112421_fk_transaction_expense.postgres.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE "transaction" +ADD FOREIGN KEY ("expense_id") REFERENCES "expense" ("id") ON DELETE CASCADE ON UPDATE CASCADE