重构dto
This commit is contained in:
@@ -3,12 +3,11 @@ package device
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"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"
|
||||
@@ -34,243 +33,11 @@ func NewController(
|
||||
return &Controller{
|
||||
deviceRepo: deviceRepo,
|
||||
areaControllerRepo: areaControllerRepo,
|
||||
deviceTemplateRepo: deviceTemplateRepo, // 初始化设备模板仓库
|
||||
deviceTemplateRepo: deviceTemplateRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// --- Request DTOs ---
|
||||
|
||||
// CreateDeviceRequest 定义了创建设备时需要传入的参数
|
||||
type CreateDeviceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
DeviceTemplateID uint `json:"device_template_id" binding:"required"`
|
||||
AreaControllerID uint `json:"area_controller_id" binding:"required"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateDeviceRequest 定义了更新设备时需要传入的参数
|
||||
type UpdateDeviceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
DeviceTemplateID uint `json:"device_template_id" binding:"required"`
|
||||
AreaControllerID uint `json:"area_controller_id" binding:"required"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// CreateAreaControllerRequest 定义了创建区域主控时需要传入的参数
|
||||
type CreateAreaControllerRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
NetworkID string `json:"network_id" binding:"required"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateAreaControllerRequest 定义了更新区域主控时需要传入的参数
|
||||
type UpdateAreaControllerRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
NetworkID string `json:"network_id" binding:"required"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// CreateDeviceTemplateRequest 定义了创建设备模板时需要传入的参数
|
||||
type CreateDeviceTemplateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Manufacturer string `json:"manufacturer,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Category models.DeviceCategory `json:"category" binding:"required"`
|
||||
Commands map[string]interface{} `json:"commands" binding:"required"`
|
||||
Values []models.ValueDescriptor `json:"values,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateDeviceTemplateRequest 定义了更新设备模板时需要传入的参数
|
||||
type UpdateDeviceTemplateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Manufacturer string `json:"manufacturer,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Category models.DeviceCategory `json:"category" binding:"required"`
|
||||
Commands map[string]interface{} `json:"commands" binding:"required"`
|
||||
Values []models.ValueDescriptor `json:"values,omitempty"`
|
||||
}
|
||||
|
||||
// --- Response DTOs ---
|
||||
|
||||
// DeviceResponse 定义了返回给客户端的单个设备信息的结构
|
||||
type DeviceResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DeviceTemplateID uint `json:"device_template_id"`
|
||||
DeviceTemplateName string `json:"device_template_name"`
|
||||
AreaControllerID uint `json:"area_controller_id"`
|
||||
AreaControllerName string `json:"area_controller_name"`
|
||||
Location string `json:"location"`
|
||||
Properties map[string]interface{} `json:"properties"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// AreaControllerResponse 定义了返回给客户端的单个区域主控信息的结构
|
||||
type AreaControllerResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
NetworkID string `json:"network_id"`
|
||||
Location string `json:"location"`
|
||||
Status string `json:"status"`
|
||||
Properties map[string]interface{} `json:"properties"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// DeviceTemplateResponse 定义了返回给客户端的单个设备模板信息的结构
|
||||
type DeviceTemplateResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
Description string `json:"description"`
|
||||
Category models.DeviceCategory `json:"category"`
|
||||
Commands map[string]interface{} `json:"commands"`
|
||||
Values []models.ValueDescriptor `json:"values"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// --- DTO 转换函数 ---
|
||||
|
||||
// newDeviceResponse 从数据库模型创建一个新的设备响应 DTO
|
||||
func newDeviceResponse(device *models.Device) (*DeviceResponse, error) {
|
||||
if device == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var props map[string]interface{}
|
||||
if len(device.Properties) > 0 && string(device.Properties) != "null" {
|
||||
if err := device.ParseProperties(&props); err != nil {
|
||||
return nil, fmt.Errorf("解析设备属性失败 (ID: %d): %w", device.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 确保 DeviceTemplate 和 AreaController 已预加载
|
||||
deviceTemplateName := ""
|
||||
if device.DeviceTemplate.ID != 0 {
|
||||
deviceTemplateName = device.DeviceTemplate.Name
|
||||
}
|
||||
|
||||
areaControllerName := ""
|
||||
if device.AreaController.ID != 0 {
|
||||
areaControllerName = device.AreaController.Name
|
||||
}
|
||||
|
||||
return &DeviceResponse{
|
||||
ID: device.ID,
|
||||
Name: device.Name,
|
||||
DeviceTemplateID: device.DeviceTemplateID,
|
||||
DeviceTemplateName: deviceTemplateName,
|
||||
AreaControllerID: device.AreaControllerID,
|
||||
AreaControllerName: areaControllerName,
|
||||
Location: device.Location,
|
||||
Properties: props,
|
||||
CreatedAt: device.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: device.UpdatedAt.Format(time.RFC3339),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newListDeviceResponse 从数据库模型切片创建一个新的设备列表响应 DTO 切片
|
||||
func newListDeviceResponse(devices []*models.Device) ([]*DeviceResponse, error) {
|
||||
list := make([]*DeviceResponse, 0, len(devices))
|
||||
for _, device := range devices {
|
||||
resp, err := newDeviceResponse(device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, resp)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// newAreaControllerResponse 从数据库模型创建一个新的区域主控响应 DTO
|
||||
func newAreaControllerResponse(ac *models.AreaController) (*AreaControllerResponse, error) {
|
||||
if ac == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var props map[string]interface{}
|
||||
if len(ac.Properties) > 0 && string(ac.Properties) != "null" {
|
||||
if err := json.Unmarshal(ac.Properties, &props); err != nil {
|
||||
return nil, fmt.Errorf("解析区域主控属性失败 (ID: %d): %w", ac.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return &AreaControllerResponse{
|
||||
ID: ac.ID,
|
||||
Name: ac.Name,
|
||||
NetworkID: ac.NetworkID,
|
||||
Location: ac.Location,
|
||||
Status: ac.Status,
|
||||
Properties: props,
|
||||
CreatedAt: ac.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: ac.UpdatedAt.Format(time.RFC3339),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newListAreaControllerResponse 从数据库模型切片创建一个新的区域主控列表响应 DTO 切片
|
||||
func newListAreaControllerResponse(acs []*models.AreaController) ([]*AreaControllerResponse, error) {
|
||||
list := make([]*AreaControllerResponse, 0, len(acs))
|
||||
for _, ac := range acs {
|
||||
resp, err := newAreaControllerResponse(ac)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, resp)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// newDeviceTemplateResponse 从数据库模型创建一个新的设备模板响应 DTO
|
||||
func newDeviceTemplateResponse(dt *models.DeviceTemplate) (*DeviceTemplateResponse, error) {
|
||||
if dt == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var commands map[string]interface{}
|
||||
if err := dt.ParseCommands(&commands); err != nil {
|
||||
return nil, fmt.Errorf("解析设备模板命令失败 (ID: %d): %w", dt.ID, err)
|
||||
}
|
||||
|
||||
var values []models.ValueDescriptor
|
||||
if dt.Category == models.CategorySensor {
|
||||
if err := dt.ParseValues(&values); err != nil {
|
||||
return nil, fmt.Errorf("解析设备模板值描述符失败 (ID: %d): %w", dt.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return &DeviceTemplateResponse{
|
||||
ID: dt.ID,
|
||||
Name: dt.Name,
|
||||
Manufacturer: dt.Manufacturer,
|
||||
Description: dt.Description,
|
||||
Category: dt.Category,
|
||||
Commands: commands,
|
||||
Values: values,
|
||||
CreatedAt: dt.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: dt.UpdatedAt.Format(time.RFC3339),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newListDeviceTemplateResponse 从数据库模型切片创建一个新的设备模板列表响应 DTO 切片
|
||||
func newListDeviceTemplateResponse(dts []*models.DeviceTemplate) ([]*DeviceTemplateResponse, error) {
|
||||
list := make([]*DeviceTemplateResponse, 0, len(dts))
|
||||
for _, dt := range dts {
|
||||
resp, err := newDeviceTemplateResponse(dt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, resp)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// --- Controller Methods: Devices ---
|
||||
|
||||
// CreateDevice godoc
|
||||
@@ -279,12 +46,12 @@ func newListDeviceTemplateResponse(dts []*models.DeviceTemplate) ([]*DeviceTempl
|
||||
// @Tags 设备管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param device body CreateDeviceRequest true "设备信息"
|
||||
// @Success 200 {object} controller.Response{data=DeviceResponse}
|
||||
// @Param device body dto.CreateDeviceRequest true "设备信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||
// @Router /api/v1/devices [post]
|
||||
func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
const actionType = "创建设备"
|
||||
var req CreateDeviceRequest
|
||||
var req dto.CreateDeviceRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -325,9 +92,9 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceResponse(createdDevice)
|
||||
resp, err := dto.NewDeviceResponse(createdDevice)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, createdDevice)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败", actionType, "响应序列化失败", createdDevice)
|
||||
return
|
||||
}
|
||||
@@ -342,7 +109,7 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
// @Tags 设备管理
|
||||
// @Produce json
|
||||
// @Param id path string true "设备ID"
|
||||
// @Success 200 {object} controller.Response{data=DeviceResponse}
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||
// @Router /api/v1/devices/{id} [get]
|
||||
func (c *Controller) GetDevice(ctx *gin.Context) {
|
||||
const actionType = "获取设备"
|
||||
@@ -371,7 +138,7 @@ func (c *Controller) GetDevice(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceResponse(device)
|
||||
resp, err := dto.NewDeviceResponse(device)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, device)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误", actionType, "响应序列化失败", device)
|
||||
@@ -387,7 +154,7 @@ func (c *Controller) GetDevice(ctx *gin.Context) {
|
||||
// @Description 获取系统中所有设备的列表
|
||||
// @Tags 设备管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]DeviceResponse}
|
||||
// @Success 200 {object} controller.Response{data=[]dto.DeviceResponse}
|
||||
// @Router /api/v1/devices [get]
|
||||
func (c *Controller) ListDevices(ctx *gin.Context) {
|
||||
const actionType = "获取设备列表"
|
||||
@@ -398,7 +165,7 @@ func (c *Controller) ListDevices(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newListDeviceResponse(devices)
|
||||
resp, err := dto.NewListDeviceResponse(devices)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, Devices: %+v", actionType, err, devices)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误", actionType, "响应序列化失败", devices)
|
||||
@@ -416,8 +183,8 @@ func (c *Controller) ListDevices(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "设备ID"
|
||||
// @Param device body UpdateDeviceRequest true "要更新的设备信息"
|
||||
// @Success 200 {object} controller.Response{data=DeviceResponse}
|
||||
// @Param device body dto.UpdateDeviceRequest true "要更新的设备信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
||||
// @Router /api/v1/devices/{id} [put]
|
||||
func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
const actionType = "更新设备"
|
||||
@@ -440,7 +207,7 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateDeviceRequest
|
||||
var req dto.UpdateDeviceRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -479,7 +246,7 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceResponse(updatedDevice)
|
||||
resp, err := dto.NewDeviceResponse(updatedDevice)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, updatedDevice)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败", actionType, "响应序列化失败", updatedDevice)
|
||||
@@ -539,12 +306,12 @@ func (c *Controller) DeleteDevice(ctx *gin.Context) {
|
||||
// @Tags 区域主控管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param areaController body CreateAreaControllerRequest true "区域主控信息"
|
||||
// @Success 200 {object} controller.Response{data=AreaControllerResponse}
|
||||
// @Param areaController body dto.CreateAreaControllerRequest true "区域主控信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||
// @Router /api/v1/area-controllers [post]
|
||||
func (c *Controller) CreateAreaController(ctx *gin.Context) {
|
||||
const actionType = "创建区域主控"
|
||||
var req CreateAreaControllerRequest
|
||||
var req dto.CreateAreaControllerRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -577,7 +344,7 @@ func (c *Controller) CreateAreaController(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newAreaControllerResponse(ac)
|
||||
resp, err := dto.NewAreaControllerResponse(ac)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控创建成功,但响应生成失败", actionType, "响应序列化失败", ac)
|
||||
@@ -594,7 +361,7 @@ func (c *Controller) CreateAreaController(ctx *gin.Context) {
|
||||
// @Tags 区域主控管理
|
||||
// @Produce json
|
||||
// @Param id path string true "区域主控ID"
|
||||
// @Success 200 {object} controller.Response{data=AreaControllerResponse}
|
||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||
// @Router /api/v1/area-controllers/{id} [get]
|
||||
func (c *Controller) GetAreaController(ctx *gin.Context) {
|
||||
const actionType = "获取区域主控"
|
||||
@@ -619,7 +386,7 @@ func (c *Controller) GetAreaController(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newAreaControllerResponse(ac)
|
||||
resp, err := dto.NewAreaControllerResponse(ac)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, ac)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: 内部数据格式错误", actionType, "响应序列化失败", ac)
|
||||
@@ -635,7 +402,7 @@ func (c *Controller) GetAreaController(ctx *gin.Context) {
|
||||
// @Description 获取系统中所有区域主控的列表
|
||||
// @Tags 区域主控管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]AreaControllerResponse}
|
||||
// @Success 200 {object} controller.Response{data=[]dto.AreaControllerResponse}
|
||||
// @Router /api/v1/area-controllers [get]
|
||||
func (c *Controller) ListAreaControllers(ctx *gin.Context) {
|
||||
const actionType = "获取区域主控列表"
|
||||
@@ -646,7 +413,7 @@ func (c *Controller) ListAreaControllers(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newListAreaControllerResponse(acs)
|
||||
resp, err := dto.NewListAreaControllerResponse(acs)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, AreaControllers: %+v", actionType, err, acs)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: 内部数据格式错误", actionType, "响应序列化失败", acs)
|
||||
@@ -664,8 +431,8 @@ func (c *Controller) ListAreaControllers(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "区域主控ID"
|
||||
// @Param areaController body UpdateAreaControllerRequest true "要更新的区域主控信息"
|
||||
// @Success 200 {object} controller.Response{data=AreaControllerResponse}
|
||||
// @Param areaController body dto.UpdateAreaControllerRequest true "要更新的区域主控信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
||||
// @Router /api/v1/area-controllers/{id} [put]
|
||||
func (c *Controller) UpdateAreaController(ctx *gin.Context) {
|
||||
const actionType = "更新区域主控"
|
||||
@@ -690,7 +457,7 @@ func (c *Controller) UpdateAreaController(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateAreaControllerRequest
|
||||
var req dto.UpdateAreaControllerRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -721,7 +488,7 @@ func (c *Controller) UpdateAreaController(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newAreaControllerResponse(existingAC)
|
||||
resp, err := dto.NewAreaControllerResponse(existingAC)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, existingAC)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控更新成功,但响应生成失败", actionType, "响应序列化失败", existingAC)
|
||||
@@ -781,12 +548,12 @@ func (c *Controller) DeleteAreaController(ctx *gin.Context) {
|
||||
// @Tags 设备模板管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param deviceTemplate body CreateDeviceTemplateRequest true "设备模板信息"
|
||||
// @Success 200 {object} controller.Response{data=DeviceTemplateResponse}
|
||||
// @Param deviceTemplate body dto.CreateDeviceTemplateRequest true "设备模板信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||
// @Router /api/v1/device-templates [post]
|
||||
func (c *Controller) CreateDeviceTemplate(ctx *gin.Context) {
|
||||
const actionType = "创建设备模板"
|
||||
var req CreateDeviceTemplateRequest
|
||||
var req dto.CreateDeviceTemplateRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -828,7 +595,7 @@ func (c *Controller) CreateDeviceTemplate(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceTemplateResponse(deviceTemplate)
|
||||
resp, err := dto.NewDeviceTemplateResponse(deviceTemplate)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板创建成功,但响应生成失败", actionType, "响应序列化失败", deviceTemplate)
|
||||
@@ -845,7 +612,7 @@ func (c *Controller) CreateDeviceTemplate(ctx *gin.Context) {
|
||||
// @Tags 设备模板管理
|
||||
// @Produce json
|
||||
// @Param id path string true "设备模板ID"
|
||||
// @Success 200 {object} controller.Response{data=DeviceTemplateResponse}
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||
// @Router /api/v1/device-templates/{id} [get]
|
||||
func (c *Controller) GetDeviceTemplate(ctx *gin.Context) {
|
||||
const actionType = "获取设备模板"
|
||||
@@ -870,7 +637,7 @@ func (c *Controller) GetDeviceTemplate(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceTemplateResponse(deviceTemplate)
|
||||
resp, err := dto.NewDeviceTemplateResponse(deviceTemplate)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, deviceTemplate)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplate)
|
||||
@@ -886,7 +653,7 @@ func (c *Controller) GetDeviceTemplate(ctx *gin.Context) {
|
||||
// @Description 获取系统中所有设备模板的列表
|
||||
// @Tags 设备模板管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]DeviceTemplateResponse}
|
||||
// @Success 200 {object} controller.Response{data=[]dto.DeviceTemplateResponse}
|
||||
// @Router /api/v1/device-templates [get]
|
||||
func (c *Controller) ListDeviceTemplates(ctx *gin.Context) {
|
||||
const actionType = "获取设备模板列表"
|
||||
@@ -897,7 +664,7 @@ func (c *Controller) ListDeviceTemplates(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newListDeviceTemplateResponse(deviceTemplates)
|
||||
resp, err := dto.NewListDeviceTemplateResponse(deviceTemplates)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplates: %+v", actionType, err, deviceTemplates)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplates)
|
||||
@@ -915,8 +682,8 @@ func (c *Controller) ListDeviceTemplates(ctx *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "设备模板ID"
|
||||
// @Param deviceTemplate body UpdateDeviceTemplateRequest true "要更新的设备模板信息"
|
||||
// @Success 200 {object} controller.Response{data=DeviceTemplateResponse}
|
||||
// @Param deviceTemplate body dto.UpdateDeviceTemplateRequest true "要更新的设备模板信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
||||
// @Router /api/v1/device-templates/{id} [put]
|
||||
func (c *Controller) UpdateDeviceTemplate(ctx *gin.Context) {
|
||||
const actionType = "更新设备模板"
|
||||
@@ -941,7 +708,7 @@ func (c *Controller) UpdateDeviceTemplate(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateDeviceTemplateRequest
|
||||
var req dto.UpdateDeviceTemplateRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
@@ -981,7 +748,7 @@ func (c *Controller) UpdateDeviceTemplate(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := newDeviceTemplateResponse(existingDeviceTemplate)
|
||||
resp, err := dto.NewDeviceTemplateResponse(existingDeviceTemplate)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, existingDeviceTemplate)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板更新成功,但响应生成失败", actionType, "响应序列化失败", existingDeviceTemplate)
|
||||
|
||||
Reference in New Issue
Block a user