38 lines
835 B
Go
38 lines
835 B
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitea.zjmud.com/phyer/rbac/models"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserController struct {
|
|
DB *gorm.DB
|
|
Redis *redis.Client
|
|
}
|
|
|
|
func NewUserController(db *gorm.DB, redis *redis.Client) *UserController {
|
|
return &UserController{DB: db, Redis: redis}
|
|
}
|
|
|
|
func (uc *UserController) GetUsers(c *gin.Context) {
|
|
var users []models.User
|
|
if err := uc.DB.Find(&users).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch users"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, users)
|
|
}
|
|
|
|
func (uc *UserController) GetUserByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var user models.User
|
|
if err := uc.DB.First(&user, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, user)
|
|
}
|