Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherry-pick support query user info by username or uuid in the same api #155

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ run_rproxy_local.sh
run_server_local.sh
run_user_local.sh
cmd/csghub-server/__debug_*
*/*/_api_test/
29 changes: 26 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8392,7 +8392,7 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "username",
"description": "username or uuid, defined by the query string 'type'",
"name": "username",
"in": "path",
"required": true
Expand All @@ -8401,8 +8401,17 @@ const docTemplate = `{
"type": "string",
"description": "current user",
"name": "current_user",
"in": "query",
"required": true
"in": "query"
},
{
"enum": [
"username",
"uuid"
],
"type": "string",
"description": "path param is usernam or uuid, default to username",
"name": "type",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -15481,6 +15490,17 @@ const docTemplate = `{
"UnknownRepo"
]
},
"types.ResourceType": {
"type": "string",
"enum": [
"cpu",
"gpu"
],
"x-enum-varnames": [
"ResourceTypeCPU",
"ResourceTypeGPU"
]
},
"types.Response": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -15712,6 +15732,9 @@ const docTemplate = `{
},
"resources": {
"type": "string"
},
"type": {
"$ref": "#/definitions/types.ResourceType"
}
}
},
Expand Down
29 changes: 26 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8381,7 +8381,7 @@
"parameters": [
{
"type": "string",
"description": "username",
"description": "username or uuid, defined by the query string 'type'",
"name": "username",
"in": "path",
"required": true
Expand All @@ -8390,8 +8390,17 @@
"type": "string",
"description": "current user",
"name": "current_user",
"in": "query",
"required": true
"in": "query"
},
{
"enum": [
"username",
"uuid"
],
"type": "string",
"description": "path param is usernam or uuid, default to username",
"name": "type",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -15470,6 +15479,17 @@
"UnknownRepo"
]
},
"types.ResourceType": {
"type": "string",
"enum": [
"cpu",
"gpu"
],
"x-enum-varnames": [
"ResourceTypeCPU",
"ResourceTypeGPU"
]
},
"types.Response": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -15701,6 +15721,9 @@
},
"resources": {
"type": "string"
},
"type": {
"$ref": "#/definitions/types.ResourceType"
}
}
},
Expand Down
20 changes: 18 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,14 @@ definitions:
- SpaceRepo
- CodeRepo
- UnknownRepo
types.ResourceType:
enum:
- cpu
- gpu
type: string
x-enum-varnames:
- ResourceTypeCPU
- ResourceTypeGPU
types.Response:
properties:
data: {}
Expand Down Expand Up @@ -1898,6 +1906,8 @@ definitions:
type: string
resources:
type: string
type:
$ref: '#/definitions/types.ResourceType'
type: object
types.SpaceSdk:
properties:
Expand Down Expand Up @@ -9196,15 +9206,21 @@ paths:
consumes:
- application/json
parameters:
- description: username
- description: username or uuid, defined by the query string 'type'
in: path
name: username
required: true
type: string
- description: current user
in: query
name: current_user
required: true
type: string
- description: path param is usernam or uuid, default to username
enum:
- username
- uuid
in: query
name: type
type: string
produces:
- application/json
Expand Down
19 changes: 12 additions & 7 deletions user/component/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,18 @@ func (c *UserComponent) CanAdmin(ctx context.Context, username string) (bool, er
return user.CanAdmin(), nil
}

func (c *UserComponent) Get(ctx context.Context, userName, visitorName string) (*types.User, error) {
func (c *UserComponent) Get(ctx context.Context, userNameOrUUID, visitorName string, useUUID bool) (*types.User, error) {
var dbuser = new(database.User)
var err error
if useUUID {
dbuser, err = c.us.FindByUUID(ctx, userNameOrUUID)
} else {
*dbuser, err = c.us.FindByUsername(ctx, userNameOrUUID)
}
if err != nil {
return nil, fmt.Errorf("failed to find user by name or uuid '%s' in db,error:%w", userNameOrUUID, err)
}
userName := dbuser.Username
var onlyBasicInfo bool
//allow anonymous user to get basic info
if visitorName == "" {
Expand All @@ -360,12 +371,6 @@ func (c *UserComponent) Get(ctx context.Context, userName, visitorName string) (
}
}

dbuser, err := c.us.FindByUsername(ctx, userName)
if err != nil {
newError := fmt.Errorf("failed to find user by name in db,error:%w", err)
return nil, newError
}

u := types.User{
Username: dbuser.Username,
Nickname: dbuser.NickName,
Expand Down
12 changes: 7 additions & 5 deletions user/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,25 @@ func (h *UserHandler) Delete(ctx *gin.Context) {
// @Tags User
// @Accept json
// @Produce json
// @Param username path string true "username"
// @Param current_user query string true "current user"
// @Param username path string true "username or uuid, defined by the query string 'type'"
// @Param current_user query string false "current user"
// @Param type query string false "path param is usernam or uuid, default to username" Enums(username, uuid)
// @Success 200 {object} types.Response{data=types.User} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /user/{username} [get]
func (h *UserHandler) Get(ctx *gin.Context) {
visitorName := httpbase.GetCurrentUser(ctx)
userName := ctx.Param("username")
user, err := h.c.Get(ctx, userName, visitorName)
userNameOrUUID := ctx.Param("username")
useUUID := ctx.Query("type") == "uuid"
user, err := h.c.Get(ctx, userNameOrUUID, visitorName, useUUID)
if err != nil {
slog.Error("Failed to get user", slog.Any("error", err))
httpbase.ServerError(ctx, err)
return
}

slog.Info("Get user succeed", slog.String("userName", userName))
slog.Info("Get user succeed", slog.String("userName", userNameOrUUID))
httpbase.OK(ctx, user)
}

Expand Down