|
@@ -1,23 +1,26 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
"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/domain"
|
|
|
"git.sxidc.com/go-framework/baize/infrastructure"
|
|
|
+ "git.sxidc.com/go-framework/baize/infrastructure/database"
|
|
|
+ "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
|
|
|
+ "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
DEATH "github.com/vrecan/death"
|
|
|
"syscall"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
type CreateClassJsonBody struct {
|
|
|
Name string `json:"name" binding:"required" assign:"toField:Name"`
|
|
@@ -42,17 +45,31 @@ type GetClassQueryParams struct {
|
|
|
ID string `form:"id" binding:"required" assign:"toField:ID"`
|
|
|
}
|
|
|
|
|
|
+type DomainIDField struct {
|
|
|
+ ID string `sqlmapping:"column:id"`
|
|
|
+}
|
|
|
+
|
|
|
type Class struct {
|
|
|
- ID string
|
|
|
- Name string
|
|
|
+ *DomainIDField
|
|
|
+ Name string `sqlmapping:"column:name"`
|
|
|
+ CreatedTime time.Time
|
|
|
+ LastUpdatedTime *time.Time
|
|
|
+}
|
|
|
+
|
|
|
+type InfoIDField struct {
|
|
|
+ ID string `json:"id" sqlresult:"column:id"`
|
|
|
}
|
|
|
|
|
|
type ClassInfo struct {
|
|
|
- ID string `json:"id"`
|
|
|
- Name string `json:"name"`
|
|
|
+ *InfoIDField
|
|
|
+ Name string `json:"name" sqlresult:"column:name"`
|
|
|
+ CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
|
|
|
+ LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
|
|
|
}
|
|
|
|
|
|
-var classMap = make(map[string]domain.Object)
|
|
|
+const (
|
|
|
+ tableName = "test.classes"
|
|
|
+)
|
|
|
|
|
|
func main() {
|
|
|
app := baize.NewApplication(application.Config{
|
|
@@ -60,10 +77,66 @@ func main() {
|
|
|
UrlPrefix: "test",
|
|
|
Port: "10000",
|
|
|
},
|
|
|
+ InfrastructureConfig: application.InfrastructureConfig{
|
|
|
+ Database: infrastructure.DatabaseConfig{
|
|
|
+ Operations: &operations.Config{
|
|
|
+ UserName: "test",
|
|
|
+ Password: "123456",
|
|
|
+ Address: "localhost",
|
|
|
+ Port: "30432",
|
|
|
+ Database: "test",
|
|
|
+ MaxConnections: 40,
|
|
|
+ MaxIdleConnections: 10,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ baize.DestroyApplication(app)
|
|
|
+ }()
|
|
|
+
|
|
|
+ dbOperations := app.Infrastructure().GetDBOperations()
|
|
|
+ err := dbOperations.AutoMigrate(operations.Table{
|
|
|
+ TableName: tableName,
|
|
|
+ Columns: []operations.TableColumn{
|
|
|
+ {
|
|
|
+ Name: "id",
|
|
|
+ Type: "varchar(32)",
|
|
|
+ Comment: "id",
|
|
|
+ PrimaryKey: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Name: "name",
|
|
|
+ Type: "varchar(128)",
|
|
|
+ Comment: "名称",
|
|
|
+ NotNull: true,
|
|
|
+ Index: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Name: "created_time",
|
|
|
+ Type: "timestamp with time zone",
|
|
|
+ Comment: "创建时间",
|
|
|
+ NotNull: true,
|
|
|
+ Index: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Name: "last_updated_time",
|
|
|
+ Type: "timestamp with time zone",
|
|
|
+ Comment: "更新时间",
|
|
|
+ NotNull: true,
|
|
|
+ Index: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
})
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
|
|
|
v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1")
|
|
|
|
|
|
+ binding.SetGlobalInfrastructure(app.Infrastructure())
|
|
|
+
|
|
|
|
|
|
binding.PostBind(v1Router, &binding.SimpleBindItem[string]{
|
|
|
Path: "/class/create",
|
|
@@ -73,7 +146,12 @@ func main() {
|
|
|
ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
|
|
|
e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
e.ID = strutils.SimpleUUID()
|
|
|
- classMap[e.ID] = e
|
|
|
+
|
|
|
+ err := database.InsertEntity(i.GetDBOperations(), tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
return e.ID, nil
|
|
|
},
|
|
|
})
|
|
@@ -85,9 +163,13 @@ func main() {
|
|
|
DTO: &DeleteClassPathParams{},
|
|
|
Objects: []domain.Object{&Class{}},
|
|
|
ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
- id := domain.Field[string](objects[0], "ID")
|
|
|
- delete(classMap, id)
|
|
|
- fmt.Println("Deleted Entity:" + id)
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+
|
|
|
+ err := database.DeleteEntity(i.GetDBOperations(), tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
return nil, nil
|
|
|
},
|
|
|
})
|
|
@@ -99,20 +181,34 @@ func main() {
|
|
|
DTO: &UpdateClassJsonBody{},
|
|
|
Objects: []domain.Object{&Class{}},
|
|
|
ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
- id := domain.Field[string](objects[0], "ID")
|
|
|
- newName := domain.Field[string](objects[0], "Name")
|
|
|
-
|
|
|
- existEntity, ok := classMap[id]
|
|
|
- if !ok {
|
|
|
- fmt.Println("Update Entity:" + id)
|
|
|
- fmt.Println("Not Find")
|
|
|
- return nil, nil
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+
|
|
|
+ result, err := database.QueryOne(i.GetDBOperations(), &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},
|
|
|
}
|
|
|
|
|
|
- domain.SetField(existEntity, "Name", newName)
|
|
|
+ if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
|
|
|
+ newEntity.Name = e.Name
|
|
|
+ }
|
|
|
|
|
|
- fmt.Println("Update Entity:" + id)
|
|
|
- fmt.Println("Name:" + newName)
|
|
|
+ err = database.UpdateEntity(i.GetDBOperations(), tableName, newEntity)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
|
|
|
return nil, nil
|
|
|
},
|
|
@@ -125,60 +221,39 @@ func main() {
|
|
|
DTO: &QueryClassesQueryParams{},
|
|
|
Objects: []domain.Object{&Class{}},
|
|
|
ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
|
|
|
- name := domain.Field[string](objects[0], "Name")
|
|
|
-
|
|
|
- classInfos := make([]ClassInfo, 0)
|
|
|
-
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
pageNo := binding.Field[int](dto, "PageNo")
|
|
|
pageSize := binding.Field[int](dto, "PageSize")
|
|
|
|
|
|
- startCount := 1
|
|
|
- if pageNo != 0 && pageSize != 0 {
|
|
|
- startCount = pageNo*pageSize + 1
|
|
|
+ conditions := sql.NewConditions()
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(e.Name) {
|
|
|
+ conditions.Equal("name", e.Name)
|
|
|
}
|
|
|
|
|
|
- needFindCount := len(classMap)
|
|
|
- if pageNo != 0 && pageSize != 0 {
|
|
|
- needFindCount = pageSize
|
|
|
+ results, totalCount, err := database.Query(i.GetDBOperations(), &sql.QueryExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: conditions,
|
|
|
+ PageNo: pageNo,
|
|
|
+ PageSize: pageSize,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return binding.InfosData[ClassInfo]{
|
|
|
+ Infos: make([]ClassInfo, 0),
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
- count := 1
|
|
|
- findCount := 0
|
|
|
-
|
|
|
- for _, existEntity := range classMap {
|
|
|
- existID := domain.Field[string](existEntity, "ID")
|
|
|
- existName := domain.Field[string](existEntity, "Name")
|
|
|
-
|
|
|
- if findCount >= needFindCount {
|
|
|
- break
|
|
|
- }
|
|
|
-
|
|
|
- if count >= startCount {
|
|
|
- find := false
|
|
|
- if strutils.IsStringNotEmpty(name) {
|
|
|
- if existName == name {
|
|
|
- find = true
|
|
|
- findCount++
|
|
|
- }
|
|
|
- } else {
|
|
|
- find = true
|
|
|
- findCount++
|
|
|
- }
|
|
|
-
|
|
|
- if find {
|
|
|
- classInfos = append(classInfos, ClassInfo{
|
|
|
- ID: existID,
|
|
|
- Name: existName,
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- count++
|
|
|
+ classInfos := make([]ClassInfo, 0)
|
|
|
+ err = sql.ParseSqlResult(results, &classInfos)
|
|
|
+ if err != nil {
|
|
|
+ return binding.InfosData[ClassInfo]{
|
|
|
+ Infos: make([]ClassInfo, 0),
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
return binding.InfosData[ClassInfo]{
|
|
|
Infos: classInfos,
|
|
|
- TotalCount: int64(len(classMap)),
|
|
|
+ TotalCount: totalCount,
|
|
|
PageNo: pageNo,
|
|
|
}, nil
|
|
|
},
|
|
@@ -191,19 +266,23 @@ func main() {
|
|
|
DTO: &GetClassQueryParams{},
|
|
|
Objects: []domain.Object{&Class{}},
|
|
|
ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
|
|
|
- id := domain.Field[string](objects[0], "ID")
|
|
|
-
|
|
|
- classInfo := new(ClassInfo)
|
|
|
- for _, existEntity := range classMap {
|
|
|
- if domain.Field[string](existEntity, "ID") == id {
|
|
|
- classInfo = &ClassInfo{
|
|
|
- ID: domain.Field[string](existEntity, "ID"),
|
|
|
- Name: domain.Field[string](existEntity, "Name"),
|
|
|
- }
|
|
|
- }
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+
|
|
|
+ result, err := database.QueryOne(i.GetDBOperations(), &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 classInfo, nil
|
|
|
+ return info, nil
|
|
|
},
|
|
|
})
|
|
|
|