fix minor bugs in role api

This commit is contained in:
Adrian Zürcher
2025-10-24 17:35:32 +02:00
parent 1b7218e5de
commit b6988638c0

19
api.go
View File

@@ -181,7 +181,7 @@ func (aH *AccessHandlerAPI) AddRole(c *gin.Context) {
} }
// Check if a role with this name already exists // Check if a role with this name already exists
if err := aH.dbHandler.Exists(&models.Role{}, "role", role, false); err == nil { if err := aH.dbHandler.Exists(&models.Role{}, "role", role.Role, false); err == nil {
aH.logger.Error("AddRole", fmt.Sprintf("role with name %s already exists", role.Role)) aH.logger.Error("AddRole", fmt.Sprintf("role with name %s already exists", role.Role))
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("role with name %s already exists", role.Role))) c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("role with name %s already exists", role.Role)))
} }
@@ -199,25 +199,30 @@ func (aH *AccessHandlerAPI) AddRole(c *gin.Context) {
func (aH *AccessHandlerAPI) GetRole(c *gin.Context) { func (aH *AccessHandlerAPI) GetRole(c *gin.Context) {
var i int var i int
var err error var err error
var roles []models.Role
role := c.Query("role")
id := c.Query("id") id := c.Query("id")
if id != "" {
if role != "" {
err = aH.dbHandler.GetByKey(&roles, "role", role, false)
} else if id != "" {
i, err = strconv.Atoi(id) i, err = strconv.Atoi(id)
if err != nil { if err != nil {
aH.logger.Error("GetRole", err) c.JSON(http.StatusBadRequest, gin.H{
c.JSON(http.StatusInternalServerError, nil) "message": err.Error(),
})
return return
} }
err = aH.dbHandler.GetById(&roles, uint(i))
} }
var role []models.Role
err = aH.dbHandler.GetById(&role, uint(i))
if err != nil { if err != nil {
aH.logger.Error("GetRole", err) aH.logger.Error("GetRole", err)
c.JSON(http.StatusInternalServerError, nil) c.JSON(http.StatusInternalServerError, nil)
return return
} }
c.JSON(http.StatusOK, role) c.JSON(http.StatusOK, roles)
} }
func (aH *AccessHandlerAPI) UpdateRole(c *gin.Context) { func (aH *AccessHandlerAPI) UpdateRole(c *gin.Context) {