2017-10-04 19:35:59 -04:00
|
|
|
package handlers
|
2017-01-23 20:40:39 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-02 06:14:47 -05:00
|
|
|
"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"
|
2017-01-23 20:40:39 -05:00
|
|
|
"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)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 20:50:37 -05:00
|
|
|
tabulation, err := reports.RunReport(tx, user, report)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
|
|
|
// TODO handle different failure cases differently
|
2017-12-10 20:50:37 -05:00
|
|
|
log.Print("reports.RunReport returned:", err)
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 10:28:50 -04:00
|
|
|
tabulation.ReportId = reportid
|
|
|
|
|
2017-10-14 14:20:50 -04:00
|
|
|
return tabulation
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:17:27 -05:00
|
|
|
func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
|
|
|
|
user, err := GetUserFromSession(context.Tx, r)
|
2017-01-23 20:40:39 -05:00
|
|
|
if err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(1 /*Not Signed In*/)
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
if r.Method == "POST" {
|
2017-12-05 05:58:36 -05:00
|
|
|
var report models.Report
|
2017-11-13 20:48:19 -05:00
|
|
|
if err := ReadJSON(r, &report); err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
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 {
|
2017-11-03 20:50:19 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
|
|
|
}
|
|
|
|
|
2017-12-09 05:56:45 -05:00
|
|
|
err = context.Tx.InsertReport(&report)
|
2017-02-11 06:06:45 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(999 /*Internal Error*/)
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
2017-01-23 20:40:39 -05:00
|
|
|
|
2017-10-14 14:20:50 -04:00
|
|
|
return ResponseWrapper{201, &report}
|
2017-06-16 20:55:22 -04:00
|
|
|
} else if r.Method == "GET" {
|
2017-11-12 21:12:49 -05:00
|
|
|
if context.LastLevel() {
|
2017-06-16 20:55:22 -04:00
|
|
|
//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)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(999 /*Internal Error*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
rl.Reports = reports
|
2017-10-14 14:20:50 -04:00
|
|
|
return &rl
|
2017-11-12 21:12:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
reportid, err := context.NextID()
|
|
|
|
if err != nil {
|
|
|
|
return NewError(3 /*Invalid Request*/)
|
|
|
|
}
|
|
|
|
|
|
|
|
if context.NextLevel() == "tabulations" {
|
|
|
|
return ReportTabulationHandler(context.Tx, r, user, reportid)
|
2017-06-16 20:55:22 -04:00
|
|
|
} else {
|
|
|
|
// Return Report with this Id
|
2017-12-09 05:56:45 -05:00
|
|
|
report, err := context.Tx.GetReport(reportid, user.UserId)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-10-14 14:20:50 -04:00
|
|
|
return report
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-12 21:12:49 -05:00
|
|
|
reportid, err := context.NextID()
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == "PUT" {
|
2017-12-05 05:58:36 -05:00
|
|
|
var report models.Report
|
2017-11-13 20:48:19 -05:00
|
|
|
if err := ReadJSON(r, &report); err != nil || report.ReportId != reportid {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
report.UserId = user.UserId
|
|
|
|
|
2017-12-05 05:58:36 -05:00
|
|
|
if len(report.Lua) >= models.LuaMaxLength {
|
2017-11-03 20:50:19 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
|
|
|
}
|
|
|
|
|
2017-12-09 05:56:45 -05:00
|
|
|
err = context.Tx.UpdateReport(&report)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(999 /*Internal Error*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-10-14 14:20:50 -04:00
|
|
|
return &report
|
2017-06-16 20:55:22 -04:00
|
|
|
} else if r.Method == "DELETE" {
|
2017-12-09 05:56:45 -05:00
|
|
|
report, err := context.Tx.GetReport(reportid, user.UserId)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-12-09 05:56:45 -05:00
|
|
|
err = context.Tx.DeleteReport(report)
|
2017-06-16 20:55:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(999 /*Internal Error*/)
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-10-14 14:20:50 -04:00
|
|
|
return SuccessWriter{}
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
2017-10-14 14:20:50 -04:00
|
|
|
return NewError(3 /*Invalid Request*/)
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|