WIP: Full restruct, seprate service and api

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2026-01-24 11:42:35 +08:00
parent dfd5532b20
commit 8e11ba4631
31 changed files with 830 additions and 248 deletions

View File

@@ -1,44 +1,40 @@
package user
import (
"net/url"
"nixcn-cms/data"
"nixcn-cms/internal/cryptography"
"nixcn-cms/internal/exception"
"nixcn-cms/utils"
"unicode/utf8"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func Update(c *gin.Context) {
// New user model
userIdOrig, ok := c.Get("user_id")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorMissingUserId).
Build(c)
utils.HttpResponse(c, 403, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
SetError(err).
Build(c)
utils.HttpResponse(c, 500, errorCode)
return
}
// userIdOrig, ok := c.Get("user_id")
// if !ok {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorMissingUserId).
// Build(c)
// utils.HttpResponse(c, 403, errorCode)
// return
// }
// userId, err := uuid.Parse(userIdOrig.(string))
// if err != nil {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusServer).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorUuidParseFailed).
// SetError(err).
// Build(c)
// utils.HttpResponse(c, 500, errorCode)
// return
// }
var ReqInfo data.User
err = c.ShouldBindJSON(&ReqInfo)
@@ -79,92 +75,87 @@ func Update(c *gin.Context) {
// utils.HttpResponse(c, 400, "", "invilad user name")
// return
if ReqInfo.Username != "" {
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build(c)
utils.HttpResponse(c, 400, errorCode)
return
}
userData.Username = ReqInfo.Username
}
// if ReqInfo.Username != "" {
// if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorInvalidInput).
// Build(c)
// utils.HttpResponse(c, 400, errorCode)
// return
// }
// userData.Username = ReqInfo.Username
// }
if ReqInfo.Nickname != "" {
if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build(c)
utils.HttpResponse(c, 400, errorCode)
return
}
userData.Nickname = ReqInfo.Nickname
}
// if ReqInfo.Nickname != "" {
// if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorInvalidInput).
// Build(c)
// utils.HttpResponse(c, 400, errorCode)
// return
// }
// userData.Nickname = ReqInfo.Nickname
// }
if ReqInfo.Subtitle != "" {
if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build(c)
utils.HttpResponse(c, 400, errorCode)
return
}
userData.Subtitle = ReqInfo.Subtitle
}
// if ReqInfo.Subtitle != "" {
// if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorInvalidInput).
// Build(c)
// utils.HttpResponse(c, 400, errorCode)
// return
// }
// userData.Subtitle = ReqInfo.Subtitle
// }
if ReqInfo.Avatar != "" {
_, err := url.ParseRequestURI(ReqInfo.Avatar)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(err).
Build(c)
utils.HttpResponse(c, 400, errorCode)
return
}
userData.Avatar = ReqInfo.Avatar
}
// if ReqInfo.Avatar != "" {
// _, err := url.ParseRequestURI(ReqInfo.Avatar)
// if err != nil {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorInvalidInput).
// SetError(err).
// Build(c)
// utils.HttpResponse(c, 400, errorCode)
// return
// }
// userData.Avatar = ReqInfo.Avatar
// }
if ReqInfo.Bio != "" {
if !cryptography.IsBase64Std(ReqInfo.Bio) {
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build(c)
utils.HttpResponse(c, 400, errorCode)
return
}
userData.Bio = ReqInfo.Bio
}
// if ReqInfo.Bio != "" {
// if !cryptography.IsBase64Std(ReqInfo.Bio) {
// errorCode := new(exception.Builder).
// SetStatus(exception.StatusUser).
// SetService(exception.ServiceUser).
// SetEndpoint(exception.EndpointUserServiceUpdate).
// SetType(exception.TypeCommon).
// SetOriginal(exception.CommonErrorInvalidInput).
// Build(c)
// utils.HttpResponse(c, 400, errorCode)
// return
// }
// userData.Bio = ReqInfo.Bio
// }
// Update user info
userData.UpdateByUserID(c, userId)
errorCode := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceUpdate).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
errorCode :=
Build(c)
utils.HttpResponse(c, 200, errorCode)
}