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:
@@ -3,7 +3,6 @@ package handler
|
||||
import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/internal/service"
|
||||
"carrotskin/internal/types"
|
||||
"strconv"
|
||||
|
||||
@@ -43,9 +42,8 @@ func (h *TextureHandler) GenerateUploadURL(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := service.GenerateTextureUploadURL(
|
||||
result, err := h.container.UploadService.GenerateTextureUploadURL(
|
||||
c.Request.Context(),
|
||||
h.container.Storage,
|
||||
userID,
|
||||
req.FileName,
|
||||
string(req.TextureType),
|
||||
@@ -83,12 +81,13 @@ func (h *TextureHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
maxTextures := h.container.UserService.GetMaxTexturesPerUser()
|
||||
if err := h.container.TextureService.CheckUploadLimit(userID, maxTextures); err != nil {
|
||||
if err := h.container.TextureService.CheckUploadLimit(c.Request.Context(), userID, maxTextures); err != nil {
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.Create(
|
||||
c.Request.Context(),
|
||||
userID,
|
||||
req.Name,
|
||||
req.Description,
|
||||
@@ -120,7 +119,7 @@ func (h *TextureHandler) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.GetByID(id)
|
||||
texture, err := h.container.TextureService.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
RespondNotFound(c, err.Error())
|
||||
return
|
||||
@@ -146,7 +145,7 @@ func (h *TextureHandler) Search(c *gin.Context) {
|
||||
textureType = model.TextureTypeCape
|
||||
}
|
||||
|
||||
textures, total, err := h.container.TextureService.Search(keyword, textureType, publicOnly, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.Search(c.Request.Context(), keyword, textureType, publicOnly, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("搜索材质失败", zap.String("keyword", keyword), zap.Error(err))
|
||||
RespondServerError(c, "搜索材质失败", err)
|
||||
@@ -175,7 +174,7 @@ func (h *TextureHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.Update(textureID, userID, req.Name, req.Description, req.IsPublic)
|
||||
texture, err := h.container.TextureService.Update(c.Request.Context(), textureID, userID, req.Name, req.Description, req.IsPublic)
|
||||
if err != nil {
|
||||
h.logger.Error("更新材质失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -202,7 +201,7 @@ func (h *TextureHandler) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.TextureService.Delete(textureID, userID); err != nil {
|
||||
if err := h.container.TextureService.Delete(c.Request.Context(), textureID, userID); err != nil {
|
||||
h.logger.Error("删除材质失败",
|
||||
zap.Int64("user_id", userID),
|
||||
zap.Int64("texture_id", textureID),
|
||||
@@ -228,7 +227,7 @@ func (h *TextureHandler) ToggleFavorite(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isFavorited, err := h.container.TextureService.ToggleFavorite(userID, textureID)
|
||||
isFavorited, err := h.container.TextureService.ToggleFavorite(c.Request.Context(), userID, textureID)
|
||||
if err != nil {
|
||||
h.logger.Error("切换收藏状态失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -252,7 +251,7 @@ func (h *TextureHandler) GetUserTextures(c *gin.Context) {
|
||||
page := parseIntWithDefault(c.DefaultQuery("page", "1"), 1)
|
||||
pageSize := parseIntWithDefault(c.DefaultQuery("page_size", "20"), 20)
|
||||
|
||||
textures, total, err := h.container.TextureService.GetByUserID(userID, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.GetByUserID(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户材质列表失败", zap.Int64("user_id", userID), zap.Error(err))
|
||||
RespondServerError(c, "获取材质列表失败", err)
|
||||
@@ -272,7 +271,7 @@ func (h *TextureHandler) GetUserFavorites(c *gin.Context) {
|
||||
page := parseIntWithDefault(c.DefaultQuery("page", "1"), 1)
|
||||
pageSize := parseIntWithDefault(c.DefaultQuery("page_size", "20"), 20)
|
||||
|
||||
textures, total, err := h.container.TextureService.GetUserFavorites(userID, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.GetUserFavorites(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户收藏列表失败", zap.Int64("user_id", userID), zap.Error(err))
|
||||
RespondServerError(c, "获取收藏列表失败", err)
|
||||
|
||||
Reference in New Issue
Block a user