Skip to content

Instantly share code, notes, and snippets.

@bensonmacharia
Created May 12, 2023 22:35
Show Gist options
  • Save bensonmacharia/6fe6efc937bde705f687a35a66fec53e to your computer and use it in GitHub Desktop.
Save bensonmacharia/6fe6efc937bde705f687a35a66fec53e to your computer and use it in GitHub Desktop.
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