VILLASweb-backend-go/routes/simulator/simulator_middleware.go
Sonja Happ f3a7ed0e61 **Major revision of repository structure**
- rename common package to database
- move all code not related to database to new helper package
- add more test in database package to improve code coverage
- add a new (own) package for AMQP client
2019-09-10 16:28:57 +02:00

37 lines
863 B
Go

package simulator
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"github.com/gin-gonic/gin"
"strconv"
)
func checkPermissions(c *gin.Context, modeltype database.ModelName, operation database.CRUD, hasID bool) (bool, Simulator) {
var s Simulator
err := database.ValidateRole(c, modeltype, operation)
if err != nil {
helper.UnprocessableEntityError(c, err.Error())
return false, s
}
if hasID {
// Get the ID of the simulator from the context
simulatorID, err := strconv.Atoi(c.Param("simulatorID"))
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("Could not get simulator's ID from context"))
return false, s
}
err = s.ByID(uint(simulatorID))
if helper.DBError(c, err) {
return false, s
}
}
return true, s
}