| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- 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"
- )
- // 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"
- 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.ToConcrete[*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.ToConcrete[*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.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
- },
- })
- // 查询班级
- 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.ToConcrete[*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
- },
- })
- // 通过ID获取班级
- 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.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
- },
- })
- 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()
- }
|