Created
May 12, 2023 22:35
-
-
Save bensonmacharia/6fe6efc937bde705f687a35a66fec53e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controller | |
import ( | |
"bmacharia/jwt-go-rbac/model" | |
"errors" | |
"net/http" | |
"strconv" | |
"github.com/gin-gonic/gin" | |
"gorm.io/gorm" | |
) | |
// create Role | |
func CreateRole(c *gin.Context) { | |
var Role model.Role | |
c.BindJSON(&Role) | |
err := model.CreateRole(&Role) | |
if err != nil { | |
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | |
return | |
} | |
c.JSON(http.StatusOK, Role) | |
} | |
// get Roles | |
func GetRoles(c *gin.Context) { | |
var Role []model.Role | |
err := model.GetRoles(&Role) | |
if err != nil { | |
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | |
return | |
} | |
c.JSON(http.StatusOK, Role) | |
} | |
// get Role by id | |
func GetRole(c *gin.Context) { | |
id, _ := strconv.Atoi(c.Param("id")) | |
var Role model.Role | |
err := model.GetRole(&Role, id) | |
if err != nil { | |
if errors.Is(err, gorm.ErrRecordNotFound) { | |
c.AbortWithStatus(http.StatusNotFound) | |
return | |
} | |
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | |
return | |
} | |
c.JSON(http.StatusOK, Role) | |
} | |
// update Role | |
func UpdateRole(c *gin.Context) { | |
var Role model.Role | |
id, _ := strconv.Atoi(c.Param("id")) | |
err := model.GetRole(&Role, id) | |
if err != nil { | |
if errors.Is(err, gorm.ErrRecordNotFound) { | |
c.AbortWithStatus(http.StatusNotFound) | |
return | |
} | |
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | |
return | |
} | |
c.BindJSON(&Role) | |
err = model.UpdateRole(&Role) | |
if err != nil { | |
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | |
return | |
} | |
c.JSON(http.StatusOK, Role) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment