Recount-Server/web/rendering.go

108 lines
3.1 KiB
Go
Raw Permalink Normal View History

2024-01-17 19:58:31 -08:00
package web
import (
2024-02-27 18:38:00 -08:00
"nickiel.net/recount_server/web/templates"
2024-02-10 08:51:27 -08:00
2024-01-17 19:58:31 -08:00
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"time"
2024-01-19 21:05:08 -08:00
"github.com/go-chi/chi/v5"
2024-01-17 19:58:31 -08:00
"github.com/rs/zerolog/log"
"nickiel.net/recount_server/types"
)
2024-02-27 18:38:00 -08:00
const DEFAULT_RESULT_COUNT = 20
2024-01-17 19:58:31 -08:00
2024-02-10 08:51:27 -08:00
func GetTransactionsRows(w http.ResponseWriter, req *http.Request) {
2024-02-27 18:38:00 -08:00
transactions := make([]types.HumanLegibleTransaction, 10)
2024-01-17 19:58:31 -08:00
// Populate the slice with dummy data (you can replace this with your actual data)
for i := 10; i > 0; i-- {
transaction := types.HumanLegibleTransaction{
Id: i,
Amount: fmt.Sprintf("%d.00", (i+1)*100),
Description: sql.NullString{String: fmt.Sprintf("Transaction %d", i+1), Valid: true},
AccountName: sql.NullString{String: "Savings", Valid: true},
Account: 123,
Bucket: sql.NullInt64{Int64: int64(i + 100), Valid: true},
BucketName: sql.NullString{String: fmt.Sprintf("Bucket %d", i+1), Valid: true},
Date: time.Now(),
}
2024-02-27 18:38:00 -08:00
transactions[10-i] = transaction
2024-01-17 19:58:31 -08:00
}
2024-02-27 18:38:00 -08:00
component := templates.TransactionRows(&transactions)
component.Render(context.Background(), w)
2024-01-17 19:58:31 -08:00
}
2024-02-10 08:51:27 -08:00
func GetTransactionQuickAccessEntries(w http.ResponseWriter, req *http.Request) {
2024-02-27 18:38:00 -08:00
transactions := make([]types.HumanLegibleTransaction, 20)
2024-01-24 18:58:29 -08:00
2024-01-27 12:20:25 -08:00
// Populate the slice with dummy data (you can replace this with your actual data)
2024-02-09 19:02:08 -08:00
for i := 0; i < 20; i++ {
2024-01-27 12:20:25 -08:00
transaction := types.HumanLegibleTransaction{
Id: i,
Amount: fmt.Sprintf("%d.00", (i+1)*100),
Description: sql.NullString{String: fmt.Sprintf("Transaction %d", i+1), Valid: true},
AccountName: sql.NullString{String: "Savings", Valid: true},
Account: 123,
Bucket: sql.NullInt64{Int64: int64(i + 100), Valid: true},
BucketName: sql.NullString{String: fmt.Sprintf("Bucket %d", i+1), Valid: true},
Date: time.Now(),
}
2024-02-09 19:02:08 -08:00
transactions[i] = transaction
2024-01-27 12:20:25 -08:00
}
2024-02-27 18:38:00 -08:00
component := templates.TransactionQuickAccessEntries(&transactions)
component.Render(context.Background(), w)
2024-02-09 19:02:08 -08:00
}
2024-02-10 08:51:27 -08:00
func GetExpenditureChart(w http.ResponseWriter, req *http.Request) {
2024-02-27 18:38:00 -08:00
data_package := struct {
Labels []string `json:"labels"`
IncomeDataset []int `json:"income_data"`
ExpensesDataset []int `json:"expenses_data"`
}{
Labels: []string{"Income", "Expenses"},
IncomeDataset: []int{300, 250},
ExpensesDataset: []int{200, 100},
}
json_data, err := json.Marshal(data_package)
if err != nil {
log.Fatal().Err(err).Msg("Could not jsonify data_package")
}
w.Write(json_data)
2024-01-17 19:58:31 -08:00
}
2024-01-19 21:05:08 -08:00
2024-02-10 08:51:27 -08:00
func GetAccountSummaries(w http.ResponseWriter, req *http.Request) {
2024-02-27 18:38:00 -08:00
accounts := make([]types.TwoIntsItem, 20)
for i := 0; i < 20; i++ {
accounts[i] = types.TwoIntsItem{Item1: i * 100, Item2: i + 5}
}
component := templates.AccountSummaryRows(&accounts)
component.Render(context.Background(), w)
2024-01-19 21:05:08 -08:00
}
2024-02-10 08:51:27 -08:00
func GetAccountSummaryChart(w http.ResponseWriter, req *http.Request) {
2024-02-27 18:38:00 -08:00
accountID := chi.URLParam(req, "accountID")
data_package := types.ChartjsData{
Labels: []string{accountID, "1/10", "1/17", "1/24"},
Data: []int{100, 0, -50, 25},
}
json_data, err := json.Marshal(data_package)
if err != nil {
log.Fatal().Err(err).Msg("Could not jsonify data_package")
}
w.Write(json_data)
2024-01-19 21:05:08 -08:00
}