From b46012f212a4302b4d1325d8fdf9634e7083e76a Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Fri, 11 Oct 2024 22:47:16 +0200 Subject: First draft of recurrent costs and categories --- main.go | 18 +++++++++++++++ model/categories.sql.go | 46 ++++++++++++++++++++++++++++++++++++++ model/models.go | 2 +- model/rexps.sql.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ sql/categories.sql | 4 ++++ sql/ddl/pgsql.sql | 4 ++-- sql/rexps.sql | 4 ++++ templ/categories.tpl | 9 ++++++++ templ/navlinks.tpl | 2 +- templ/recur.tpl | 9 ++++++++ 10 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 model/categories.sql.go create mode 100644 model/rexps.sql.go create mode 100644 sql/categories.sql create mode 100644 sql/rexps.sql create mode 100644 templ/categories.tpl create mode 100644 templ/recur.tpl diff --git a/main.go b/main.go index 84dcd6e..29c85da 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,8 @@ func main() { // handlers that required authentification authMux.Handle("GET /{$}", indexPage()) + authMux.Handle("GET /recur/{$}", recurPage()) + authMux.Handle("GET /categories/{$}", categoriesPage()) authMux.Handle("GET /", notfound()) log.Fatal(http.ListenAndServe(os.Getenv("GOSTEN_ADDRESS"), handler)) @@ -83,6 +85,22 @@ func indexPage() http.HandlerFunc { } } +func recurPage() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + uid := userId(r) + exps, _ := Q.GetRecurExpenses(r.Context(), uid) + showTemplate(w, "recur", exps) + } +} + +func categoriesPage() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + uid := userId(r) + cats, _ := Q.GetCategories(r.Context(), uid) + showTemplate(w, "categories", cats) + } +} + func notfound() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { showTemplate(w, "404", r.RequestURI) diff --git a/model/categories.sql.go b/model/categories.sql.go new file mode 100644 index 0000000..74eb8a9 --- /dev/null +++ b/model/categories.sql.go @@ -0,0 +1,46 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: categories.sql + +package model + +import ( + "context" +) + +const getCategories = `-- name: GetCategories :many +SELECT id, name + FROM categories + WHERE user_id = $1 +` + +type GetCategoriesRow struct { + ID int32 + Name string +} + +// GetCategories +// +// SELECT id, name +// FROM categories +// WHERE user_id = $1 +func (q *Queries) GetCategories(ctx context.Context, userID int32) ([]GetCategoriesRow, error) { + rows, err := q.db.Query(ctx, getCategories, userID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetCategoriesRow + for rows.Next() { + var i GetCategoriesRow + if err := rows.Scan(&i.ID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/model/models.go b/model/models.go index 2ac5a4b..b2a47dc 100644 --- a/model/models.go +++ b/model/models.go @@ -15,7 +15,7 @@ type Category struct { UserID int32 } -type ConstExpense struct { +type RecurExpense struct { ID int32 Description pgtype.Text Expense pgtype.Numeric diff --git a/model/rexps.sql.go b/model/rexps.sql.go new file mode 100644 index 0000000..9b32c5b --- /dev/null +++ b/model/rexps.sql.go @@ -0,0 +1,59 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: rexps.sql + +package model + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const getRecurExpenses = `-- name: GetRecurExpenses :many +SELECT id, description, expense, duration, start, "end" + FROM recur_expenses + WHERE user_id = $1 +` + +type GetRecurExpensesRow struct { + ID int32 + Description pgtype.Text + Expense pgtype.Numeric + Duration pgtype.Interval + Start pgtype.Date + End pgtype.Date +} + +// GetRecurExpenses +// +// SELECT id, description, expense, duration, start, "end" +// FROM recur_expenses +// WHERE user_id = $1 +func (q *Queries) GetRecurExpenses(ctx context.Context, userID int32) ([]GetRecurExpensesRow, error) { + rows, err := q.db.Query(ctx, getRecurExpenses, userID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetRecurExpensesRow + for rows.Next() { + var i GetRecurExpensesRow + if err := rows.Scan( + &i.ID, + &i.Description, + &i.Expense, + &i.Duration, + &i.Start, + &i.End, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/sql/categories.sql b/sql/categories.sql new file mode 100644 index 0000000..2c8e5c7 --- /dev/null +++ b/sql/categories.sql @@ -0,0 +1,4 @@ +-- name: GetCategories :many +SELECT id, name + FROM categories + WHERE user_id = $1; \ No newline at end of file diff --git a/sql/ddl/pgsql.sql b/sql/ddl/pgsql.sql index 09c7133..18dc388 100644 --- a/sql/ddl/pgsql.sql +++ b/sql/ddl/pgsql.sql @@ -19,14 +19,14 @@ CREATE INDEX user_id ON categories (user_id); -CREATE TABLE const_expenses ( +CREATE TABLE recur_expenses ( id SERIAL PRIMARY KEY, description TEXT, expense NUMERIC(10, 2) NOT NULL, duration INTERVAL NOT NULL, start DATE NOT NULL, "end" DATE NOT NULL, - prev_id INTEGER REFERENCES const_expenses, + prev_id INTEGER REFERENCES recur_expenses, category_id INTEGER NOT NULL REFERENCES categories, user_id INTEGER NOT NULL REFERENCES users ); diff --git a/sql/rexps.sql b/sql/rexps.sql new file mode 100644 index 0000000..87dc23f --- /dev/null +++ b/sql/rexps.sql @@ -0,0 +1,4 @@ +-- name: GetRecurExpenses :many +SELECT id, description, expense, duration, start, "end" + FROM recur_expenses + WHERE user_id = $1; \ No newline at end of file diff --git a/templ/categories.tpl b/templ/categories.tpl new file mode 100644 index 0000000..662e1e1 --- /dev/null +++ b/templ/categories.tpl @@ -0,0 +1,9 @@ +{{define "main"}} + +{{end}} \ No newline at end of file diff --git a/templ/navlinks.tpl b/templ/navlinks.tpl index 0d71d7e..cdd54d7 100644 --- a/templ/navlinks.tpl +++ b/templ/navlinks.tpl @@ -3,6 +3,6 @@ Kategorien {{end}} \ No newline at end of file diff --git a/templ/recur.tpl b/templ/recur.tpl new file mode 100644 index 0000000..316b768 --- /dev/null +++ b/templ/recur.tpl @@ -0,0 +1,9 @@ +{{define "main"}} + +{{end}} \ No newline at end of file -- cgit v1.2.3-70-g09d2