1. 操作历史增删改查
2. 定义设备开关接口(只有定义)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/config"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/controller/device"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/controller/operation"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/controller/user"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
@@ -36,6 +37,9 @@ type API struct {
|
||||
// operationController 操作历史控制器
|
||||
operationController *operation.Controller
|
||||
|
||||
// deviceController 设备控制控制器
|
||||
deviceController *device.Controller
|
||||
|
||||
// authMiddleware 鉴权中间件
|
||||
authMiddleware *middleware.AuthMiddleware
|
||||
|
||||
@@ -45,7 +49,7 @@ type API struct {
|
||||
|
||||
// NewAPI 创建并返回一个新的API实例
|
||||
// 初始化Gin引擎和相关配置
|
||||
func NewAPI(cfg *config.Config, userRepo repository.UserRepo, operationHistoryRepo repository.OperationHistoryRepo) *API {
|
||||
func NewAPI(cfg *config.Config, userRepo repository.UserRepo, operationHistoryRepo repository.OperationHistoryRepo, deviceControlRepo repository.DeviceControlRepo) *API {
|
||||
// 设置Gin为发布模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
@@ -75,6 +79,9 @@ func NewAPI(cfg *config.Config, userRepo repository.UserRepo, operationHistoryRe
|
||||
// 创建操作历史控制器
|
||||
operationController := operation.NewController(operationHistoryRepo)
|
||||
|
||||
// 创建设备控制控制器
|
||||
deviceController := device.NewController(deviceControlRepo)
|
||||
|
||||
// 创建鉴权中间件
|
||||
authMiddleware := middleware.NewAuthMiddleware(userRepo)
|
||||
|
||||
@@ -83,6 +90,7 @@ func NewAPI(cfg *config.Config, userRepo repository.UserRepo, operationHistoryRe
|
||||
config: cfg,
|
||||
userController: userController,
|
||||
operationController: operationController,
|
||||
deviceController: deviceController,
|
||||
authMiddleware: authMiddleware,
|
||||
logger: logs.NewLogger(),
|
||||
}
|
||||
@@ -157,29 +165,12 @@ func (a *API) setupRoutes() {
|
||||
operationGroup.GET("/:id", a.operationController.Get)
|
||||
}
|
||||
|
||||
// 示例受保护路由
|
||||
protectedGroup.GET("/profile", func(c *gin.Context) {
|
||||
// 从上下文中获取用户信息
|
||||
userValue, exists := c.Get("user")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
|
||||
return
|
||||
}
|
||||
// 设备控制相关路由
|
||||
deviceGroup := protectedGroup.Group("/device")
|
||||
{
|
||||
deviceGroup.POST("/switch", a.deviceController.Switch)
|
||||
}
|
||||
|
||||
user, ok := userValue.(*middleware.AuthUser)
|
||||
if !ok {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "获取用户信息成功",
|
||||
"user": map[string]interface{}{
|
||||
"id": user.ID,
|
||||
"username": user.Username,
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: 添加更多路由
|
||||
|
||||
Reference in New Issue
Block a user