Recount-Server/web/data_endpoints.go

59 lines
1.6 KiB
Go
Raw Normal View History

2024-01-17 19:58:31 -08:00
package web
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/rs/zerolog/log"
"nickiel.net/recount_server/types"
)
const DEFAULT_RESULT_COUNT = 20;
func getTransactions(w http.ResponseWriter, req *http.Request) {
transactions := make([]types.HumanLegibleTransaction, 10)
// 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(),
}
transactions[10 - i] = transaction
}
component := transaction_rows(&transactions);
component.Render(context.Background(), w);
}
func getExpenditureChart(w http.ResponseWriter, req *http.Request) {
2024-01-18 18:57:03 -08:00
data_package := struct{
Labels []string `json:"labels"`
IncomeDataset []int `json:"income_data"`
ExpensesDataset []int `json:"expenses_data"`
} {
2024-01-17 19:58:31 -08:00
Labels: []string{"Income", "Expenses"},
2024-01-18 18:57:03 -08:00
IncomeDataset: []int {300, 250},
ExpensesDataset: []int {200, 100},
2024-01-17 19:58:31 -08:00
};
json_data, err := json.Marshal(data_package);
if err != nil {
log.Fatal().Err(err).Msg("Could not jsonify data_package");
}
w.Write(json_data);
}