From 7a921d1965934ad66bbe00204a6514f273bf91b8 Mon Sep 17 00:00:00 2001 From: ESteanes <19161310+ESteanes@users.noreply.github.com> Date: Sat, 31 Aug 2024 19:31:02 +1000 Subject: [PATCH] Creating some basic templates (#8) * Adding missing description to api spec * Renaming directory & removing unused templ templates * transactions handler and template * accounts template * Moving components template into new directory * cleaning up openapi.json * cleanup * newline * formatting --- datafetcher/handlers/accounts.go | 9 +- datafetcher/handlers/transactions.go | 14 +- datafetcher/server.go | 5 +- datafetcher/templ/hello.templ | 5 - datafetcher/templ/hello_templ.go | 48 ----- datafetcher/templates/accounts.templ | 31 ++++ datafetcher/templates/accounts_templ.go | 118 ++++++++++++ .../{templ => templates}/components.templ | 2 +- .../{templ => templates}/components_templ.go | 12 +- datafetcher/templates/transactions.templ | 85 +++++++++ datafetcher/templates/transactions_templ.go | 170 ++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 13 files changed, 425 insertions(+), 80 deletions(-) delete mode 100644 datafetcher/templ/hello.templ delete mode 100644 datafetcher/templ/hello_templ.go create mode 100644 datafetcher/templates/accounts.templ create mode 100644 datafetcher/templates/accounts_templ.go rename datafetcher/{templ => templates}/components.templ (96%) rename datafetcher/{templ => templates}/components_templ.go (96%) create mode 100644 datafetcher/templates/transactions.templ create mode 100644 datafetcher/templates/transactions_templ.go diff --git a/datafetcher/handlers/accounts.go b/datafetcher/handlers/accounts.go index 5facf25..6b8b884 100644 --- a/datafetcher/handlers/accounts.go +++ b/datafetcher/handlers/accounts.go @@ -6,6 +6,7 @@ import ( "log" "net/http" + "github.com/esteanes/expense-manager/datafetcher/templates" "github.com/esteanes/expense-manager/datafetcher/upclient" ) @@ -28,15 +29,15 @@ func NewAccountHandler(log *log.Logger, upclient *upclient.APIClient, auth conte func (h *AccountHandler) Post(w http.ResponseWriter, r *http.Request) {} func (h *AccountHandler) Get(w http.ResponseWriter, r *http.Request) { pageSize := int32(30) - filterAccountType := upclient.AccountTypeEnum("SAVER") filterOwnershipType := upclient.OwnershipTypeEnum("INDIVIDUAL") - resp, r2, err := h.UpClient.AccountsAPI.AccountsGet(h.UpAuth).PageSize(pageSize).FilterAccountType(filterAccountType).FilterOwnershipType(filterOwnershipType).Execute() - + resp, r2, err := h.UpClient.AccountsAPI.AccountsGet(h.UpAuth).PageSize(pageSize).FilterOwnershipType(filterOwnershipType).Execute() + h.Log.Println(resp) if err != nil { fmt.Fprintf(w, "Error when calling `AccountsAPI.AccountsGet``: %v\n", err) fmt.Fprintf(w, "Full HTTP response: %v\n", r2) h.Log.Println("Unable to get account information") } - fmt.Fprintf(w, "Response from `AccountsAPI.AccountsGet`: %v\n", resp) + // fmt.Fprintf(w, "Response from `AccountsAPI.AccountsGet`: %v\n", resp) + templates.Accounts(resp.Data).Render(r.Context(), w) } diff --git a/datafetcher/handlers/transactions.go b/datafetcher/handlers/transactions.go index 5c980c7..f7e0deb 100644 --- a/datafetcher/handlers/transactions.go +++ b/datafetcher/handlers/transactions.go @@ -2,11 +2,12 @@ package handlers import ( "context" - "encoding/json" "fmt" "log" "net/http" + "github.com/esteanes/expense-manager/datafetcher/templates" + "github.com/esteanes/expense-manager/datafetcher/upclient" ) @@ -29,19 +30,12 @@ func NewTransactionHandler(log *log.Logger, upclient *upclient.APIClient, auth c func (h *TransactionsHandler) Post(w http.ResponseWriter, r *http.Request) {} func (h *TransactionsHandler) Get(w http.ResponseWriter, r *http.Request) { pageSize := int32(30) // int32 | The number of records to return in each page. (optional) - resp2, r2, err := h.UpClient.TransactionsAPI.TransactionsGet(h.UpAuth).PageSize(pageSize).Execute() + resp, r2, err := h.UpClient.TransactionsAPI.TransactionsGet(h.UpAuth).PageSize(pageSize).Execute() if err != nil { fmt.Fprintf(w, "Error when calling `TransactionsAPI.TransactionsGet``: %v\n", err) fmt.Fprintf(w, "Full HTTP response: %v\n", r2.Body) h.Log.Println(r2.Body) } // response from `TransactionsGet`: ListTransactionsResponse - fmt.Fprintf(w, "Response from `TransactionsAPI.TransactionsGet`: %v\n", resp2) - jsonString := `{"account":{"data":{"type":"accounts","id":"a90b55ad-1bcb-4e75-b407-0e0e1e5c8a6d"},"links":{"related":"https://api.up.com.au/api/v1/accounts/a90b55ad-1bcb-4e75-b407-0e0e1e5c8a6d"}},"transferAccount":{"data":null},"category":{"data":{"type":"categories","id":"public-transport"},"links":{"self":"https://api.up.com.au/api/v1/transactions/e33e57c4-efa9-496d-83bd-e1f8c0b9651c/relationships/category","related":"https://api.up.com.au/api/v1/categories/public-transport"}},"parentCategory":{"data":{"type":"categories","id":"transport"},"links":{"related":"https://api.up.com.au/api/v1/categories/transport"}},"tags":{"data":[],"links":{"self":"https://api.up.com.au/api/v1/transactions/e33e57c4-efa9-496d-83bd-e1f8c0b9651c/relationships/tags"}},"attachment":{"data":null}}` - var manualResponse upclient.TransactionResourceRelationships - err = json.Unmarshal([]byte(jsonString), &manualResponse) - fmt.Fprintf(w, "Manual json unmarshalling: %v\n", manualResponse) - if err != nil { - fmt.Fprintf(w, "error unmarshalling: %s\n", err) - } + templates.Transactions(resp.Data).Render(r.Context(), w) } diff --git a/datafetcher/server.go b/datafetcher/server.go index 894d499..6524938 100644 --- a/datafetcher/server.go +++ b/datafetcher/server.go @@ -7,9 +7,8 @@ import ( "net/http" "time" - "github.com/a-h/templ" "github.com/esteanes/expense-manager/datafetcher/handlers" - templates "github.com/esteanes/expense-manager/datafetcher/templ" + "github.com/esteanes/expense-manager/datafetcher/templates" "github.com/esteanes/expense-manager/datafetcher/upclient" "github.com/alexedwards/scs/v2" @@ -88,7 +87,6 @@ func HandleRequests(upBankToken string, log *log.Logger) { // Creating individual handlers accountHandler := handlers.NewAccountHandler(log, apiClient, auth) transactionsHandler := handlers.NewTransactionHandler(log, apiClient, auth) - component := templates.Hello("its ya boi") mux := http.NewServeMux() mux.HandleFunc(accountHandler.Uri, accountHandler.ServeHTTP) mux.HandleFunc(transactionsHandler.Uri, transactionsHandler.ServeHTTP) @@ -96,7 +94,6 @@ func HandleRequests(upBankToken string, log *log.Logger) { mux.HandleFunc("/", homePage) mux.HandleFunc("/info", getInfo) mux.Handle("/time", NewNowHandler(time.Now)) - mux.Handle("/hello", templ.Handler(component)) mux.HandleFunc("/counter", handleInfo) log.Println("Serving request at localhost:8080") muxWithSessionMiddleware := sessionManager.LoadAndSave(mux) diff --git a/datafetcher/templ/hello.templ b/datafetcher/templ/hello.templ deleted file mode 100644 index 6f6255e..0000000 --- a/datafetcher/templ/hello.templ +++ /dev/null @@ -1,5 +0,0 @@ -package templ - -templ Hello(name string) { -
Account Type: { strings.ToTitle(string(account.Attributes.AccountType)) }
+Balance: { account.Attributes.Balance.CurrencyCode } { account.Attributes.Balance.Value }
+Created At: { account.Attributes.CreatedAt.String() }
+Account Type: ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(strings.ToTitle(string(account.Attributes.AccountType))) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/accounts.templ`, Line: 23, Col: 81} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Balance: ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(account.Attributes.Balance.CurrencyCode) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/accounts.templ`, Line: 24, Col: 60} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(account.Attributes.Balance.Value) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/accounts.templ`, Line: 24, Col: 95} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Created At: ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(account.Attributes.CreatedAt.String()) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/accounts.templ`, Line: 25, Col: 61} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Transaction Amount | +Description | +Status | +Time | +Message | +Purchase Method | +Card Number | +
---|---|---|---|---|---|---|
{ transaction.Attributes.Amount.Value } | +{ transaction.Attributes.Description } | +{ string(transaction.Attributes.Status) } | +{ transaction.Attributes.CreatedAt.String() } | +{ getMessage(transaction) } | +{ getCardPurchase(transaction) } | +{ getCardPurchaseNumber(transaction) } | +
Transaction Amount | Description | Status | Time | Message | Purchase Method | Card Number |
---|---|---|---|---|---|---|
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var2 string + templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(transaction.Attributes.Amount.Value) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 76, Col: 56} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(transaction.Attributes.Description) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 77, Col: 55} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(string(transaction.Attributes.Status)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 78, Col: 58} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(transaction.Attributes.CreatedAt.String()) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 79, Col: 62} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(getMessage(transaction)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 80, Col: 44} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(getCardPurchase(transaction)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 81, Col: 49} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var8 string + templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(getCardPurchaseNumber(transaction)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `datafetcher/templates/transactions.templ`, Line: 82, Col: 55} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" |