From 23c4921c55f513f07229f8b253a70dcb03f58067 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 19 May 2019 12:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=BC=9A=E5=91=98=E8=AF=A6=E6=83=85=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/admin_api.md | 11 ++++ src/controller/user/profile.go | 92 ++++++++++++++++++++++++++++- src/controller/user/profile_test.go | 82 +++++++++++++++++++++++++ src/router_admin.go | 2 +- 4 files changed, 185 insertions(+), 2 deletions(-) diff --git a/docs/admin_api.md b/docs/admin_api.md index f76b1391d..b7ed176b2 100644 --- a/docs/admin_api.md +++ b/docs/admin_api.md @@ -37,6 +37,17 @@ +
获取指定会员详情[GET] /v1/user/u/:user_id + + +

+ +获取指定会员详情 + +

+ +
+
修改会员密码[PUT] /v1/user/u/:user_id/password diff --git a/src/controller/user/profile.go b/src/controller/user/profile.go index 68e153e6f..c63ad0a77 100644 --- a/src/controller/user/profile.go +++ b/src/controller/user/profile.go @@ -61,7 +61,76 @@ func GetProfile(context controller.Context) (res schema.Response) { user := model.User{Id: context.Uid} - if err = tx.Where(&user).Last(&user).Error; err != nil { + if err = tx.Last(&user).Error; err != nil { + if err == gorm.ErrRecordNotFound { + err = exception.UserNotExist + } + return + } + + if err = mapstructure.Decode(user, &data.ProfilePure); err != nil { + return + } + + data.PayPassword = user.PayPassword != nil && len(*user.PayPassword) != 0 + data.CreatedAt = user.CreatedAt.Format(time.RFC3339Nano) + data.UpdatedAt = user.UpdatedAt.Format(time.RFC3339Nano) + + return +} + +func GetProfileByAdmin(context controller.Context, userId string) (res schema.Response) { + var ( + err error + data schema.Profile + tx *gorm.DB + ) + + defer func() { + if r := recover(); r != nil { + switch t := r.(type) { + case string: + err = errors.New(t) + case error: + err = t + default: + err = exception.Unknown + } + } + + if tx != nil { + if err != nil { + _ = tx.Rollback().Error + } else { + err = tx.Commit().Error + } + } + + if err != nil { + res.Message = err.Error() + res.Data = nil + } else { + res.Data = data + res.Status = schema.StatusSuccess + } + }() + + tx = service.Db.Begin() + + adminInfo := model.Admin{ + Id: context.Uid, + } + + if err = tx.Last(&adminInfo).Error; err != nil { + if err == gorm.ErrRecordNotFound { + err = exception.AdminNotExist + } + return + } + + user := model.User{Id: userId} + + if err = tx.Last(&user).Error; err != nil { if err == gorm.ErrRecordNotFound { err = exception.UserNotExist } @@ -98,6 +167,27 @@ func GetProfileRouter(context *gin.Context) { }) } +func GetProfileByAdminRouter(context *gin.Context) { + var ( + err error + res = schema.Response{} + ) + + defer func() { + if err != nil { + res.Data = nil + res.Message = err.Error() + } + context.JSON(http.StatusOK, res) + }() + + userId := context.Param("user_id") + + res = GetProfileByAdmin(controller.Context{ + Uid: context.GetString(middleware.ContextUidField), + }, userId) +} + func UpdateProfile(context controller.Context, input UpdateProfileParams) (res schema.Response) { var ( err error diff --git a/src/controller/user/profile_test.go b/src/controller/user/profile_test.go index e2984dc64..e3a849958 100644 --- a/src/controller/user/profile_test.go +++ b/src/controller/user/profile_test.go @@ -3,6 +3,9 @@ package user_test import ( "encoding/json" "fmt" + "github.com/axetroy/go-server/src/controller" + "github.com/axetroy/go-server/src/controller/auth" + "github.com/axetroy/go-server/src/controller/user" "github.com/axetroy/go-server/src/exception" "github.com/axetroy/go-server/src/schema" "github.com/axetroy/go-server/src/util" @@ -67,6 +70,41 @@ func TestGetProfileWithInvalidToken(t *testing.T) { func TestGetProfile(t *testing.T) { userInfo, _ := tester.CreateUser() + defer auth.DeleteUserByUserName(userInfo.Username) + + r := user.GetProfile(controller.Context{Uid: userInfo.Id}) + + profile := schema.Profile{} + + assert.Nil(t, tester.Decode(r.Data, &profile)) + + assert.Equal(t, userInfo.Id, profile.Id) + assert.Equal(t, userInfo.Username, profile.Username) + assert.Equal(t, userInfo.CreatedAt, profile.CreatedAt) +} + +func TestGetProfileByAdmin(t *testing.T) { + adminInfo, _ := tester.LoginAdmin() + userInfo, _ := tester.CreateUser() + + defer auth.DeleteUserByUserName(userInfo.Username) + + r := user.GetProfileByAdmin(controller.Context{Uid: adminInfo.Id}, userInfo.Id) + + profile := schema.Profile{} + + assert.Nil(t, tester.Decode(r.Data, &profile)) + + assert.Equal(t, userInfo.Id, profile.Id) + assert.Equal(t, userInfo.Username, profile.Username) + assert.Equal(t, userInfo.CreatedAt, profile.CreatedAt) +} + +func TestGetProfileRouter(t *testing.T) { + userInfo, _ := tester.CreateUser() + + defer auth.DeleteUserByUserName(userInfo.Username) + header := mocker.Header{ "Authorization": util.TokenPrefix + " " + userInfo.Token, } @@ -104,3 +142,47 @@ func TestGetProfile(t *testing.T) { return } } + +func TestGetProfileByAdminRouter(t *testing.T) { + adminInfo, _ := tester.LoginAdmin() + userInfo, _ := tester.CreateUser() + + defer auth.DeleteUserByUserName(userInfo.Username) + + header := mocker.Header{ + "Authorization": util.TokenPrefix + " " + adminInfo.Token, + } + + r := tester.HttpAdmin.Get("/v1/user/u/"+userInfo.Id, nil, &header) + + if !assert.Equal(t, http.StatusOK, r.Code) { + return + } + + res := schema.Response{} + + if !assert.Nil(t, json.Unmarshal([]byte(r.Body.String()), &res)) { + return + } + + if !assert.Equal(t, schema.StatusSuccess, res.Status) { + fmt.Println(res.Message) + return + } + if !assert.Equal(t, "", res.Message) { + return + } + + profile := schema.Profile{} + + if assert.Nil(t, tester.Decode(res.Data, &profile)) { + return + } + + if !assert.Equal(t, userInfo.Id, profile.Id) { + return + } + if !assert.Equal(t, userInfo.Username, *profile.Email) { + return + } +} diff --git a/src/router_admin.go b/src/router_admin.go index d2d9b25e4..357fca35a 100644 --- a/src/router_admin.go +++ b/src/router_admin.go @@ -61,7 +61,7 @@ func init() { { userRouter.GET("", user.GetListRouter) // 获取会员列表 userRouter.PUT("/u/:user_id/password", user.UpdatePasswordByAdminRouter) // 修改会员密码 - userRouter.GET("/u/:user_id", admin.GetAdminInfoRouter) // TODO: 获取单个会员的信息 + userRouter.GET("/u/:user_id", user.GetProfileByAdminRouter) // 获取单个会员的信息 userRouter.PUT("/u/:user_id", admin.GetAdminInfoRouter) // TODO: 更新会员信息 userRouter.DELETE("/u/:user_id", admin.GetAdminInfoRouter) // TODO: 删除会员信息 }