Files
cms-server/api/user/update.go
Asai Neko 2f3eaf17ea
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Add agenda and stats and event management service with api
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-03-26 16:15:07 +08:00

103 lines
3.2 KiB
Go

package user
import (
"errors"
"nixcn-cms/internal/exception"
"nixcn-cms/service/service_user"
"nixcn-cms/tracer"
"nixcn-cms/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// Update modifies the profile information for the currently authenticated user.
//
// @Summary Update User Information
// @Description Updates specific profile fields such as username, nickname, subtitle, avatar (URL), and bio (Base64).
// @Description Validation: Username (5-255 chars), Nickname (max 24 chars), Subtitle (max 32 chars).
// @Tags User
// @Accept json
// @Produce json
// @Security Bearer
// @Param payload body service_user.UserInfoUpdateData true "Updated User Profile Data"
// @Success 200 {object} utils.RespStatus{data=nil} "Successful profile update"
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
// @Failure 401 {object} utils.RespStatus{data=nil} "Unauthorized"
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error"
// @Router /user/update [patch]
func (self *UserHandler) Update(c *gin.Context) {
ctx, span := tracer.StartSpan(
c.Request.Context(),
"api_user",
"update",
)
defer span.End()
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointUserUpdate)
ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint)
userIdOrig, ok := c.Get("user_id")
if !ok {
errorCode := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorMissingUserId),
exception.WithError(errors.New("Missing UserId")),
).Throw(ctx).String()
utils.HttpResponse(c, 403, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
errorCode := exception.New(
exception.WithStatus(exception.StatusServer),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
exception.WithError(err),
).Throw(ctx).String()
utils.HttpResponse(c, 500, errorCode)
return
}
permissionLevelOrig, ok := c.Get("permission_level")
if !ok {
errorCode := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorPermissionDenied),
exception.WithError(errors.New("Missing PermissionLevel")),
).Throw(ctx).String()
utils.HttpResponse(c, 403, errorCode)
return
}
payload := &service_user.UserInfoPayload{
Context: ctx,
UserId: userId,
OperatorId: userId,
OperatorLevel: permissionLevelOrig.(uint),
}
if err := c.ShouldBindJSON(&payload.Data); err != nil {
errorCode := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorInvalidInput),
exception.WithError(err),
).Throw(ctx).String()
utils.HttpResponse(c, 400, errorCode)
return
}
result := self.svc.UpdateInfo(payload)
if result.Common.Exception.Original != exception.CommonSuccess {
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
return
}
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
}