1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2025-07-03 12:48:38 -04:00

Split reports into models

This commit is contained in:
2017-12-05 05:58:36 -05:00
parent 5f296e8669
commit 3e3038295d
7 changed files with 122 additions and 117 deletions

View File

@ -2,14 +2,12 @@ package handlers
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/aclindsa/moneygo/internal/models"
"github.com/yuin/gopher-lua"
"log"
"net/http"
"strings"
"time"
)
@ -26,67 +24,8 @@ const (
const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for
type Report struct {
ReportId int64
UserId int64
Name string
Lua string
}
// The maximum length (in bytes) the Lua code may be. This is used to set the
// max size of the database columns (with an added fudge factor)
const LuaMaxLength int = 65536
func (r *Report) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(r)
}
func (r *Report) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(r)
}
type ReportList struct {
Reports *[]Report `json:"reports"`
}
func (rl *ReportList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(rl)
}
func (rl *ReportList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(rl)
}
type Series struct {
Values []float64
Series map[string]*Series
}
type Tabulation struct {
ReportId int64
Title string
Subtitle string
Units string
Labels []string
Series map[string]*Series
}
func (t *Tabulation) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(t)
}
func (t *Tabulation) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(t)
}
func GetReport(tx *Tx, reportid int64, userid int64) (*Report, error) {
var r Report
func GetReport(tx *Tx, reportid int64, userid int64) (*models.Report, error) {
var r models.Report
err := tx.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid)
if err != nil {
@ -95,8 +34,8 @@ func GetReport(tx *Tx, reportid int64, userid int64) (*Report, error) {
return &r, nil
}
func GetReports(tx *Tx, userid int64) (*[]Report, error) {
var reports []Report
func GetReports(tx *Tx, userid int64) (*[]models.Report, error) {
var reports []models.Report
_, err := tx.Select(&reports, "SELECT * from reports where UserId=?", userid)
if err != nil {
@ -105,7 +44,7 @@ func GetReports(tx *Tx, userid int64) (*[]Report, error) {
return &reports, nil
}
func InsertReport(tx *Tx, r *Report) error {
func InsertReport(tx *Tx, r *models.Report) error {
err := tx.Insert(r)
if err != nil {
return err
@ -113,7 +52,7 @@ func InsertReport(tx *Tx, r *Report) error {
return nil
}
func UpdateReport(tx *Tx, r *Report) error {
func UpdateReport(tx *Tx, r *models.Report) error {
count, err := tx.Update(r)
if err != nil {
return err
@ -124,7 +63,7 @@ func UpdateReport(tx *Tx, r *Report) error {
return nil
}
func DeleteReport(tx *Tx, r *Report) error {
func DeleteReport(tx *Tx, r *models.Report) error {
count, err := tx.Delete(r)
if err != nil {
return err
@ -135,7 +74,7 @@ func DeleteReport(tx *Tx, r *Report) error {
return nil
}
func runReport(tx *Tx, user *models.User, report *Report) (*Tabulation, error) {
func runReport(tx *Tx, user *models.User, report *models.Report) (*models.Tabulation, error) {
// Create a new LState without opening the default libs for security
L := lua.NewState(lua.Options{SkipOpenLibs: true})
defer L.Close()
@ -189,7 +128,7 @@ func runReport(tx *Tx, user *models.User, report *Report) (*Tabulation, error) {
value := L.Get(-1)
if ud, ok := value.(*lua.LUserData); ok {
if tabulation, ok := ud.Value.(*Tabulation); ok {
if tabulation, ok := ud.Value.(*models.Tabulation); ok {
return tabulation, nil
} else {
return nil, fmt.Errorf("generate() for %s (Id: %d) didn't return a tabulation", report.Name, report.ReportId)
@ -224,14 +163,14 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
}
if r.Method == "POST" {
var report Report
var report models.Report
if err := ReadJSON(r, &report); err != nil {
return NewError(3 /*Invalid Request*/)
}
report.ReportId = -1
report.UserId = user.UserId
if len(report.Lua) >= LuaMaxLength {
if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/)
}
@ -245,7 +184,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
} else if r.Method == "GET" {
if context.LastLevel() {
//Return all Reports
var rl ReportList
var rl models.ReportList
reports, err := GetReports(context.Tx, user.UserId)
if err != nil {
log.Print(err)
@ -278,13 +217,13 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
}
if r.Method == "PUT" {
var report Report
var report models.Report
if err := ReadJSON(r, &report); err != nil || report.ReportId != reportid {
return NewError(3 /*Invalid Request*/)
}
report.UserId = user.UserId
if len(report.Lua) >= LuaMaxLength {
if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/)
}