修改controller包
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
@@ -12,18 +13,18 @@ import (
|
||||
|
||||
// Controller 用户控制器
|
||||
type Controller struct {
|
||||
ctx context.Context
|
||||
userService service.UserService
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewController 创建用户控制器实例
|
||||
func NewController(
|
||||
ctx context.Context,
|
||||
userService service.UserService,
|
||||
logger *logs.Logger,
|
||||
) *Controller {
|
||||
return &Controller{
|
||||
ctx: ctx,
|
||||
userService: userService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,15 +40,17 @@ func NewController(
|
||||
// @Success 200 {object} controller.Response{data=dto.CreateUserResponse} "业务码为201代表创建成功"
|
||||
// @Router /api/v1/users [post]
|
||||
func (c *Controller) CreateUser(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateUser")
|
||||
|
||||
var req dto.CreateUserRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
c.logger.Errorf("创建用户: 参数绑定失败: %v", err)
|
||||
logger.Errorf("创建用户: 参数绑定失败: %v", err)
|
||||
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||
}
|
||||
|
||||
resp, err := c.userService.CreateUser(&req)
|
||||
resp, err := c.userService.CreateUser(reqCtx, &req)
|
||||
if err != nil {
|
||||
c.logger.Errorf("创建用户: 服务层调用失败: %v", err)
|
||||
logger.Errorf("创建用户: 服务层调用失败: %v", err)
|
||||
return controller.SendErrorResponse(ctx, controller.CodeInternalError, err.Error())
|
||||
}
|
||||
|
||||
@@ -64,15 +67,17 @@ func (c *Controller) CreateUser(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.LoginResponse} "业务码为200代表登录成功"
|
||||
// @Router /api/v1/users/login [post]
|
||||
func (c *Controller) Login(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "Login")
|
||||
|
||||
var req dto.LoginRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
c.logger.Errorf("登录: 参数绑定失败: %v", err)
|
||||
logger.Errorf("登录: 参数绑定失败: %v", err)
|
||||
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||
}
|
||||
|
||||
resp, err := c.userService.Login(&req)
|
||||
resp, err := c.userService.Login(reqCtx, &req)
|
||||
if err != nil {
|
||||
c.logger.Errorf("登录: 服务层调用失败: %v", err)
|
||||
logger.Errorf("登录: 服务层调用失败: %v", err)
|
||||
return controller.SendErrorResponse(ctx, controller.CodeUnauthorized, err.Error())
|
||||
}
|
||||
|
||||
@@ -91,30 +96,31 @@ func (c *Controller) Login(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=string} "成功响应"
|
||||
// @Router /api/v1/users/{id}/notifications/test [post]
|
||||
func (c *Controller) SendTestNotification(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "SendTestNotification")
|
||||
const actionType = "发送测试通知"
|
||||
|
||||
// 1. 从 URL 中获取用户 ID
|
||||
userID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 无效的用户ID格式: %v", actionType, err)
|
||||
logger.Errorf("%s: 无效的用户ID格式: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", ctx.Param("id"))
|
||||
}
|
||||
|
||||
// 2. 从请求体 (JSON Body) 中获取要测试的通知类型
|
||||
var req dto.SendTestNotificationRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "请求体格式错误或缺少 'type' 字段: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
// 3. 调用服务层
|
||||
err = c.userService.SendTestNotification(uint(userID), &req)
|
||||
err = c.userService.SendTestNotification(reqCtx, uint(userID), &req)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 服务层调用失败: %v", actionType, err)
|
||||
logger.Errorf("%s: 服务层调用失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "发送测试消息失败: "+err.Error(), actionType, "服务层调用失败", map[string]interface{}{"userID": userID, "type": req.Type})
|
||||
}
|
||||
|
||||
// 4. 返回成功响应
|
||||
c.logger.Infof("%s: 成功为用户 %d 发送类型为 %s 的测试消息", actionType, userID, req.Type)
|
||||
logger.Infof("%s: 成功为用户 %d 发送类型为 %s 的测试消息", actionType, userID, req.Type)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "测试消息已发送,请检查您的接收端。", nil, actionType, "测试消息发送成功", map[string]interface{}{"userID": userID, "type": req.Type})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user