-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Testing out some templ handlers * Updating readme with templ instructions * refactoring the account and transactions handlers * refactoring the account and transactions handlers * Revert "refactoring the account and transactions handlers" This reverts commit d107f8a. * Trying a new setup * No changes from gofmt? * go mod tidy
- Loading branch information
Showing
13 changed files
with
516 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/esteanes/expense-manager/datafetcher/upclient" | ||
) | ||
|
||
type AccountHandler struct { | ||
*BaseHandler | ||
} | ||
|
||
func NewAccountHandler(log *log.Logger, upclient *upclient.APIClient, auth context.Context) *AccountHandler { | ||
handler := &AccountHandler{} | ||
handler.BaseHandler = &BaseHandler{ | ||
Uri: "/accounts", | ||
Log: log, | ||
UpClient: upclient, | ||
UpAuth: auth, | ||
Handler: handler, // Set the Handler interface to the specific handler | ||
} | ||
return handler | ||
} | ||
|
||
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() | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/esteanes/expense-manager/datafetcher/upclient" | ||
) | ||
|
||
type Handler interface { | ||
Post(w http.ResponseWriter, r *http.Request) | ||
Get(w http.ResponseWriter, r *http.Request) | ||
} | ||
|
||
type BaseHandler struct { | ||
Uri string | ||
Log *log.Logger | ||
UpClient *upclient.APIClient | ||
UpAuth context.Context | ||
Handler Handler // Embed the Handler interface | ||
} | ||
|
||
func (h *BaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case http.MethodPost: | ||
h.Handler.Post(w, r) | ||
case http.MethodGet: | ||
h.Handler.Get(w, r) | ||
default: | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/esteanes/expense-manager/datafetcher/upclient" | ||
) | ||
|
||
type TransactionsHandler struct { | ||
*BaseHandler | ||
} | ||
|
||
func NewTransactionHandler(log *log.Logger, upclient *upclient.APIClient, auth context.Context) *AccountHandler { | ||
handler := &AccountHandler{} | ||
handler.BaseHandler = &BaseHandler{ | ||
Uri: "/transactions", | ||
Log: log, | ||
UpClient: upclient, | ||
UpAuth: auth, | ||
Handler: handler, // Set the Handler interface to the specific handler | ||
} | ||
return handler | ||
} | ||
|
||
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() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error when calling `TransactionsAPI.TransactionsGet``: %v\n", err) | ||
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r2.Body) | ||
} | ||
// response from `TransactionsGet`: ListTransactionsResponse | ||
fmt.Fprintf(os.Stdout, "Response from `TransactionsAPI.TransactionsGet`: %v\n", resp2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package templ | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
templ TimeComponent(d time.Time) { | ||
<div>{ d.String() }</div> | ||
} | ||
|
||
templ NotFoundComponent() { | ||
<div>404 - Not found</div> | ||
} | ||
|
||
|
||
templ counts(global, user int) { | ||
<div>Global: { strconv.Itoa(global) }</div> | ||
<div>User: { strconv.Itoa(user) }</div> | ||
} | ||
|
||
templ form() { | ||
<form action="/counter" method="POST"> | ||
<div><button type="submit" name="global" value="global">Global</button></div> | ||
<div><button type="submit" name="user" value="user">User</button></div> | ||
</form> | ||
} | ||
|
||
templ Page(global, user int) { | ||
@counts(global, user) | ||
@form() | ||
} |
Oops, something went wrong.