From 8938fa052bdd344d87e4e481069a572aefd4a6e1 Mon Sep 17 00:00:00 2001 From: Asai Neko Date: Sat, 31 Jan 2026 00:24:26 +0800 Subject: [PATCH] Add user get other info api Signed-off-by: Asai Neko --- api/user/handler.go | 1 + api/user/info.go | 1 + api/user/other.go | 58 +++++++++++++++++++++++++++ service/service_user/get_user_info.go | 24 +++++++++++ 4 files changed, 84 insertions(+) create mode 100644 api/user/other.go diff --git a/api/user/handler.go b/api/user/handler.go index 9327ab1..1412cbd 100644 --- a/api/user/handler.go +++ b/api/user/handler.go @@ -17,6 +17,7 @@ func ApiHandler(r *gin.RouterGroup) { r.Use(middleware.ApiVersionCheck(), middleware.JWTAuth(), middleware.Permission(5)) r.GET("/info", userHandler.Info) + r.GET("/info/*user_id", userHandler.Other) r.PATCH("/update", userHandler.Update) r.GET("/list", middleware.Permission(20), userHandler.List) r.POST("/create", middleware.Permission(50), userHandler.Create) diff --git a/api/user/info.go b/api/user/info.go index b22faa8..0cf1c23 100644 --- a/api/user/info.go +++ b/api/user/info.go @@ -55,6 +55,7 @@ func (self *UserHandler) Info(c *gin.Context) { UserInfoPayload := &service_user.UserInfoPayload{ Context: c, UserId: userId, + IsOther: false, Data: nil, } diff --git a/api/user/other.go b/api/user/other.go new file mode 100644 index 0000000..ee84d7f --- /dev/null +++ b/api/user/other.go @@ -0,0 +1,58 @@ +package user + +import ( + "nixcn-cms/internal/exception" + "nixcn-cms/service/service_user" + "nixcn-cms/utils" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +// Info retrieves the profile information of the other user. +// +// @Summary Get Other User Information +// @Description Fetches the complete profile data for the user associated with the provided session/token. +// @Tags User +// @Accept json +// @Produce json +// @Success 200 {object} utils.RespStatus{data=service_user.UserInfoData} "Successful profile retrieval" +// @Failure 403 {object} utils.RespStatus{data=nil} "Missing User ID / Unauthorized" +// @Failure 404 {object} utils.RespStatus{data=nil} "User Not Found" +// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error (UUID Parse Failed)" +// @Security ApiKeyAuth +// @Router /user/info [get] +func (self *UserHandler) Other(c *gin.Context) { + userIdOrig := c.Param("user_id") + + userId, err := uuid.Parse(userIdOrig) + if err != nil { + errorCode := new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceUser). + SetEndpoint(exception.EndpointUserServiceInfo). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorUuidParseFailed). + SetError(err). + Throw(c). + String() + utils.HttpResponse(c, 500, errorCode) + return + } + + UserInfoPayload := &service_user.UserInfoPayload{ + Context: c, + UserId: userId, + IsOther: true, + Data: nil, + } + + result := self.svc.GetUserInfo(UserInfoPayload) + + 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(), result.Data) +} diff --git a/service/service_user/get_user_info.go b/service/service_user/get_user_info.go index 05c1011..ca5e593 100644 --- a/service/service_user/get_user_info.go +++ b/service/service_user/get_user_info.go @@ -24,6 +24,7 @@ type UserInfoData struct { type UserInfoPayload struct { Context context.Context UserId uuid.UUID + IsOther bool Data *UserInfoData } @@ -63,6 +64,29 @@ func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *User return } + if payload.IsOther { + if !userData.AllowPublic { + exception := new(exception.Builder). + SetStatus(exception.StatusUser). + SetService(exception.ServiceUser). + SetEndpoint(exception.EndpointUserServiceInfo). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorUserNotFound). + SetError(err). + Throw(payload.Context) + + result = &UserInfoResult{ + Common: shared.CommonResult{ + HttpCode: 404, + Exception: exception, + }, + Data: nil, + } + + return + } + } + exception := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceUser).