Skip to content

Commit

Permalink
Creating some basic templates (#8)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ESteanes authored Aug 31, 2024
1 parent 1122d02 commit 7a921d1
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 80 deletions.
9 changes: 5 additions & 4 deletions datafetcher/handlers/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"

"github.com/esteanes/expense-manager/datafetcher/templates"
"github.com/esteanes/expense-manager/datafetcher/upclient"
)

Expand All @@ -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)
}
14 changes: 4 additions & 10 deletions datafetcher/handlers/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
5 changes: 1 addition & 4 deletions datafetcher/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -88,15 +87,13 @@ 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)

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)
Expand Down
5 changes: 0 additions & 5 deletions datafetcher/templ/hello.templ

This file was deleted.

48 changes: 0 additions & 48 deletions datafetcher/templ/hello_templ.go

This file was deleted.

31 changes: 31 additions & 0 deletions datafetcher/templates/accounts.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package templates

import (
"github.com/esteanes/expense-manager/datafetcher/upclient"
"strings"
)

templ Accounts(accounts []upclient.AccountResource) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Accounts</title>
</head>
<body>
<h1>Accounts</h1>
<ul>
for _, account := range accounts {
<div>
<h2>{ account.Attributes.DisplayName }</h2>
<p>Account Type: { strings.ToTitle(string(account.Attributes.AccountType)) }</p>
<p>Balance: { account.Attributes.Balance.CurrencyCode } { account.Attributes.Balance.Value }</p>
<p>Created At: { account.Attributes.CreatedAt.String() }</p>
</div>
<hr/>
}
</ul>
</body>
</html>
}
118 changes: 118 additions & 0 deletions datafetcher/templates/accounts_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package templ
package templates

import (
"strconv"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7a921d1

Please sign in to comment.