package main import ( "git.sxidc.com/go-framework/baize" "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/operations" DEATH "github.com/vrecan/death" "syscall" "time" ) // 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":"10ec51a6485b4fadb9fc1b4f1360b8d0", "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=10ec51a6485b4fadb9fc1b4f1360b8d0" // curl -X DELETE "http://localhost:10100/test/v1/class/10ec51a6485b4fadb9fc1b4f1360b8d0/delete" type CreateClassJsonBody struct { Name string `json:"name" binding:"required" assign:"toField:Name"` StudentNum int `json:"studentNum" binding:"required" assign:"toField:StudentNum"` } 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"` StudentNum int `json:"studentNum" assign:"toField:StudentNum"` } type QueryClassesQueryParams struct { 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 Class struct { 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"` } 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'"` } const ( tableName = "test.classes" ) func main() { app := baize.NewApplication(application.Config{ ApiConfig: application.ApiConfig{ UrlPrefix: "test", Port: "10100", }, 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().DBOperations() 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: "student_num", Type: "integer", 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) } app.Api(). PrefixRouter(). RegisterVersionedRouter("v1") binder := app.Binder("v1") // 创建班级 binding.PostBind(binder, &binding.SimpleBindItem[string]{ Path: "/class/create", ResponseFunc: response.SendIDResponse[string], DTO: &CreateClassJsonBody{}, Objects: []domain.Object{&Class{}}, ServiceFunc: entity_crud.CommonEntityCreate(tableName, infrastructure.DBExecutorOperations, nil), }) // 删除班级 binding.DeleteBind(binder, &binding.SimpleBindItem[any]{ Path: "/class/:id/delete", ResponseFunc: response.SendMsgResponse, DTO: &DeleteClassPathParams{}, Objects: []domain.Object{&Class{}}, ServiceFunc: entity_crud.CommonEntityDelete(tableName, infrastructure.DBExecutorOperations, nil), }) // 修改班级 binding.PutBind(binder, &binding.SimpleBindItem[any]{ Path: "/class/update", ResponseFunc: response.SendMsgResponse, DTO: &UpdateClassJsonBody{}, Objects: []domain.Object{&Class{}}, ServiceFunc: entity_crud.CommonEntityUpdate(tableName, infrastructure.DBExecutorOperations, nil), }) // 查询班级 binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[ClassInfo]]{ Path: "/class/query", ResponseFunc: response.SendInfosResponse[ClassInfo], DTO: &QueryClassesQueryParams{}, Objects: []domain.Object{&Class{}}, ServiceFunc: entity_crud.CommonEntityQuery[ClassInfo](tableName, infrastructure.DBExecutorOperations, nil, nil), }) // 通过ID获取班级 binding.GetBind(binder, &binding.SimpleBindItem[ClassInfo]{ Path: "/class/get", ResponseFunc: response.SendInfoResponse[ClassInfo], DTO: &GetClassQueryParams{}, Objects: []domain.Object{&Class{}}, ServiceFunc: entity_crud.CommonEntityQueryByID[ClassInfo](tableName, infrastructure.DBExecutorOperations, 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() }