ExpiryDatesManager/server/controllers/reportPositionsController.go
2019-01-05 16:19:24 +01:00

46 lines
1.1 KiB
Go

package controllers
import (
"strconv"
"encoding/json"
"github.com/gorilla/mux"
"ExpiryDatesManager/server/model"
"ExpiryDatesManager/server/utils"
"net/http"
)
var GetReportsPositionsForReport = func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
companyID, err := strconv.ParseUint(vars["reportId"], 10, 0)
if err != nil {
utils.Respond(w, utils.Message(false, "Error while decoding request body"))
return
}
data := model.GetReportPositionsForReport(uint(companyID))
resp := utils.Message(true, "success")
resp["data"] = data
utils.Respond(w, resp)
}
var CreateOrUpdateReportPosition = func(w http.ResponseWriter, r *http.Request) {
reportPosition := &model.ReportPosition{}
err := json.NewDecoder(r.Body).Decode(reportPosition)
if err != nil {
utils.Respond(w, utils.Message(false, "Error while decoding request body"))
return
}
data := reportPosition.CreateOrUpdate()
if data.ID == 0 {
utils.Respond(w, utils.Message(false, "Database error"))
return
}
resp := utils.Message(true, "success")
resp["data"] = data
utils.Respond(w, resp)
}