作者 yangfu

add org

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CreateOrgCommand struct {
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
// 组织名称
OrgName string `cname:"组织名称" json:"orgName" valid:"Required"`
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
}
func (createOrgCommand *CreateOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createOrgCommand *CreateOrgCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createOrgCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(createOrgCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type EnableOrgCommand struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `cname:"组织状态 1:启用 2:禁用 3.删除" json:"orgStatus" valid:"Required"`
}
func (enableOrgCommand *EnableOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (enableOrgCommand *EnableOrgCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(enableOrgCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(enableOrgCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type RemoveOrgCommand struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
}
func (removeOrgCommand *RemoveOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeOrgCommand *RemoveOrgCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeOrgCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(removeOrgCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UpdateOrgCommand struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode" valid:"Required"`
// 组织名称
OrgName string `cname:"组织名称" json:"orgName" valid:"Required"`
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg" valid:"Required"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string" valid:"Required"`
}
func (updateOrgCommand *UpdateOrgCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateOrgCommand *UpdateOrgCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateOrgCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateOrgCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetOrgQuery struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
}
func (getOrgQuery *GetOrgQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getOrgQuery *GetOrgQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getOrgQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getOrgQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetOrgSubDepartmentQuery struct {
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
// 获取所有子节点 (1:获取当前下级子节点 2:获取当前下级所有子节点)
ChildFlag int `cname:"获取所有子节点 (1:获取当前下级子节点 2:获取当前下级所有子节点)" json:"childFlag,omitempty"`
}
func (getOrgSubDepartmentQuery *GetOrgSubDepartmentQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getOrgSubDepartmentQuery *GetOrgSubDepartmentQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getOrgSubDepartmentQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getOrgSubDepartmentQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type ListOrgQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset,omitempty"`
// 查询限制
Limit int `cname:"查询限制" json:"limit,omitempty"`
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
// 组织编码
OrgCode string `cname:"组织编码" json:"orgCode,omitempty"`
// 部门名称
DepName string `cname:"部门名称" json:"depName,omitempty"`
// 父级ID
ParentId int64 `cname:"父级ID" json:"parentId,string,omitempty"`
// 是否是组织(是:1 不是:2)
IsOrg int `cname:"是否是组织(是:1 不是:2)" json:"isOrg,omitempty"`
}
func (listOrgQuery *ListOrgQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listOrgQuery *ListOrgQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listOrgQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listOrgQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(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/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
)
// 组织管理
type OrgService struct {
}
// 创建组织
func (orgService *OrgService) CreateOrg(createOrgCommand *command.CreateOrgCommand) (interface{}, error) {
if err := createOrgCommand.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()
}()
newOrg := &domain.Org{
CompanyId: createOrgCommand.CompanyId,
OrgCode: createOrgCommand.OrgCode,
OrgName: createOrgCommand.OrgName,
IsOrg: createOrgCommand.IsOrg,
ParentId: createOrgCommand.ParentId,
}
var orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
if org, err := orgRepository.Save(newOrg); 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 org, nil
}
}
// 设置组织启用状态
func (orgService *OrgService) EnableOrg(enableOrgCommand *command.EnableOrgCommand) (interface{}, error) {
if err := enableOrgCommand.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 (orgService *OrgService) GetOrg(getOrgQuery *query.GetOrgQuery) (interface{}, error) {
if err := getOrgQuery.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 orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
org, err := orgRepository.FindOne(map[string]interface{}{"orgId": getOrgQuery.OrgId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if org == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrgQuery.OrgId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return org, nil
}
}
// 获取组织的子部门(通用部门列表使用)
func (orgService *OrgService) GetOrgSubDepartment(getOrgSubDepartmentQuery *query.GetOrgSubDepartmentQuery) (interface{}, error) {
if err := getOrgSubDepartmentQuery.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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 返回组织列表
func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interface{}, error) {
if err := listOrgQuery.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 orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
if count, orgs, err := orgRepository.Find(tool_funs.SimpleStructToMap(listOrgQuery)); 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,
"orgs": orgs,
}, nil
}
}
// 移除组织
func (orgService *OrgService) RemoveOrg(removeOrgCommand *command.RemoveOrgCommand) (interface{}, error) {
if err := removeOrgCommand.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 orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
org, err := orgRepository.FindOne(map[string]interface{}{"orgId": removeOrgCommand.OrgId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if org == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeOrgCommand.OrgId)))
}
if org, err := orgRepository.Remove(org); 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 org, nil
}
}
// 更新组织
func (orgService *OrgService) UpdateOrg(updateOrgCommand *command.UpdateOrgCommand) (interface{}, error) {
if err := updateOrgCommand.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 orgRepository domain.OrgRepository
if value, err := factory.CreateOrgRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orgRepository = value
}
org, err := orgRepository.FindOne(map[string]interface{}{"orgId": updateOrgCommand.OrgId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if org == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrgCommand.OrgId)))
}
if err := org.Update(tool_funs.SimpleStructToMap(updateOrgCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if org, err := orgRepository.Save(org); 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 org, nil
}
}
func NewOrgService(options map[string]interface{}) *OrgService {
newOrgService := &OrgService{}
return newOrgService
}
... ...
... ... @@ -7,9 +7,9 @@ var POSTGRESQL_USER = "postgres"
var POSTGRESQL_PASSWORD = "123456"
var POSTGRESQL_HOST = "127.0.0.1"
var POSTGRESQL_PORT = "5432"
var DISABLE_CREATE_TABLE = false
var DISABLE_SQL_GENERATE_PRINT = false
var DISABLE_SQL_GENERATE_COMMENT = false
var DISABLE_CREATE_TABLE = true
var DISABLE_SQL_GENERATE_PRINT = true
var DISABLE_SQL_GENERATE_COMMENT = true
func init() {
if os.Getenv("POSTGRESQL_DB_NAME") != "" {
... ...
... ... @@ -67,7 +67,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("INSERT INTO orgs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
fmt.Sprintf("INSERT INTO org (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
org.OrgId,
org.CompanyId,
org.CreatedAt,
... ... @@ -98,7 +98,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("UPDATE orgs SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
fmt.Sprintf("UPDATE org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/org/service"
)
type OrgController struct {
beego.BaseController
}
func (controller *OrgController) CreateOrg() {
orgService := service.NewOrgService(nil)
createOrgCommand := &command.CreateOrgCommand{}
controller.Unmarshal(createOrgCommand)
data, err := orgService.CreateOrg(createOrgCommand)
controller.Response(data, err)
}
func (controller *OrgController) UpdateOrg() {
orgService := service.NewOrgService(nil)
updateOrgCommand := &command.UpdateOrgCommand{}
controller.Unmarshal(updateOrgCommand)
orgId, _ := controller.GetInt64(":orgId")
updateOrgCommand.OrgId = orgId
data, err := orgService.UpdateOrg(updateOrgCommand)
controller.Response(data, err)
}
func (controller *OrgController) GetOrg() {
orgService := service.NewOrgService(nil)
getOrgQuery := &query.GetOrgQuery{}
orgId, _ := controller.GetInt64(":orgId")
getOrgQuery.OrgId = orgId
data, err := orgService.GetOrg(getOrgQuery)
controller.Response(data, err)
}
func (controller *OrgController) RemoveOrg() {
orgService := service.NewOrgService(nil)
removeOrgCommand := &command.RemoveOrgCommand{}
controller.Unmarshal(removeOrgCommand)
orgId, _ := controller.GetInt64(":orgId")
removeOrgCommand.OrgId = orgId
data, err := orgService.RemoveOrg(removeOrgCommand)
controller.Response(data, err)
}
func (controller *OrgController) ListOrg() {
orgService := service.NewOrgService(nil)
listOrgQuery := &query.ListOrgQuery{}
data, err := orgService.ListOrg(listOrgQuery)
controller.Response(data, err)
}
func (controller *OrgController) GetOrgSubDepartment() {
orgService := service.NewOrgService(nil)
getOrgSubDepartmentQuery := &query.GetOrgSubDepartmentQuery{}
orgId, _ := controller.GetInt64(":orgId")
getOrgSubDepartmentQuery.OrgId = orgId
childFlag, _ := controller.GetInt("childFlag")
getOrgSubDepartmentQuery.ChildFlag = childFlag
data, err := orgService.GetOrgSubDepartment(getOrgSubDepartmentQuery)
controller.Response(data, err)
}
func (controller *OrgController) EnableOrg() {
orgService := service.NewOrgService(nil)
enableOrgCommand := &command.EnableOrgCommand{}
controller.Unmarshal(enableOrgCommand)
data, err := orgService.EnableOrg(enableOrgCommand)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers"
)
func init() {
web.Router("/org/", &controllers.OrgController{}, "Post:CreateOrg")
web.Router("/org/:orgId", &controllers.OrgController{}, "Put:UpdateOrg")
web.Router("/org/:orgId", &controllers.OrgController{}, "Get:GetOrg")
web.Router("/org/:orgId", &controllers.OrgController{}, "Delete:RemoveOrg")
web.Router("/org/search", &controllers.OrgController{}, "Post:ListOrg")
web.Router("/org/:orgId/sub-department", &controllers.OrgController{}, "Get:GetOrgSubDepartment")
web.Router("/org/enable", &controllers.OrgController{}, "Post:EnableOrg")
}
... ...
package org
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("创建组织", func() {
Describe("提交数据创建组织", func() {
Context("提交正确的新组织 organization数据", func() {
It("返回组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": "int64",
"orgCode": "string",
"orgName": "string",
"isOrg": "int",
"parentId": "int64",
}
httpExpect.POST("/org/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("orgId").ValueNotEqual("orgId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("设置组织启用状态", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("设置组织启用状态", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"orgId": "int64",
"orgStatus": "int",
}
httpExpect.POST("/org/enable").
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 org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("获取组织的子部门(通用部门列表使用)", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("获取组织的子部门(通用部门列表使用)", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/org/{orgId}/sub-department").
WithQuery("childFlag", "int").
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 org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("返回组织", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据orgId参数返回组织 organization", func() {
Context("传入有效的orgId", func() {
It("返回组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/org/{orgId}").
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 org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("返回组织列表", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回组织 organization列表", func() {
Context("传入有效的参数", func() {
It("返回组织 organization数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"offset": "int",
"limit": "int",
"companyId": "int64",
"orgCode": "string",
"depName": "string",
"parentId": "int64",
"isOrg": "int",
}
httpExpect.POST("/org/search").
WithJSON(body).
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("org").Value("org").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/application"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
)
func TestOrg(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port Org 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()
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("移除组织", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除组织", func() {
Context("传入有效的orgId", func() {
It("返回被移除组织 organization的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/org/{orgId}").
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 org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package org
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/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("更新组织", func() {
var orgId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orgId),
"INSERT INTO org (org_id, company_id, created_at, updated_at, deleted_at, org_code, org_name, ext, is_org, parent_id, org_status, parent_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING org_id",
"testOrgId", "testCompanyId", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testOrgCode", "testOrgName", "testExt", "testIsOrg", "testParentId", "testOrgStatus", "testParentPath")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新组织", func() {
Context("提交正确的组织 organization数据", func() {
It("返回更新后的组织 organization数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"orgCode": "string",
"orgName": "string",
"isOrg": "int",
"parentId": "int64",
}
httpExpect.PUT("/org/{orgId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("orgId").ValueEqual("orgId", orgId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...