VILLASweb-backend-go/endpoints/simulationEndpoints.go

106 lines
No EOL
2.5 KiB
Go

package endpoints
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/queries"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/serializers"
)
// getSimulationsEp godoc
// @Summary Get all simulations
// @ID GetAllSimulations
// @Produce json
// @Tags simulation
// @Success 200 {array} common.Simulation "Array of simulations to which user has access"
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
// @Failure 404 "Not found"
// @Failure 500 "Internal server error"
// @Router /simulations [get]
func getSimulationsEp(c *gin.Context) {
//TODO Identify user who is issuing the request and return only those simulations that are known to the user
allSimulations, _, _ := queries.FindAllSimulations()
serializer := serializers.SimulationsSerializer{c, allSimulations}
c.JSON(http.StatusOK, gin.H{
"simulations": serializer.Response(),
})
}
func simulationRegistrationEp(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "NOT implemented",
})
}
func simulationCloneEp(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "NOT implemented",
})
}
func simulationUpdateEp(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "NOT implemented",
})
}
// getSimulationEp godoc
// @Summary Get simulation
// @ID GetSimulation
// @Produce json
// @Tags simulation
// @Success 200 {object} common.Simulation "Simulation requested by user"
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
// @Failure 404 "Not found"
// @Failure 500 "Internal server error"
// @Param simulationID path int true "Simulation ID"
// @Router /simulations/{simulationID} [get]
func getSimulationEp(c *gin.Context) {
simID, err := GetSimulationID(c)
if err != nil {
return
}
sim, err := queries.FindSimulation(simID)
if common.ProvideErrorResponse(c, err) {
return
}
serializer := serializers.SimulationSerializer{c, sim}
c.JSON(http.StatusOK, gin.H{
"simulation": serializer.Response(),
})
}
func simulationDeleteEp(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "NOT implemented",
})
}
func GetSimulationID(c *gin.Context) (int, error) {
simID, err := strconv.Atoi(c.Param("SimulationID"))
if err != nil {
errormsg := fmt.Sprintf("Bad request. No or incorrect format of simulation ID")
c.JSON(http.StatusBadRequest, gin.H{
"error": errormsg,
})
return -1, err
} else {
return simID, err
}
}