作者 陈爱青
提交者 yangfu

[add] 用户

... ... @@ -13,6 +13,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/moul/http2curl v1.0.0 // indirect
... ...
... ... @@ -132,6 +132,8 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type ConvertUserStatusCommand struct {
// 用户id
UserId int64 `json:"userId,omitempty"`
// 状态 1正常 2禁用
Status int64 `json:"status" valid:"Required"`
}
func (convertUserStatusCommand *ConvertUserStatusCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (convertUserStatusCommand *ConvertUserStatusCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(convertUserStatusCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"time"
"github.com/beego/beego/v2/core/validation"
)
type CreateUserCommand struct {
// 1.高管 2.合伙人 4:游客
UserType int `json:"userType" valid:"Required"`
// 管理员类型 1.超级管理员 10:企业管理员 100:普通用户
AdminType int `json:"adminType" valid:"Required"`
// 状态 1正常 2禁用
Status int64 `json:"status" valid:"Required"`
// 是否是公司负责人
IsPrincipal bool `json:"isPrincipal" valid:"Required"`
// 统一用户id
Uid int64 `json:"uid" valid:"Required"`
// 用户账号
UserAccount string `json:"userAccount" valid:"Required"`
// 用户头像URL
UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
// 用户名称
UserName string `json:"userName" valid:"Required"`
// 邮件地址
Email string `json:"email" valid:"Required"`
// 性别
Gender int `json:"gender" valid:"Required"`
// 入职时间
EntryTime time.Time `json:"entryTime" valid:"Required"`
// 分机
Extension string `json:"extension" valid:"Required"`
// 工作地
Workplace string `json:"workplace" valid:"Required"`
// 私人电话
PrivateNumber string `json:"privateNumber" valid:"Required"`
// 工号
JobNumber string `json:"jobNumber" valid:"Required"`
// 合伙人账号
PartnerAccount string `json:"partnerAccount" valid:"Required"`
// 合伙人姓名
PartnerName string `json:"partnerName" valid:"Required"`
// 区域名称 eg:华南地区
RegionName string `json:"regionName,omitempty"`
// 合伙时间
CooperateTime time.Time `json:"cooperateTime" valid:"Required"`
// 业务员
Salesmans []*domain.Salesman `json:"salesmans,omitempty"`
// 用户信息
UserInfo *domain.UserInfo `json:"userInfo,omitempty"`
//合伙人信息
PartnerInfo *domain.PartnerInfo `json:"partnerInfo"`
//// 合伙人类型
PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"`
// 可查看合伙人列表
AccessPartners []int64 `json:"accessPartners,omitempty"`
}
func (createUserCommand *CreateUserCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createUserCommand *CreateUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createUserCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type RemoveUserCommand struct {
// 用户id
UserId int64 `json:"userId" valid:"Required"`
}
func (removeUserCommand *RemoveUserCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeUserCommand *RemoveUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeUserCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type SetPermissionCommand struct {
// 用户id
UserId int64 `json:"userId" valid:"Required"`
// 权限编码列表
Permissons []int `json:"permissons,omitempty"`
}
func (setPermissionCommand *SetPermissionCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (setPermissionCommand *SetPermissionCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(setPermissionCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"time"
"github.com/beego/beego/v2/core/validation"
)
type UpdateUserCommand struct {
// 用户id
UserId int64 `json:"userId" valid:"Required"`
// 1.高管 2.合伙人 4:游客
UserType int `json:"userType,omitempty"`
// 管理员类型 1.超级管理员 10:企业管理员 100:普通用户
AdminType int `json:"adminType,omitempty"`
// 状态 1正常 2禁用
Status int64 `json:"status" valid:"Required"`
// 是否是公司负责人
IsPrincipal bool `json:"isPrincipal" valid:"Required"`
// 统一用户id
Uid int64 `json:"uid" valid:"Required"`
// 用户账号
UserAccount string `json:"userAccount" valid:"Required"`
// 用户头像URL
UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
// 用户名称
UserName string `json:"userName" valid:"Required"`
// 邮件地址
Email string `json:"email" valid:"Required"`
// 性别
Gender int `json:"gender" valid:"Required"`
// 入职时间
EntryTime time.Time `json:"entryTime" valid:"Required"`
// 分机
Extension string `json:"extension" valid:"Required"`
// 工作地
Workplace string `json:"workplace" valid:"Required"`
// 私人电话
PrivateNumber string `json:"privateNumber" valid:"Required"`
// 工号
JobNumber string `json:"jobNumber" valid:"Required"`
// 合伙人账号
PartnerAccount string `json:"partnerAccount" valid:"Required"`
// 合伙人姓名
PartnerName string `json:"partnerName" valid:"Required"`
// 区域名称 eg:华南地区
RegionName string `json:"regionName,omitempty"`
// 合伙时间
CooperateTime time.Time `json:"cooperateTime" valid:"Required"`
// 业务员
Salesmans []*domain.Salesman `json:"salesmans,omitempty"`
// 合伙人类型
PartnerCategorys []*domain.PartnerCategory `json:"partnerCategorys,omitempty"`
// 可查看合伙人列表
AccessPartners []int64 `json:"accessPartners,omitempty"`
}
func (updateUserCommand *UpdateUserCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateUserCommand *UpdateUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateUserCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type GetUserQuery struct {
// 用户id
UserId int64 `json:"userId" valid:"Required"`
}
func (getUserQuery *GetUserQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getUserQuery *GetUserQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getUserQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type ListUserQuery struct {
// 查询偏离量
Offset int `json:"offset" valid:"Required"`
// 查询限制
Limit int `json:"limit" valid:"Required"`
}
func (listUserQuery *ListUserQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listUserQuery *ListUserQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listUserQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
// 用户管理服务
type UserService struct {
}
// 用户状态转换(禁用、启用)
func (userService *UserService) ConvertUserStatus(convertUserStatusCommand *command.ConvertUserStatusCommand) (interface{}, error) {
if err := convertUserStatusCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 创建
func (userService *UserService) CreateUser(createUserCommand *command.CreateUserCommand) (interface{}, error) {
if err := createUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
newUser := &domain.User{
UserType: createUserCommand.UserType,
AdminType: createUserCommand.AdminType,
Status: createUserCommand.Status,
UserInfo: createUserCommand.UserInfo,
PartnerInfo: createUserCommand.PartnerInfo,
AccessPartners: createUserCommand.AccessPartners,
}
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
if user, err := userRepository.Save(newUser); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
}
}
// 返回
func (userService *UserService) GetUser(getUserQuery *query.GetUserQuery) (interface{}, error) {
if err := getUserQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
user, err := userRepository.FindOne(map[string]interface{}{"userId": getUserQuery.UserId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if user == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getUserQuery.UserId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
}
}
// 返回列表
func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (interface{}, error) {
if err := listUserQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
if count, users, err := userRepository.Find(tool_funs.SimpleStructToMap(listUserQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"users": users,
}, nil
}
}
// 移除
func (userService *UserService) RemoveUser(removeUserCommand *command.RemoveUserCommand) (interface{}, error) {
if err := removeUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
user, err := userRepository.FindOne(map[string]interface{}{"userId": removeUserCommand.UserId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if user == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeUserCommand.UserId)))
}
if user, err := userRepository.Remove(user); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
}
}
// 设置权限
func (userService *UserService) SetPermission(setPermissionCommand *command.SetPermissionCommand) (interface{}, error) {
if err := setPermissionCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 更新
func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUserCommand) (interface{}, error) {
if err := updateUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
user, err := userRepository.FindOne(map[string]interface{}{"userId": updateUserCommand.UserId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if user == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateUserCommand.UserId)))
}
if err := user.Update(tool_funs.SimpleStructToMap(updateUserCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if user, err := userRepository.Save(user); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
}
}
func NewUserService(options map[string]interface{}) *UserService {
newUserService := &UserService{}
return newUserService
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/user/service"
)
type UserController struct {
beego.BaseController
}
func (controller *UserController) CreateUser() {
userService := service.NewUserService(nil)
createUserCommand := &command.CreateUserCommand{}
controller.Unmarshal(createUserCommand)
data, err := userService.CreateUser(createUserCommand)
controller.Response(data, err)
}
func (controller *UserController) UpdateUser() {
userService := service.NewUserService(nil)
updateUserCommand := &command.UpdateUserCommand{}
controller.Unmarshal(updateUserCommand)
userId, _ := controller.GetInt64(":userId")
updateUserCommand.UserId = userId
data, err := userService.UpdateUser(updateUserCommand)
controller.Response(data, err)
}
func (controller *UserController) GetUser() {
userService := service.NewUserService(nil)
getUserQuery := &query.GetUserQuery{}
userId, _ := controller.GetInt64(":userId")
getUserQuery.UserId = userId
data, err := userService.GetUser(getUserQuery)
controller.Response(data, err)
}
func (controller *UserController) RemoveUser() {
userService := service.NewUserService(nil)
removeUserCommand := &command.RemoveUserCommand{}
controller.Unmarshal(removeUserCommand)
userId, _ := controller.GetInt64(":userId")
removeUserCommand.UserId = userId
data, err := userService.RemoveUser(removeUserCommand)
controller.Response(data, err)
}
func (controller *UserController) ListUser() {
userService := service.NewUserService(nil)
listUserQuery := &query.ListUserQuery{}
offset, _ := controller.GetInt("offset")
listUserQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listUserQuery.Limit = limit
data, err := userService.ListUser(listUserQuery)
controller.Response(data, err)
}
func (controller *UserController) ConvertUserStatus() {
userService := service.NewUserService(nil)
convertUserStatusCommand := &command.ConvertUserStatusCommand{}
controller.Unmarshal(convertUserStatusCommand)
data, err := userService.ConvertUserStatus(convertUserStatusCommand)
controller.Response(data, err)
}
func (controller *UserController) SetPermission() {
userService := service.NewUserService(nil)
setPermissionCommand := &command.SetPermissionCommand{}
controller.Unmarshal(setPermissionCommand)
data, err := userService.SetPermission(setPermissionCommand)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers"
)
func init() {
web.Router("/users/", &controllers.UserController{}, "Post:CreateUser")
web.Router("/users/:userId", &controllers.UserController{}, "Put:UpdateUser")
web.Router("/users/:userId", &controllers.UserController{}, "Get:GetUser")
web.Router("/users/:userId", &controllers.UserController{}, "Delete:RemoveUser")
web.Router("/users/", &controllers.UserController{}, "Get:ListUser")
web.Router("/users/convert-user-status", &controllers.UserController{}, "Post:ConvertUserStatus")
web.Router("/users/set-permission", &controllers.UserController{}, "Post:SetPermission")
}
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("用户状态转换(禁用、启用)", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("用户状态转换(禁用、启用)", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"userId": "int64",
"status": "int64",
}
httpExpect.POST("/users/convertUserStatus").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("创建", func() {
Describe("提交数据创建", func() {
Context("提交正确的新用户实体数据", func() {
It("返回用户实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"userType": "int",
"adminType": "int",
"status": "int64",
"isPrincipal": "boolean",
"uid": "int64",
"userAccount": "string",
"userAvatarUrl": "string",
"userName": "string",
"email": "string",
"gender": "int",
"entryTime": "datetime",
"extension": "string",
"workplace": "string",
"privateNumber": "string",
"jobNumber": "string",
"partnerAccount": "string",
"partnerName": "string",
"regionName": "string",
"cooperateTime": "datetime",
"salesmans": "array",
"partnerCategorys": "array",
"accessPartners": "array",
}
httpExpect.POST("/users/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("userId").ValueNotEqual("userId", BeZero())
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据userId参数返回用户实体", func() {
Context("传入有效的userId", func() {
It("返回用户实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/users/{userId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回列表", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回用户实体列表", func() {
Context("传入有效的参数", func() {
It("返回用户实体数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/users/").
WithQuery("offset", "int").
WithQuery("limit", "int").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("users").Value("users").Array()
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("移除", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除", func() {
Context("传入有效的userId", func() {
It("返回被移除用户实体的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/users/{userId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("设置权限", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("设置权限", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"userId": "int64",
"permissons": "array",
}
httpExpect.POST("/users/setPermission").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM users WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("更新", func() {
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, user_type, permissions, company_id, user_info, partner_info, status, admin_type, access_partners, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testUserType", "testPermissions", "testCompanyId", "testUserInfo", "testPartnerInfo", "testStatus", "testAdminType", "testAccessPartners", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新", func() {
Context("提交正确的用户实体数据", func() {
It("返回更新后的用户实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"userType": "int",
"adminType": "int",
"status": "int64",
"isPrincipal": "boolean",
"uid": "int64",
"userAccount": "string",
"userAvatarUrl": "string",
"userName": "string",
"email": "string",
"gender": "int",
"entryTime": "datetime",
"extension": "string",
"workplace": "string",
"privateNumber": "string",
"jobNumber": "string",
"partnerAccount": "string",
"partnerName": "string",
"regionName": "string",
"cooperateTime": "datetime",
"salesmans": "array",
"partnerCategorys": "array",
"accessPartners": "array",
}
httpExpect.PUT("/users/{userId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("userId").ValueEqual("userId", userId)
})
})
})
AfterEach(func() {
//_, err := pG.DB.Exec("DELETE FROM users WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego"
)
func TestUser(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port User Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = web.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...