ソースを参照

完成多对多Demo编写

yjp 1 年間 前
コミット
2e095ddaee

+ 8 - 8
convenient/binding/request/common.go

@@ -6,7 +6,7 @@ type WithID interface {
 }
 
 type IDJsonBody struct {
-	ID string `json:"id" binding:"required"`
+	ID string `json:"id" binding:"required" assign:"toField:ID"`
 }
 
 func (id *IDJsonBody) GetID() string {
@@ -14,7 +14,7 @@ func (id *IDJsonBody) GetID() string {
 }
 
 type IDPath struct {
-	ID string `uri:"id" binding:"required"`
+	ID string `uri:"id" binding:"required" assign:"toField:ID"`
 }
 
 func (id *IDPath) GetID() string {
@@ -22,7 +22,7 @@ func (id *IDPath) GetID() string {
 }
 
 type IDQuery struct {
-	ID string `form:"id" binding:"required"`
+	ID string `form:"id" binding:"required" assign:"toField:ID"`
 }
 
 func (id *IDQuery) GetID() string {
@@ -35,7 +35,7 @@ type TenantID interface {
 }
 
 type TenantIDJsonBody struct {
-	TenantID string `json:"tenant_id" binding:"required"`
+	TenantID string `json:"tenant_id" binding:"required" assign:"toField:TenantID"`
 }
 
 func (id *IDJsonBody) GetTenantID() string {
@@ -43,7 +43,7 @@ func (id *IDJsonBody) GetTenantID() string {
 }
 
 type TenantIDPath struct {
-	TenantID string `uri:"tenant_id" binding:"required"`
+	TenantID string `uri:"tenant_id" binding:"required" assign:"toField:TenantID"`
 }
 
 func (id *IDPath) GetTenantID() string {
@@ -51,7 +51,7 @@ func (id *IDPath) GetTenantID() string {
 }
 
 type TenantIDQuery struct {
-	TenantID string `form:"tenant_id" binding:"required"`
+	TenantID string `form:"tenant_id" binding:"required" assign:"toField:TenantID"`
 }
 
 func (id *IDQuery) GetTenantID() string {
@@ -65,8 +65,8 @@ type Query interface {
 }
 
 type BaseQuery struct {
-	PageNo   int `form:"pageNo"`
-	PageSize int `form:"pageSize"`
+	PageNo   int `form:"pageNo" assign:"-"`
+	PageSize int `form:"pageSize" assign:"-"`
 }
 
 func (q *BaseQuery) GetPageNo() int {

+ 1 - 1
convenient/relation/remote/service.go

@@ -1,4 +1,4 @@
-package many2many
+package remote
 
 import (
 	"git.sxidc.com/go-framework/baize/convenient/binding"

+ 1 - 1
convenient/relation/remote/simple.go

@@ -1,4 +1,4 @@
-package many2many
+package remote
 
 import (
 	"git.sxidc.com/go-framework/baize/convenient/binding"

+ 2 - 0
examples/examples/project/application/application.go

@@ -38,8 +38,10 @@ var applications = []Service{
 	&service.Configuration{},
 	&service.Class{},
 	&service.Student{},
+	&service.Identity{},
 	&service.Family{},
 	&service.StudentAndFamily{},
+	&service.StudentAndIdentity{},
 }
 
 func Start() error {

+ 82 - 0
examples/examples/project/application/domain/identity/entity.go

@@ -0,0 +1,82 @@
+package identity
+
+import (
+	"git.sxidc.com/go-framework/baize/framwork/domain/entity"
+	"git.sxidc.com/go-tools/utils/strutils"
+	"git.sxidc.com/service-supports/fserr"
+)
+
+const (
+	ColumnName = "name"
+)
+
+const (
+	fieldNameMaxLen = 128
+)
+
+type Entity struct {
+	entity.Base
+	Name       string   `sqlmapping:"column:name" sqlresult:"column:name"`
+	StudentIDs []string `sqlmapping:"-" sqlresult:"-"`
+	entity.TimeFields
+}
+
+func (e *Entity) DomainCNName() string {
+	return "身份"
+}
+
+func (e *Entity) DomainCamelName() string {
+	return "Identity"
+}
+
+func (e *Entity) ForCreate() error {
+	err := e.CheckFieldID(e.DomainCNName())
+	if err != nil {
+		return err
+	}
+
+	err = e.checkFieldName()
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (e *Entity) ForUpdate() error {
+	err := e.CheckFieldID(e.DomainCNName())
+	if err != nil {
+		return err
+	}
+
+	err = e.checkUpdateFields()
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (e *Entity) checkFieldName() error {
+	if strutils.IsStringEmpty(e.Name) {
+		return fserr.New(e.DomainCNName() + "名称为空")
+	}
+
+	if len(e.Name) > fieldNameMaxLen {
+		return fserr.New(e.DomainCNName() + "名称超出限定长度")
+	}
+
+	return nil
+}
+
+func (e *Entity) checkUpdateFields() error {
+	if strutils.AllBlank(e.Name) {
+		return fserr.New(e.DomainCNName() + "没有传递需要更新的字段")
+	}
+
+	if strutils.IsStringNotEmpty(e.Name) && len(e.Name) > fieldNameMaxLen {
+		return fserr.New(e.DomainCNName() + "名称超出限定长度")
+	}
+
+	return nil
+}

+ 11 - 0
examples/examples/project/application/domain/identity/info.go

@@ -0,0 +1,11 @@
+package identity
+
+import (
+	"git.sxidc.com/go-framework/baize/framwork/application"
+)
+
+type Info struct {
+	application.InfoIDField
+	Name string `json:"name" sqlresult:"column:name"`
+	application.InfoTimeFields
+}

+ 38 - 0
examples/examples/project/application/domain/identity/request_params.go

@@ -0,0 +1,38 @@
+package identity
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/binding/request"
+)
+
+type (
+	CreateJsonBody struct {
+		Name string `json:"name" binding:"required" assign:"toField:Name"`
+	}
+
+	DeletePathParams struct {
+		request.IDPath
+	}
+
+	UpdateJsonBody struct {
+		request.IDJsonBody
+		Name string `json:"name" assign:"toField:Name"`
+	}
+
+	QueryQueryParams struct {
+		request.BaseQuery
+		Name string `form:"name" assign:"toField:Name"`
+	}
+
+	GetByIDQueryParams struct {
+		request.IDQuery
+	}
+
+	UpdateStudentsOfIdentityJsonBody struct {
+		request.IDJsonBody
+		StudentIDs string `json:"studentIds" assign:"toField:StudentIDs"`
+	}
+
+	QueryStudentsOfIdentityQueryParams struct {
+		request.QueryWithID
+	}
+)

+ 6 - 5
examples/examples/project/application/domain/student/entity.go

@@ -16,8 +16,9 @@ const (
 
 type Entity struct {
 	entity.Base
-	Name     string `sqlmapping:"column:name" sqlresult:"column:name"`
-	FamilyID string `sqlmapping:"column:family_id" sqlresult:"column:family_id"`
+	Name        string   `sqlmapping:"column:name" sqlresult:"column:name"`
+	FamilyID    string   `sqlmapping:"column:family_id" sqlresult:"column:family_id"`
+	IdentityIDs []string `sqlmapping:"-" sqlresult:"-"`
 	entity.TimeFields
 }
 
@@ -59,11 +60,11 @@ func (e *Entity) ForUpdate() error {
 
 func (e *Entity) checkFieldName() error {
 	if strutils.IsStringEmpty(e.Name) {
-		return fserr.New(e.DomainCNName() + "名为空")
+		return fserr.New(e.DomainCNName() + "名为空")
 	}
 
 	if len(e.Name) > fieldNameMaxLen {
-		return fserr.New(e.DomainCNName() + "名超出限定长度")
+		return fserr.New(e.DomainCNName() + "名超出限定长度")
 	}
 
 	return nil
@@ -75,7 +76,7 @@ func (e *Entity) checkUpdateFields() error {
 	}
 
 	if strutils.IsStringNotEmpty(e.Name) && len(e.Name) > fieldNameMaxLen {
-		return fserr.New(e.DomainCNName() + "名超出限定长度")
+		return fserr.New(e.DomainCNName() + "名超出限定长度")
 	}
 
 	return nil

+ 9 - 0
examples/examples/project/application/domain/student/request_params.go

@@ -39,4 +39,13 @@ type (
 	QueryStudentWithFamilyQueryParams struct {
 		request.IDQuery
 	}
+
+	UpdateIdentitiesOfStudentJsonBody struct {
+		request.IDJsonBody
+		IdentityIDs string `json:"identityIds" assign:"toField:IdentityIDs"`
+	}
+
+	QueryIdentitiesOfStudentQueryParams struct {
+		request.QueryWithID
+	}
 )

+ 32 - 0
examples/examples/project/application/service/identity.go

@@ -0,0 +1,32 @@
+package service
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/entity"
+	"git.sxidc.com/go-framework/baize/examples/examples/project/application/domain/identity"
+	"git.sxidc.com/go-framework/baize/framwork/application"
+)
+
+type Identity struct{}
+
+func (app *Identity) Init(appInstance *application.App) error {
+	app.v1(appInstance)
+	return nil
+}
+
+func (app *Identity) Destroy() error {
+	return nil
+}
+
+func (app *Identity) v1(appInstance *application.App) {
+	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
+
+	entity.BindSimple[*identity.Info](v1Binder, &entity.Simple[*identity.Info]{
+		Entity:             &identity.Entity{},
+		Schema:             dbSchema,
+		CreateJsonBody:     &identity.CreateJsonBody{},
+		DeleteQueryParams:  &identity.DeletePathParams{},
+		UpdateJsonBody:     &identity.UpdateJsonBody{},
+		QueryQueryParams:   &identity.QueryQueryParams{},
+		GetByIDQueryParams: &identity.GetByIDQueryParams{},
+	})
+}

+ 33 - 0
examples/examples/project/application/service/student_and_identity.go

@@ -0,0 +1,33 @@
+package service
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/relation/many2many"
+	"git.sxidc.com/go-framework/baize/examples/examples/project/application/domain/identity"
+	"git.sxidc.com/go-framework/baize/examples/examples/project/application/domain/student"
+	"git.sxidc.com/go-framework/baize/framwork/application"
+)
+
+type StudentAndIdentity struct{}
+
+func (app *StudentAndIdentity) Init(appInstance *application.App) error {
+	app.v1(appInstance)
+	return nil
+}
+
+func (app *StudentAndIdentity) Destroy() error {
+	return nil
+}
+
+func (app *StudentAndIdentity) v1(appInstance *application.App) {
+	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
+
+	many2many.BindSimple(v1Binder, &many2many.Simple[student.Info, *identity.Info]{
+		Left:                  &student.Entity{},
+		Right:                 &identity.Entity{},
+		Schema:                dbSchema,
+		LeftUpdateJsonBody:    &student.UpdateIdentitiesOfStudentJsonBody{},
+		LeftQueryQueryParams:  &student.QueryIdentitiesOfStudentQueryParams{},
+		RightUpdateJsonBody:   &identity.UpdateStudentsOfIdentityJsonBody{},
+		RightQueryQueryParams: &identity.QueryStudentsOfIdentityQueryParams{},
+	})
+}

+ 46 - 0
examples/examples/project/deployment/resources/resources.yaml

@@ -125,6 +125,34 @@ spec:
 
 ---
 
+kind: DataContainer
+spec:
+  namespace: baize
+  data_source: baize
+  name: test.identities
+  spec:
+    table_name: test.identities
+    columns:
+      - name: id
+        type: varchar(32)
+        comment: id
+        primary_key: true
+      - name: name
+        type: varchar(128)
+        comment: 名称
+        not_null: true
+        index: true
+      - name: created_time
+        type: "timestamp with time zone"
+        comment: 创建时间
+        not_null: true
+      - name: last_updated_time
+        type: "timestamp with time zone"
+        comment: 最近更新时间
+        not_null: true
+
+---
+
 kind: DataContainer
 spec:
   namespace: baize
@@ -149,3 +177,21 @@ spec:
         not_null: true
         index: true
 
+---
+
+kind: DataContainer
+spec:
+  namespace: baize
+  data_source: baize
+  name: test.student_and_identity
+  spec:
+    table_name: test.student_and_identity
+    columns:
+      - name: student_id
+        type: varchar(32)
+        comment: id
+        primary_key: true
+      - name: identity_id
+        type: varchar(32)
+        comment: id
+        primary_key: true