Skip to content

Commit

Permalink
添加管理员获取会员详情接口
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed May 19, 2019
1 parent 5a945df commit 23c4921
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/admin_api.md
Expand Up @@ -37,6 +37,17 @@

</details>

<details><summary>获取指定会员详情<code>[GET] /v1/user/u/:user_id</code></summary>


<p>

获取指定会员详情

</p>

</details>

<details><summary>修改会员密码<code>[PUT] /v1/user/u/:user_id/password</code></summary>


Expand Down
92 changes: 91 additions & 1 deletion src/controller/user/profile.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions src/controller/user/profile_test.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/router_admin.go
Expand Up @@ -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: 删除会员信息
}
Expand Down

0 comments on commit 23c4921

Please sign in to comment.