ExpiryDatesManager/server/controllers/productExpiryPositionsController.go
2019-01-13 21:09:49 +01:00

70 lines
1.7 KiB
Go

package controllers
import (
"strconv"
"github.com/gorilla/mux"
"encoding/json"
"ExpiryDatesManager/server/model"
"ExpiryDatesManager/server/utils"
"net/http"
)
var GetPositionsForCompany = func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
companyID, err := strconv.ParseUint(vars["companyId"], 10, 0)
if err != nil {
utils.Respond(w, utils.Message(false, "Error while parsing request"))
return
}
data := model.GetPositionsForCompany(uint(companyID))
resp := utils.Message(true, "success")
resp["data"] = data
utils.Respond(w, resp)
}
var GetPositions = func(w http.ResponseWriter, r *http.Request) {
data := model.GetPositions()
resp := utils.Message(true, "success")
resp["data"] = data
utils.Respond(w, resp)
}
var DeletePosition = func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
positionID, err := strconv.ParseUint(vars["id"], 10, 0)
if err != nil {
utils.Respond(w, utils.Message(false, "Error while parsing request"))
return
}
err = model.DeletePosition(uint(positionID))
if err != nil {
utils.Respond(w, utils.Message(false, "Nie udało się usunąć pozycji"))
return
}
resp := utils.Message(true, "success")
utils.Respond(w, resp)
}
var CreateOrUpdatePosition = func(w http.ResponseWriter, r *http.Request) {
position := &model.ProductExpiryPosition{}
err := json.NewDecoder(r.Body).Decode(position)
if err != nil {
utils.Respond(w, utils.Message(false, "Error while decoding request body"))
return
}
data := position.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)
}