重构dto

This commit is contained in:
2025-10-03 23:02:43 +08:00
parent fadc1e2535
commit d273932693
10 changed files with 647 additions and 494 deletions

View File

@@ -5,6 +5,7 @@ import (
"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/domain/token"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
@@ -31,50 +32,6 @@ func NewController(userRepo repository.UserRepository, auditRepo repository.User
}
}
// --- DTOs ---
// CreateUserRequest 定义创建用户请求的结构体
type CreateUserRequest struct {
Username string `json:"username" binding:"required" example:"newuser"`
Password string `json:"password" binding:"required" example:"password123"`
}
// LoginRequest 定义登录请求的结构体
type LoginRequest struct {
// Identifier 可以是用户名、邮箱、手机号、微信号或飞书账号
Identifier string `json:"identifier" binding:"required" example:"testuser"`
Password string `json:"password" binding:"required" example:"password123"`
}
// CreateUserResponse 定义创建用户成功响应的结构体
type CreateUserResponse struct {
Username string `json:"username" example:"newuser"`
ID uint `json:"id" example:"1"`
}
// LoginResponse 定义登录成功响应的结构体
type LoginResponse struct {
Username string `json:"username" example:"testuser"`
ID uint `json:"id" example:"1"`
Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."`
}
// HistoryResponse 定义单条操作历史的响应结构体
type HistoryResponse struct {
UserID uint `json:"user_id" example:"101"`
Username string `json:"username" example:"testuser"`
ActionType string `json:"action_type" example:"更新设备"`
Description string `json:"description" example:"设备更新成功"`
TargetResource interface{} `json:"target_resource"`
Time string `json:"time"`
}
// ListHistoryResponse 定义操作历史列表的响应结构体
type ListHistoryResponse struct {
History []HistoryResponse `json:"history"`
Total int64 `json:"total" example:"100"`
}
// --- Controller Methods ---
// CreateUser godoc
@@ -83,11 +40,11 @@ type ListHistoryResponse struct {
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param user body CreateUserRequest true "用户信息"
// @Success 200 {object} controller.Response{data=user.CreateUserResponse} "业务码为201代表创建成功"
// @Param user body dto.CreateUserRequest true "用户信息"
// @Success 200 {object} controller.Response{data=dto.CreateUserResponse} "业务码为201代表创建成功"
// @Router /api/v1/users [post]
func (c *Controller) CreateUser(ctx *gin.Context) {
var req CreateUserRequest
var req dto.CreateUserRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("创建用户: 参数绑定失败: %v", err)
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
@@ -114,7 +71,7 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
return
}
controller.SendResponse(ctx, controller.CodeCreated, "用户创建成功", CreateUserResponse{
controller.SendResponse(ctx, controller.CodeCreated, "用户创建成功", dto.CreateUserResponse{
Username: user.Username,
ID: user.ID,
})
@@ -126,11 +83,11 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param credentials body LoginRequest true "登录凭证"
// @Success 200 {object} controller.Response{data=user.LoginResponse} "业务码为200代表登录成功"
// @Param credentials body dto.LoginRequest true "登录凭证"
// @Success 200 {object} controller.Response{data=dto.LoginResponse} "业务码为200代表登录成功"
// @Router /api/v1/users/login [post]
func (c *Controller) Login(ctx *gin.Context) {
var req LoginRequest
var req dto.LoginRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("登录: 参数绑定失败: %v", err)
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
@@ -162,7 +119,7 @@ func (c *Controller) Login(ctx *gin.Context) {
return
}
controller.SendResponse(ctx, controller.CodeSuccess, "登录成功", LoginResponse{
controller.SendResponse(ctx, controller.CodeSuccess, "登录成功", dto.LoginResponse{
Username: user.Username,
ID: user.ID,
Token: tokenString,
@@ -178,7 +135,7 @@ func (c *Controller) Login(ctx *gin.Context) {
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页大小" default(10)
// @Param action_type query string false "按操作类型过滤"
// @Success 200 {object} controller.Response{data=user.ListHistoryResponse} "业务码为200代表成功获取"
// @Success 200 {object} controller.Response{data=dto.ListHistoryResponse} "业务码为200代表成功获取"
// @Router /api/v1/users/{id}/history [get]
func (c *Controller) ListUserHistory(ctx *gin.Context) {
const actionType = "获取用户操作历史"
@@ -221,9 +178,9 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
}
// 4. 将数据库模型转换为响应 DTO
historyResponses := make([]HistoryResponse, 0, len(logs))
historyResponses := make([]dto.HistoryResponse, 0, len(logs))
for _, log := range logs {
historyResponses = append(historyResponses, HistoryResponse{
historyResponses = append(historyResponses, dto.HistoryResponse{
UserID: log.UserID,
Username: log.Username,
ActionType: log.ActionType,
@@ -234,7 +191,7 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
}
// 5. 发送成功响应
resp := ListHistoryResponse{
resp := dto.ListHistoryResponse{
History: historyResponses,
Total: total,
}