1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-27 16:02:27 -05:00
moneygo/internal/handlers/reports.go

124 lines
2.9 KiB
Go
Raw Normal View History

package handlers
import (
"github.com/aclindsa/moneygo/internal/models"
2017-12-10 20:50:37 -05:00
"github.com/aclindsa/moneygo/internal/reports"
2017-12-09 05:56:45 -05:00
"github.com/aclindsa/moneygo/internal/store"
2017-01-27 11:04:39 -05:00
"log"
"net/http"
)
2017-12-09 05:56:45 -05:00
func ReportTabulationHandler(tx store.Tx, r *http.Request, user *models.User, reportid int64) ResponseWriterWriter {
report, err := tx.GetReport(reportid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-10 20:50:37 -05:00
tabulation, err := reports.RunReport(tx, user, report)
if err != nil {
// TODO handle different failure cases differently
2017-12-10 20:50:37 -05:00
log.Print("reports.RunReport returned:", err)
return NewError(3 /*Invalid Request*/)
}
2017-06-17 10:28:50 -04:00
tabulation.ReportId = reportid
return tabulation
}
func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
user, err := GetUserFromSession(context.Tx, r)
if err != nil {
return NewError(1 /*Not Signed In*/)
}
if r.Method == "POST" {
2017-12-05 05:58:36 -05:00
var report models.Report
if err := ReadJSON(r, &report); err != nil {
return NewError(3 /*Invalid Request*/)
}
report.ReportId = -1
report.UserId = user.UserId
2017-01-27 11:04:39 -05:00
2017-12-05 05:58:36 -05:00
if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/)
}
2017-12-09 05:56:45 -05:00
err = context.Tx.InsertReport(&report)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return ResponseWrapper{201, &report}
} else if r.Method == "GET" {
if context.LastLevel() {
//Return all Reports
2017-12-05 05:58:36 -05:00
var rl models.ReportList
2017-12-09 05:56:45 -05:00
reports, err := context.Tx.GetReports(user.UserId)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
rl.Reports = reports
return &rl
}
reportid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if context.NextLevel() == "tabulations" {
return ReportTabulationHandler(context.Tx, r, user, reportid)
} else {
// Return Report with this Id
2017-12-09 05:56:45 -05:00
report, err := context.Tx.GetReport(reportid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
return report
}
} else {
reportid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if r.Method == "PUT" {
2017-12-05 05:58:36 -05:00
var report models.Report
if err := ReadJSON(r, &report); err != nil || report.ReportId != reportid {
return NewError(3 /*Invalid Request*/)
}
report.UserId = user.UserId
2017-12-05 05:58:36 -05:00
if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/)
}
2017-12-09 05:56:45 -05:00
err = context.Tx.UpdateReport(&report)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return &report
} else if r.Method == "DELETE" {
2017-12-09 05:56:45 -05:00
report, err := context.Tx.GetReport(reportid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
2017-12-09 05:56:45 -05:00
err = context.Tx.DeleteReport(report)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return SuccessWriter{}
}
}
return NewError(3 /*Invalid Request*/)
}