2025-11-28 23:30:49 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-02 19:43:39 +08:00
|
|
|
|
"carrotskin/internal/container"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
"carrotskin/internal/types"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// 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 创建档案
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Summary 创建Minecraft档案
|
|
|
|
|
|
// @Description 创建新的Minecraft角色档案,UUID由后端自动生成
|
|
|
|
|
|
// @Tags profile
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param request body types.CreateProfileRequest true "档案信息(仅需提供角色名)"
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// @Success 200 {object} model.Response{data=types.ProfileInfo} "创建成功"
|
|
|
|
|
|
// @Failure 400 {object} model.ErrorResponse "请求参数错误"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Router /api/v1/profile [post]
|
2025-12-02 19:43:39 +08:00
|
|
|
|
func (h *ProfileHandler) Create(c *gin.Context) {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
userID, ok := GetUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req types.CreateProfileRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondBadRequest(c, "请求参数错误: "+err.Error(), nil)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
maxProfiles := h.container.UserService.GetMaxProfilesPerUser()
|
2025-12-02 22:52:33 +08:00
|
|
|
|
if err := h.container.ProfileService.CheckLimit(c.Request.Context(), userID, maxProfiles); err != nil {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondBadRequest(c, err.Error(), nil)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
profile, err := h.container.ProfileService.Create(c.Request.Context(), userID, req.Name)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
if err != nil {
|
2025-12-02 19:43:39 +08:00
|
|
|
|
h.logger.Error("创建档案失败",
|
2025-12-02 10:33:19 +08:00
|
|
|
|
zap.Int64("user_id", userID),
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.String("name", req.Name),
|
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
|
)
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondServerError(c, err.Error(), nil)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondSuccess(c, ProfileToProfileInfo(profile))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// List 获取档案列表
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Summary 获取档案列表
|
|
|
|
|
|
// @Description 获取当前用户的所有档案
|
|
|
|
|
|
// @Tags profile
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Success 200 {object} model.Response{data=[]types.ProfileInfo} "获取成功"
|
|
|
|
|
|
// @Failure 500 {object} model.ErrorResponse "服务器错误"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Router /api/v1/profile [get]
|
2025-12-02 19:43:39 +08:00
|
|
|
|
func (h *ProfileHandler) List(c *gin.Context) {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
userID, ok := GetUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
profiles, err := h.container.ProfileService.GetByUserID(c.Request.Context(), userID)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
if err != nil {
|
2025-12-02 19:43:39 +08:00
|
|
|
|
h.logger.Error("获取档案列表失败",
|
2025-12-02 10:33:19 +08:00
|
|
|
|
zap.Int64("user_id", userID),
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.Error(err),
|
|
|
|
|
|
)
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondServerError(c, err.Error(), nil)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondSuccess(c, ProfilesToProfileInfos(profiles))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// Get 获取档案详情
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Summary 获取档案详情
|
|
|
|
|
|
// @Description 根据UUID获取档案详细信息
|
|
|
|
|
|
// @Tags profile
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param uuid path string true "档案UUID"
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Success 200 {object} model.Response{data=types.ProfileInfo} "获取成功"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Failure 404 {object} model.ErrorResponse "档案不存在"
|
|
|
|
|
|
// @Router /api/v1/profile/{uuid} [get]
|
2025-12-02 19:43:39 +08:00
|
|
|
|
func (h *ProfileHandler) Get(c *gin.Context) {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
uuid := c.Param("uuid")
|
2025-12-02 19:43:39 +08:00
|
|
|
|
if uuid == "" {
|
|
|
|
|
|
RespondBadRequest(c, "UUID不能为空", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-28 23:30:49 +08:00
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
profile, err := h.container.ProfileService.GetByUUID(c.Request.Context(), uuid)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
if err != nil {
|
2025-12-02 19:43:39 +08:00
|
|
|
|
h.logger.Error("获取档案失败",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.String("uuid", uuid),
|
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
|
)
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondNotFound(c, err.Error())
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondSuccess(c, ProfileToProfileInfo(profile))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// Update 更新档案
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Summary 更新档案
|
|
|
|
|
|
// @Description 更新档案信息
|
|
|
|
|
|
// @Tags profile
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param uuid path string true "档案UUID"
|
|
|
|
|
|
// @Param request body types.UpdateProfileRequest true "更新信息"
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Success 200 {object} model.Response{data=types.ProfileInfo} "更新成功"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Failure 403 {object} model.ErrorResponse "无权操作"
|
|
|
|
|
|
// @Router /api/v1/profile/{uuid} [put]
|
2025-12-02 19:43:39 +08:00
|
|
|
|
func (h *ProfileHandler) Update(c *gin.Context) {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
userID, ok := GetUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
uuid := c.Param("uuid")
|
2025-12-02 19:43:39 +08:00
|
|
|
|
if uuid == "" {
|
|
|
|
|
|
RespondBadRequest(c, "UUID不能为空", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 10:33:19 +08:00
|
|
|
|
|
2025-11-28 23:30:49 +08:00
|
|
|
|
var req types.UpdateProfileRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondBadRequest(c, "请求参数错误: "+err.Error(), nil)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var namePtr *string
|
|
|
|
|
|
if req.Name != "" {
|
|
|
|
|
|
namePtr = &req.Name
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
profile, err := h.container.ProfileService.Update(c.Request.Context(), uuid, userID, namePtr, req.SkinID, req.CapeID)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
if err != nil {
|
2025-12-02 19:43:39 +08:00
|
|
|
|
h.logger.Error("更新档案失败",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.String("uuid", uuid),
|
2025-12-02 10:33:19 +08:00
|
|
|
|
zap.Int64("user_id", userID),
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.Error(err),
|
|
|
|
|
|
)
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondWithError(c, err)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondSuccess(c, ProfileToProfileInfo(profile))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:43:39 +08:00
|
|
|
|
// Delete 删除档案
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Summary 删除档案
|
|
|
|
|
|
// @Description 删除指定的Minecraft档案
|
|
|
|
|
|
// @Tags profile
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param uuid path string true "档案UUID"
|
2025-12-26 01:15:17 +08:00
|
|
|
|
// @Success 200 {object} model.Response{data=map[string]string} "删除成功"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
// @Failure 403 {object} model.ErrorResponse "无权操作"
|
|
|
|
|
|
// @Router /api/v1/profile/{uuid} [delete]
|
2025-12-02 19:43:39 +08:00
|
|
|
|
func (h *ProfileHandler) Delete(c *gin.Context) {
|
2025-12-02 10:33:19 +08:00
|
|
|
|
userID, ok := GetUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
uuid := c.Param("uuid")
|
2025-12-02 19:43:39 +08:00
|
|
|
|
if uuid == "" {
|
|
|
|
|
|
RespondBadRequest(c, "UUID不能为空", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 10:33:19 +08:00
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
if err := h.container.ProfileService.Delete(c.Request.Context(), uuid, userID); err != nil {
|
2025-12-02 19:43:39 +08:00
|
|
|
|
h.logger.Error("删除档案失败",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.String("uuid", uuid),
|
2025-12-02 10:33:19 +08:00
|
|
|
|
zap.Int64("user_id", userID),
|
2025-11-28 23:30:49 +08:00
|
|
|
|
zap.Error(err),
|
|
|
|
|
|
)
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondWithError(c, err)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
|
RespondSuccess(c, gin.H{"message": "删除成功"})
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|