add new db handler with table change to reference many2many
This commit is contained in:
@@ -55,6 +55,7 @@ func (aH *AccessHandler) AddRole(c *gin.Context) {
|
||||
if aH.dbHandler.Exists(&models.Role{}, "role", role.Role, false) {
|
||||
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)))
|
||||
return
|
||||
}
|
||||
|
||||
// Insert new role with provided permissions
|
||||
@@ -62,6 +63,7 @@ func (aH *AccessHandler) AddRole(c *gin.Context) {
|
||||
Role: role.Role,
|
||||
Permissions: role.Permissions,
|
||||
})
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("role '%s' successfully added", role.Role),
|
||||
})
|
||||
@@ -76,7 +78,7 @@ func (aH *AccessHandler) GetRole(c *gin.Context) {
|
||||
id := c.Query("id")
|
||||
|
||||
if role != "" {
|
||||
err = aH.dbHandler.GetByKey(&roles, "role", role, false)
|
||||
err = aH.dbHandler.GetByKey(&roles, "", "role", role, false)
|
||||
} else if id != "" {
|
||||
i, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
@@ -85,9 +87,9 @@ func (aH *AccessHandler) GetRole(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
err = aH.dbHandler.GetById(&roles, uint(i))
|
||||
err = aH.dbHandler.GetById(&roles, "", uint(i))
|
||||
} else {
|
||||
err = aH.dbHandler.GetById(&roles, 0)
|
||||
err = aH.dbHandler.GetById(&roles, "", 0)
|
||||
|
||||
}
|
||||
|
||||
@@ -106,12 +108,23 @@ func (aH *AccessHandler) UpdateRole(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
err := aH.dbHandler.UpdateValuesById(&role, role.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
|
||||
if role.Id != 0 {
|
||||
err := aH.dbHandler.UpdateValuesById(&role, "", role.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := aH.dbHandler.UpdateValuesByKey(&role, "", "role", role.Role)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated role '"+role.Role+"'"))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user