refactor: Implement dependency injection for handlers and services

- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection.
- Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection.
- Updated the container to initialize service instances and provide them to handlers.
- Enhanced code structure for better testability and adherence to Go best practices.
This commit is contained in:
lafay
2025-12-02 19:43:39 +08:00
parent 188a05caa7
commit 801f1b1397
33 changed files with 3628 additions and 4129 deletions

View File

@@ -1,16 +1,28 @@
package handler
import (
"carrotskin/internal/service"
"carrotskin/internal/container"
"carrotskin/internal/types"
"carrotskin/pkg/database"
"carrotskin/pkg/logger"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// CreateProfile 创建档案
// ProfileHandler 档案处理器
type ProfileHandler struct {
container *container.Container
logger *zap.Logger
}
// NewProfileHandler 创建ProfileHandler实例
func NewProfileHandler(c *container.Container) *ProfileHandler {
return &ProfileHandler{
container: c,
logger: c.Logger,
}
}
// Create 创建档案
// @Summary 创建Minecraft档案
// @Description 创建新的Minecraft角色档案UUID由后端自动生成
// @Tags profile
@@ -18,12 +30,10 @@ import (
// @Produce json
// @Security BearerAuth
// @Param request body types.CreateProfileRequest true "档案信息(仅需提供角色名)"
// @Success 200 {object} model.Response{data=types.ProfileInfo} "创建成功返回完整档案信息含自动生成的UUID"
// @Failure 400 {object} model.ErrorResponse "请求参数错误或已达档案数量上限"
// @Failure 401 {object} model.ErrorResponse "未授权"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Success 200 {object} model.Response{data=types.ProfileInfo} "创建成功"
// @Failure 400 {object} model.ErrorResponse "请求参数错误"
// @Router /api/v1/profile [post]
func CreateProfile(c *gin.Context) {
func (h *ProfileHandler) Create(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
@@ -35,17 +45,15 @@ func CreateProfile(c *gin.Context) {
return
}
maxProfiles := service.GetMaxProfilesPerUser()
db := database.MustGetDB()
if err := service.CheckProfileLimit(db, userID, maxProfiles); err != nil {
maxProfiles := h.container.UserService.GetMaxProfilesPerUser()
if err := h.container.ProfileService.CheckLimit(userID, maxProfiles); err != nil {
RespondBadRequest(c, err.Error(), nil)
return
}
profile, err := service.CreateProfile(db, userID, req.Name)
profile, err := h.container.ProfileService.Create(userID, req.Name)
if err != nil {
logger.MustGetLogger().Error("创建档案失败",
h.logger.Error("创建档案失败",
zap.Int64("user_id", userID),
zap.String("name", req.Name),
zap.Error(err),
@@ -57,7 +65,7 @@ func CreateProfile(c *gin.Context) {
RespondSuccess(c, ProfileToProfileInfo(profile))
}
// GetProfiles 获取档案列表
// List 获取档案列表
// @Summary 获取档案列表
// @Description 获取当前用户的所有档案
// @Tags profile
@@ -65,18 +73,16 @@ func CreateProfile(c *gin.Context) {
// @Produce json
// @Security BearerAuth
// @Success 200 {object} model.Response "获取成功"
// @Failure 401 {object} model.ErrorResponse "未授权"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Router /api/v1/profile [get]
func GetProfiles(c *gin.Context) {
func (h *ProfileHandler) List(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
}
profiles, err := service.GetUserProfiles(database.MustGetDB(), userID)
profiles, err := h.container.ProfileService.GetByUserID(userID)
if err != nil {
logger.MustGetLogger().Error("获取档案列表失败",
h.logger.Error("获取档案列表失败",
zap.Int64("user_id", userID),
zap.Error(err),
)
@@ -87,7 +93,7 @@ func GetProfiles(c *gin.Context) {
RespondSuccess(c, ProfilesToProfileInfos(profiles))
}
// GetProfile 获取档案详情
// Get 获取档案详情
// @Summary 获取档案详情
// @Description 根据UUID获取档案详细信息
// @Tags profile
@@ -96,14 +102,17 @@ func GetProfiles(c *gin.Context) {
// @Param uuid path string true "档案UUID"
// @Success 200 {object} model.Response "获取成功"
// @Failure 404 {object} model.ErrorResponse "档案不存在"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Router /api/v1/profile/{uuid} [get]
func GetProfile(c *gin.Context) {
func (h *ProfileHandler) Get(c *gin.Context) {
uuid := c.Param("uuid")
if uuid == "" {
RespondBadRequest(c, "UUID不能为空", nil)
return
}
profile, err := service.GetProfileByUUID(database.MustGetDB(), uuid)
profile, err := h.container.ProfileService.GetByUUID(uuid)
if err != nil {
logger.MustGetLogger().Error("获取档案失败",
h.logger.Error("获取档案失败",
zap.String("uuid", uuid),
zap.Error(err),
)
@@ -114,7 +123,7 @@ func GetProfile(c *gin.Context) {
RespondSuccess(c, ProfileToProfileInfo(profile))
}
// UpdateProfile 更新档案
// Update 更新档案
// @Summary 更新档案
// @Description 更新档案信息
// @Tags profile
@@ -124,19 +133,19 @@ func GetProfile(c *gin.Context) {
// @Param uuid path string true "档案UUID"
// @Param request body types.UpdateProfileRequest true "更新信息"
// @Success 200 {object} model.Response "更新成功"
// @Failure 400 {object} model.ErrorResponse "请求参数错误"
// @Failure 401 {object} model.ErrorResponse "未授权"
// @Failure 403 {object} model.ErrorResponse "无权操作"
// @Failure 404 {object} model.ErrorResponse "档案不存在"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Router /api/v1/profile/{uuid} [put]
func UpdateProfile(c *gin.Context) {
func (h *ProfileHandler) Update(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
}
uuid := c.Param("uuid")
if uuid == "" {
RespondBadRequest(c, "UUID不能为空", nil)
return
}
var req types.UpdateProfileRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -149,9 +158,9 @@ func UpdateProfile(c *gin.Context) {
namePtr = &req.Name
}
profile, err := service.UpdateProfile(database.MustGetDB(), uuid, userID, namePtr, req.SkinID, req.CapeID)
profile, err := h.container.ProfileService.Update(uuid, userID, namePtr, req.SkinID, req.CapeID)
if err != nil {
logger.MustGetLogger().Error("更新档案失败",
h.logger.Error("更新档案失败",
zap.String("uuid", uuid),
zap.Int64("user_id", userID),
zap.Error(err),
@@ -163,7 +172,7 @@ func UpdateProfile(c *gin.Context) {
RespondSuccess(c, ProfileToProfileInfo(profile))
}
// DeleteProfile 删除档案
// Delete 删除档案
// @Summary 删除档案
// @Description 删除指定的Minecraft档案
// @Tags profile
@@ -172,22 +181,22 @@ func UpdateProfile(c *gin.Context) {
// @Security BearerAuth
// @Param uuid path string true "档案UUID"
// @Success 200 {object} model.Response "删除成功"
// @Failure 401 {object} model.ErrorResponse "未授权"
// @Failure 403 {object} model.ErrorResponse "无权操作"
// @Failure 404 {object} model.ErrorResponse "档案不存在"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Router /api/v1/profile/{uuid} [delete]
func DeleteProfile(c *gin.Context) {
func (h *ProfileHandler) Delete(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
}
uuid := c.Param("uuid")
if uuid == "" {
RespondBadRequest(c, "UUID不能为空", nil)
return
}
err := service.DeleteProfile(database.MustGetDB(), uuid, userID)
if err != nil {
logger.MustGetLogger().Error("删除档案失败",
if err := h.container.ProfileService.Delete(uuid, userID); err != nil {
h.logger.Error("删除档案失败",
zap.String("uuid", uuid),
zap.Int64("user_id", userID),
zap.Error(err),
@@ -199,7 +208,7 @@ func DeleteProfile(c *gin.Context) {
RespondSuccess(c, gin.H{"message": "删除成功"})
}
// SetActiveProfile 设置活跃档案
// SetActive 设置活跃档案
// @Summary 设置活跃档案
// @Description 将指定档案设置为活跃状态
// @Tags profile
@@ -208,22 +217,22 @@ func DeleteProfile(c *gin.Context) {
// @Security BearerAuth
// @Param uuid path string true "档案UUID"
// @Success 200 {object} model.Response "设置成功"
// @Failure 401 {object} model.ErrorResponse "未授权"
// @Failure 403 {object} model.ErrorResponse "无权操作"
// @Failure 404 {object} model.ErrorResponse "档案不存在"
// @Failure 500 {object} model.ErrorResponse "服务器错误"
// @Router /api/v1/profile/{uuid}/activate [post]
func SetActiveProfile(c *gin.Context) {
func (h *ProfileHandler) SetActive(c *gin.Context) {
userID, ok := GetUserIDFromContext(c)
if !ok {
return
}
uuid := c.Param("uuid")
if uuid == "" {
RespondBadRequest(c, "UUID不能为空", nil)
return
}
err := service.SetActiveProfile(database.MustGetDB(), uuid, userID)
if err != nil {
logger.MustGetLogger().Error("设置活跃档案失败",
if err := h.container.ProfileService.SetActive(uuid, userID); err != nil {
h.logger.Error("设置活跃档案失败",
zap.String("uuid", uuid),
zap.Int64("user_id", userID),
zap.Error(err),