feat: Enhance dependency injection and service integration
- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
This commit is contained in:
@@ -42,14 +42,14 @@ func (h *AuthHandler) Register(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证邮箱验证码
|
||||
if err := service.VerifyCode(c.Request.Context(), h.container.Redis, req.Email, req.VerificationCode, service.VerificationTypeRegister); err != nil {
|
||||
if err := h.container.VerificationService.VerifyCode(c.Request.Context(), req.Email, req.VerificationCode, service.VerificationTypeRegister); err != nil {
|
||||
h.logger.Warn("验证码验证失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// 注册用户
|
||||
user, token, err := h.container.UserService.Register(req.Username, req.Password, req.Email, req.Avatar)
|
||||
user, token, err := h.container.UserService.Register(c.Request.Context(), req.Username, req.Password, req.Email, req.Avatar)
|
||||
if err != nil {
|
||||
h.logger.Error("用户注册失败", zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
@@ -83,7 +83,7 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
ipAddress := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
|
||||
user, token, err := h.container.UserService.Login(req.Username, req.Password, ipAddress, userAgent)
|
||||
user, token, err := h.container.UserService.Login(c.Request.Context(), req.Username, req.Password, ipAddress, userAgent)
|
||||
if err != nil {
|
||||
h.logger.Warn("用户登录失败",
|
||||
zap.String("username_or_email", req.Username),
|
||||
@@ -117,13 +117,7 @@ func (h *AuthHandler) SendVerificationCode(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
emailService, err := h.getEmailService()
|
||||
if err != nil {
|
||||
RespondServerError(c, "邮件服务不可用", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.SendVerificationCode(c.Request.Context(), h.container.Redis, emailService, req.Email, req.Type); err != nil {
|
||||
if err := h.container.VerificationService.SendCode(c.Request.Context(), req.Email, req.Type); err != nil {
|
||||
h.logger.Error("发送验证码失败",
|
||||
zap.String("email", req.Email),
|
||||
zap.String("type", req.Type),
|
||||
@@ -154,14 +148,14 @@ func (h *AuthHandler) ResetPassword(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
if err := service.VerifyCode(c.Request.Context(), h.container.Redis, req.Email, req.VerificationCode, service.VerificationTypeResetPassword); err != nil {
|
||||
if err := h.container.VerificationService.VerifyCode(c.Request.Context(), req.Email, req.VerificationCode, service.VerificationTypeResetPassword); err != nil {
|
||||
h.logger.Warn("验证码验证失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// 重置密码
|
||||
if err := h.container.UserService.ResetPassword(req.Email, req.NewPassword); err != nil {
|
||||
if err := h.container.UserService.ResetPassword(c.Request.Context(), req.Email, req.NewPassword); err != nil {
|
||||
h.logger.Error("重置密码失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondServerError(c, err.Error(), nil)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user