初步实现device_controller
This commit is contained in:
224
internal/app/controller/device/device_controller.go
Normal file
224
internal/app/controller/device/device_controller.go
Normal file
@@ -0,0 +1,224 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Controller 设备控制器,封装了所有与设备相关的业务逻辑
|
||||
type Controller struct {
|
||||
repo repository.DeviceRepository
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewController 创建一个新的设备控制器实例
|
||||
func NewController(repo repository.DeviceRepository, logger *logs.Logger) *Controller {
|
||||
return &Controller{
|
||||
repo: repo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// --- Request & Response Structs ---
|
||||
|
||||
// CreateDeviceRequest 定义了创建设备时需要传入的参数
|
||||
type CreateDeviceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type models.DeviceType `json:"type" binding:"required"`
|
||||
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
||||
ParentID *uint `json:"parent_id,omitempty"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties datatypes.JSON `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateDeviceRequest 定义了更新设备时需要传入的参数
|
||||
type UpdateDeviceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type models.DeviceType `json:"type" binding:"required"`
|
||||
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
||||
ParentID *uint `json:"parent_id,omitempty"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties datatypes.JSON `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// --- Controller Methods ---
|
||||
|
||||
// CreateDevice godoc
|
||||
// @Summary 创建新设备
|
||||
// @Description 根据提供的信息创建一个新设备,可以是区域主控或普通设备
|
||||
// @Tags 设备管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param device body CreateDeviceRequest true "设备信息"
|
||||
// @Success 200 {object} controller.Response{data=models.Device} "业务码为201代表创建成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)"
|
||||
// @Router /devices [post]
|
||||
func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
var req CreateDeviceRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("创建设备: 参数绑定失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
device := &models.Device{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
SubType: req.SubType,
|
||||
ParentID: req.ParentID,
|
||||
Location: req.Location,
|
||||
Properties: req.Properties,
|
||||
}
|
||||
|
||||
if err := c.repo.Create(device); err != nil {
|
||||
c.logger.Errorf("创建设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusCreated, "设备创建成功", device)
|
||||
}
|
||||
|
||||
// GetDevice godoc
|
||||
// @Summary 获取设备信息
|
||||
// @Description 根据设备ID获取单个设备的详细信息
|
||||
// @Tags 设备管理
|
||||
// @Produce json
|
||||
// @Param id path string true "设备ID"
|
||||
// @Success 200 {object} controller.Response{data=models.Device} "业务码为200代表获取成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
|
||||
// @Router /devices/{id} [get]
|
||||
func (c *Controller) GetDevice(ctx *gin.Context) {
|
||||
deviceID := ctx.Param("id")
|
||||
|
||||
device, err := c.repo.FindByIDString(deviceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
controller.SendErrorResponse(ctx, http.StatusNotFound, "设备未找到")
|
||||
return
|
||||
}
|
||||
// 检查是否是ID格式错误
|
||||
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("获取设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取设备信息失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "获取设备信息成功", device)
|
||||
}
|
||||
|
||||
// ListDevices godoc
|
||||
// @Summary 获取设备列表
|
||||
// @Description 获取系统中所有设备的列表
|
||||
// @Tags 设备管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]models.Device} "业务码为200代表获取成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如500)"
|
||||
// @Router /devices [get]
|
||||
func (c *Controller) ListDevices(ctx *gin.Context) {
|
||||
devices, err := c.repo.ListAll()
|
||||
if err != nil {
|
||||
c.logger.Errorf("获取设备列表: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取设备列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "获取设备列表成功", devices)
|
||||
}
|
||||
|
||||
// UpdateDevice godoc
|
||||
// @Summary 更新设备信息
|
||||
// @Description 根据设备ID更新一个已存在的设备信息
|
||||
// @Tags 设备管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "设备ID"
|
||||
// @Param device body UpdateDeviceRequest true "要更新的设备信息"
|
||||
// @Success 200 {object} controller.Response{data=models.Device} "业务码为200代表更新成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
|
||||
// @Router /devices/{id} [put]
|
||||
func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
deviceID := ctx.Param("id")
|
||||
|
||||
// 1. 检查设备是否存在
|
||||
existingDevice, err := c.repo.FindByIDString(deviceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
controller.SendErrorResponse(ctx, http.StatusNotFound, "设备未找到")
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("更新设备: 查找设备失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "更新设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 绑定请求参数
|
||||
var req UpdateDeviceRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("更新设备: 参数绑定失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 更新字段
|
||||
existingDevice.Name = req.Name
|
||||
existingDevice.Type = req.Type
|
||||
existingDevice.SubType = req.SubType
|
||||
existingDevice.ParentID = req.ParentID
|
||||
existingDevice.Location = req.Location
|
||||
existingDevice.Properties = req.Properties
|
||||
|
||||
// 4. 保存到数据库
|
||||
if err := c.repo.Update(existingDevice); err != nil {
|
||||
c.logger.Errorf("更新设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "更新设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "设备更新成功", existingDevice)
|
||||
}
|
||||
|
||||
// DeleteDevice godoc
|
||||
// @Summary 删除设备
|
||||
// @Description 根据设备ID删除一个设备(软删除)
|
||||
// @Tags 设备管理
|
||||
// @Produce json
|
||||
// @Param id path string true "设备ID"
|
||||
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)"
|
||||
// @Router /devices/{id} [delete]
|
||||
func (c *Controller) DeleteDevice(ctx *gin.Context) {
|
||||
deviceID := ctx.Param("id")
|
||||
|
||||
// 我们需要先将字符串ID转换为uint,因为Delete方法需要uint类型
|
||||
idUint, err := strconv.ParseUint(deviceID, 10, 64)
|
||||
if err != nil {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, "无效的设备ID格式")
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.repo.Delete(uint(idUint)); err != nil {
|
||||
c.logger.Errorf("删除设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "删除设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "设备删除成功", nil)
|
||||
}
|
||||
@@ -60,10 +60,8 @@ type LoginResponse struct {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param user body CreateUserRequest true "用户信息"
|
||||
// @Success 200 {object} controller.Response{data=CreateUserResponse} "用户创建成功"
|
||||
// @Failure 400 {object} controller.Response "请求参数错误"
|
||||
// @Failure 409 {object} controller.Response "用户名已存在"
|
||||
// @Failure 500 {object} controller.Response "服务器内部错误"
|
||||
// @Success 200 {object} controller.Response{data=CreateUserResponse} "业务码为201代表创建成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 409, 500)"
|
||||
// @Router /users [post]
|
||||
func (c *Controller) CreateUser(ctx *gin.Context) {
|
||||
var req CreateUserRequest
|
||||
@@ -93,7 +91,7 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "用户创建成功", CreateUserResponse{
|
||||
controller.SendResponse(ctx, http.StatusCreated, "用户创建成功", CreateUserResponse{
|
||||
Username: user.Username,
|
||||
ID: user.ID,
|
||||
})
|
||||
@@ -106,10 +104,8 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param credentials body LoginRequest true "登录凭证"
|
||||
// @Success 200 {object} controller.Response{data=LoginResponse} "登录成功"
|
||||
// @Failure 400 {object} controller.Response "请求参数错误"
|
||||
// @Failure 401 {object} controller.Response "用户名或密码不正确"
|
||||
// @Failure 500 {object} controller.Response "服务器内部错误"
|
||||
// @Success 200 {object} controller.Response{data=LoginResponse} "业务码为200代表登录成功"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 401, 500)"
|
||||
// @Router /users/login [post]
|
||||
func (c *Controller) Login(ctx *gin.Context) {
|
||||
var req LoginRequest
|
||||
|
||||
Reference in New Issue
Block a user