|
@@ -0,0 +1,269 @@
|
|
|
+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/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"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type CreateClassJsonBody struct {
|
|
|
+ Name string `json:"name" binding:"required" assign:"toField:Name"`
|
|
|
+}
|
|
|
+
|
|
|
+type DeleteClassPathParams struct {
|
|
|
+ ID string `uri:"id" binding:"required" assign:"toField:ID"`
|
|
|
+}
|
|
|
+
|
|
|
+type UpdateClassJsonBody struct {
|
|
|
+ ID string `json:"id" binding:"required" assign:"toField:ID"`
|
|
|
+ Name string `json:"name" assign:"toField:Name"`
|
|
|
+}
|
|
|
+
|
|
|
+type QueryClassesQueryParams struct {
|
|
|
+ Name string `form:"name" assign:"toField:Name"`
|
|
|
+ PageNo int `form:"pageNo" assign:"-"`
|
|
|
+ PageSize int `form:"pageSize" assign:"-"`
|
|
|
+}
|
|
|
+
|
|
|
+type GetClassQueryParams struct {
|
|
|
+ ID string `form:"id" binding:"required" assign:"toField:ID"`
|
|
|
+}
|
|
|
+
|
|
|
+type DomainIDField struct {
|
|
|
+ ID string `sqlmapping:"column:id"`
|
|
|
+}
|
|
|
+
|
|
|
+type Class struct {
|
|
|
+ *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 {
|
|
|
+ *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'"`
|
|
|
+}
|
|
|
+
|
|
|
+const (
|
|
|
+ tableName = "test.classes"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ app := baize.NewApplication(application.Config{
|
|
|
+ ApiConfig: application.ApiConfig{
|
|
|
+ UrlPrefix: "test",
|
|
|
+ Port: "10100",
|
|
|
+ },
|
|
|
+ InfrastructureConfig: application.InfrastructureConfig{
|
|
|
+ Database: infrastructure.DatabaseConfig{
|
|
|
+ DataService: &data_service.Config{
|
|
|
+ Token: "8qe+uPgpQ2JWxu3lSyOx5NjX+INp5WsnoD1ZWb7PBm4=",
|
|
|
+ BaseUrl: "http://localhost:10000",
|
|
|
+ GrpcAddress: "localhost:10001",
|
|
|
+ Namespace: "baize",
|
|
|
+ DataSource: "baize-binding",
|
|
|
+ TimeoutSec: 30,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ baize.DestroyApplication(app)
|
|
|
+ }()
|
|
|
+
|
|
|
+ app.Api().
|
|
|
+ PrefixRouter().
|
|
|
+ RegisterVersionedRouter("v1")
|
|
|
+
|
|
|
+ binder := app.Binder("v1")
|
|
|
+
|
|
|
+
|
|
|
+ binding.PostBind(binder, &binding.SimpleBindItem[string]{
|
|
|
+ Path: "/class/create",
|
|
|
+ ResponseFunc: binding.SendIDResponse[string],
|
|
|
+ DTO: &CreateClassJsonBody{},
|
|
|
+ Objects: []domain.Object{&Class{}},
|
|
|
+ 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()
|
|
|
+
|
|
|
+ err := database.InsertEntity(i.DataService(), tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ return e.ID, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
|
|
|
+ Path: "/class/:id/delete",
|
|
|
+ ResponseFunc: binding.SendMsgResponse,
|
|
|
+ DTO: &DeleteClassPathParams{},
|
|
|
+ Objects: []domain.Object{&Class{}},
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+
|
|
|
+ err := database.DeleteEntity(i.DataService(), tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ binding.PutBind(binder, &binding.SimpleBindItem[any]{
|
|
|
+ Path: "/class/update",
|
|
|
+ ResponseFunc: binding.SendMsgResponse,
|
|
|
+ DTO: &UpdateClassJsonBody{},
|
|
|
+ Objects: []domain.Object{&Class{}},
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
+ e := domain.ToConcreteObject[*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
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ binding.GetBind(binder, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
|
|
|
+ Path: "/class/query",
|
|
|
+ ResponseFunc: binding.SendInfosResponse[ClassInfo],
|
|
|
+ DTO: &QueryClassesQueryParams{},
|
|
|
+ Objects: []domain.Object{&Class{}},
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
|
|
|
+ e := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ pageNo := binding.Field[int](dto, "PageNo")
|
|
|
+ pageSize := binding.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 binding.InfosData[ClassInfo]{
|
|
|
+ Infos: make([]ClassInfo, 0),
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ 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: totalCount,
|
|
|
+ PageNo: pageNo,
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
|
|
|
+ Path: "/class/get",
|
|
|
+ ResponseFunc: binding.SendInfoResponse[*ClassInfo],
|
|
|
+ DTO: &GetClassQueryParams{},
|
|
|
+ Objects: []domain.Object{&Class{}},
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
|
|
|
+ e := domain.ToConcreteObject[*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
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ err := app.Start()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ err := app.Finish()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
|
|
|
+ _ = death.WaitForDeath()
|
|
|
+}
|