瀏覽代碼

完成binding ds demo

yjp 1 年之前
父節點
當前提交
ab584a3294
共有 3 個文件被更改,包括 39 次插入132 次删除
  1. 5 1
      examples/binding/main.go
  2. 28 131
      examples/binding_ds/main.go
  3. 6 0
      examples/binding_ds/resources.yaml

+ 5 - 1
examples/binding/main.go

@@ -54,6 +54,10 @@ type Class struct {
 	LastUpdatedTime *time.Time
 }
 
+func (class *Class) DomainCNName() string {
+	return "班级"
+}
+
 type InfoIDField struct {
 	ID string `json:"id" sqlresult:"column:id"`
 }
@@ -186,7 +190,7 @@ func main() {
 		Path:         "/class/get",
 		ResponseFunc: response.SendInfoResponse[ClassInfo],
 		DTO:          &GetClassQueryParams{},
-		Objects:      []domain.Object{Class{}},
+		Objects:      []domain.Object{&Class{}},
 		ServiceFunc:  entity_crud.CommonEntityQueryByID[ClassInfo](tableName, infrastructure.DBExecutorOperations, nil),
 	})
 

+ 28 - 131
examples/binding_ds/main.go

@@ -2,30 +2,28 @@ package main
 
 import (
 	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/api"
 	"git.sxidc.com/go-framework/baize/application"
 	"git.sxidc.com/go-framework/baize/binding"
 	"git.sxidc.com/go-framework/baize/binding/request"
 	"git.sxidc.com/go-framework/baize/binding/response"
+	"git.sxidc.com/go-framework/baize/binding/service/entity_crud"
 	"git.sxidc.com/go-framework/baize/domain"
 	"git.sxidc.com/go-framework/baize/infrastructure"
-	"git.sxidc.com/go-framework/baize/infrastructure/database"
 	"git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
-	"git.sxidc.com/go-framework/baize/infrastructure/database/sql"
-	"git.sxidc.com/go-tools/utils/strutils"
 	DEATH "github.com/vrecan/death"
 	"syscall"
 	"time"
 )
 
-// curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:10100/test/v1/class/create"
-// curl -X PUT -H "Content-Type: application/json" -d '{"id":"975750d39d674a57af2e49c670033ee8", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
-// curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=0&pageSize=1"
-// curl -X GET "http://localhost:10100/test/v1/class/get?id=975750d39d674a57af2e49c670033ee8"
-// curl -X DELETE "http://localhost:10100/test/v1/class/975750d39d674a57af2e49c670033ee8/delete"
+// curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
+// curl -X PUT -H "Content-Type: application/json" -d '{"id":"76a386a4ff66448d9456843df848129c", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
+// curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
+// curl -X GET "http://localhost:10100/test/v1/class/get?id=76a386a4ff66448d9456843df848129c"
+// curl -X DELETE "http://localhost:10100/test/v1/class/76a386a4ff66448d9456843df848129c/delete"
 
 type CreateClassJsonBody struct {
-	Name string `json:"name" binding:"required" assign:"toField:Name"`
+	Name       string `json:"name" binding:"required" assign:"toField:Name"`
+	StudentNum int    `json:"studentNum" binding:"required" assign:"toField:StudentNum"`
 }
 
 type DeleteClassPathParams struct {
@@ -33,31 +31,33 @@ type DeleteClassPathParams struct {
 }
 
 type UpdateClassJsonBody struct {
-	ID   string `json:"id" binding:"required" assign:"toField:ID"`
-	Name string `json:"name" assign:"toField:Name"`
+	ID         string `json:"id" binding:"required" assign:"toField:ID"`
+	Name       string `json:"name" assign:"toField:Name"`
+	StudentNum int    `json:"studentNum" assign:"toField:StudentNum"`
 }
 
 type QueryClassesQueryParams struct {
-	Name     string `form:"name" assign:"toField:Name"`
-	PageNo   int    `form:"pageNo" assign:"-"`
-	PageSize int    `form:"pageSize" assign:"-"`
+	Name       string `form:"name" assign:"toField:Name"`
+	StudentNum int    `form:"studentNum" assign:"toField:StudentNum"`
+	request.BaseQuery
 }
 
 type GetClassQueryParams struct {
 	ID string `form:"id" binding:"required" assign:"toField:ID"`
 }
 
-type DomainIDField struct {
-	ID string `sqlmapping:"column:id"`
-}
-
 type Class struct {
-	*DomainIDField
+	domain.BaseEntity
 	Name            string `sqlmapping:"column:name"`
+	StudentNum      int    `sqlmapping:"column:student_num"`
 	CreatedTime     time.Time
 	LastUpdatedTime *time.Time
 }
 
+func (class *Class) DomainCNName() string {
+	return "班级"
+}
+
 type InfoIDField struct {
 	ID string `json:"id" sqlresult:"column:id"`
 }
@@ -65,6 +65,7 @@ type InfoIDField struct {
 type ClassInfo struct {
 	*InfoIDField
 	Name            string `json:"name" sqlresult:"column:name"`
+	StudentNum      int    `json:"studentNum" sqlresult:"column:student_num"`
 	CreatedTime     string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
 	LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
 }
@@ -109,17 +110,7 @@ func main() {
 		ResponseFunc: response.SendIDResponse[string],
 		DTO:          &CreateClassJsonBody{},
 		Objects:      []domain.Object{&Class{}},
-		ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
-			e := domain.ToConcrete[*Class](objects[0])
-			e.ID = strutils.SimpleUUID()
-
-			err := database.InsertEntity(i.DataService(), tableName, e)
-			if err != nil {
-				return "", err
-			}
-
-			return e.ID, nil
-		},
+		ServiceFunc:  entity_crud.CommonEntityCreate(tableName, infrastructure.DBExecutorDataService, nil),
 	})
 
 	// 删除班级
@@ -128,16 +119,7 @@ func main() {
 		ResponseFunc: response.SendMsgResponse,
 		DTO:          &DeleteClassPathParams{},
 		Objects:      []domain.Object{&Class{}},
-		ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
-			e := domain.ToConcrete[*Class](objects[0])
-
-			err := database.DeleteEntity(i.DataService(), tableName, e)
-			if err != nil {
-				return "", err
-			}
-
-			return nil, nil
-		},
+		ServiceFunc:  entity_crud.CommonEntityDelete(tableName, infrastructure.DBExecutorDataService, nil),
 	})
 
 	// 修改班级
@@ -146,38 +128,7 @@ func main() {
 		ResponseFunc: response.SendMsgResponse,
 		DTO:          &UpdateClassJsonBody{},
 		Objects:      []domain.Object{&Class{}},
-		ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
-			e := domain.ToConcrete[*Class](objects[0])
-
-			result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
-				TableName:  tableName,
-				Conditions: sql.NewConditions().Equal("id", e.ID),
-			})
-			if err != nil {
-				return nil, err
-			}
-
-			existClass := new(Class)
-			err = sql.ParseSqlResult(result, existClass)
-			if err != nil {
-				return nil, err
-			}
-
-			newEntity := &Class{
-				DomainIDField: &DomainIDField{ID: existClass.ID},
-			}
-
-			if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
-				newEntity.Name = e.Name
-			}
-
-			err = database.UpdateEntity(i.DataService(), tableName, newEntity)
-			if err != nil {
-				return "", err
-			}
-
-			return nil, nil
-		},
+		ServiceFunc:  entity_crud.CommonEntityUpdate(tableName, infrastructure.DBExecutorDataService, nil),
 	})
 
 	// 查询班级
@@ -186,70 +137,16 @@ func main() {
 		ResponseFunc: response.SendInfosResponse[ClassInfo],
 		DTO:          &QueryClassesQueryParams{},
 		Objects:      []domain.Object{&Class{}},
-		ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[ClassInfo], error) {
-			e := domain.ToConcrete[*Class](objects[0])
-			pageNo := dto.Field[int](dto, "PageNo")
-			pageSize := dto.Field[int](dto, "PageSize")
-
-			conditions := sql.NewConditions()
-
-			if strutils.IsStringNotEmpty(e.Name) {
-				conditions.Equal("name", e.Name)
-			}
-
-			results, totalCount, err := database.Query(i.DataService(), &sql.QueryExecuteParams{
-				TableName:  tableName,
-				Conditions: conditions,
-				PageNo:     pageNo,
-				PageSize:   pageSize,
-			})
-			if err != nil {
-				return response.InfosData[ClassInfo]{
-					Infos: make([]ClassInfo, 0),
-				}, nil
-			}
-
-			classInfos := make([]ClassInfo, 0)
-			err = sql.ParseSqlResult(results, &classInfos)
-			if err != nil {
-				return response.InfosData[ClassInfo]{
-					Infos: make([]ClassInfo, 0),
-				}, nil
-			}
-
-			return response.InfosData[ClassInfo]{
-				Infos:      classInfos,
-				TotalCount: totalCount,
-				PageNo:     pageNo,
-			}, nil
-		},
+		ServiceFunc:  entity_crud.CommonEntityQuery[ClassInfo](tableName, infrastructure.DBExecutorDataService, nil, nil),
 	})
 
 	// 通过ID获取班级
-	binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
+	binding.GetBind(binder, &binding.SimpleBindItem[ClassInfo]{
 		Path:         "/class/get",
-		ResponseFunc: response.SendInfoResponse[*ClassInfo],
+		ResponseFunc: response.SendInfoResponse[ClassInfo],
 		DTO:          &GetClassQueryParams{},
 		Objects:      []domain.Object{&Class{}},
-		ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
-			e := domain.ToConcrete[*Class](objects[0])
-
-			result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
-				TableName:  tableName,
-				Conditions: sql.NewConditions().Equal("id", e.ID),
-			})
-			if err != nil {
-				return nil, err
-			}
-
-			info := new(ClassInfo)
-			err = sql.ParseSqlResult(result, info)
-			if err != nil {
-				return nil, err
-			}
-
-			return info, nil
-		},
+		ServiceFunc:  entity_crud.CommonEntityQueryByID[ClassInfo](tableName, infrastructure.DBExecutorDataService, nil),
 	})
 
 	go func() {

+ 6 - 0
examples/binding_ds/resources.yaml

@@ -37,6 +37,12 @@ spec:
         type: varchar(128)
         comment: 班名
         not_null: true
+        index: true
+      - name: student_num
+        type: integer
+        comment: 学生数量
+        not_null: true
+        index: true
       - name: created_time
         type: "timestamp with time zone"
         comment: 创建时间