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-tools/utils/strutils" DEATH "github.com/vrecan/death" "syscall" ) // curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:10000/test/v1/class/create" // curl -X PUT -H "Content-Type: application/json" -d '{"id":"47748950c1c74e8c9cd9dc913996fc8b", "name":"test-new"}' "http://localhost:10000/test/v1/class/update" // curl -X GET "http://localhost:10000/test/v1/class/query?name=test-new&pageNo=0&pageSize=1" // curl -X GET "http://localhost:10000/test/v1/class/get?id=47748950c1c74e8c9cd9dc913996fc8b" // curl -X DELETE "http://localhost:10000/test/v1/class/47748950c1c74e8c9cd9dc913996fc8b/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 Class struct { ID string Name string } type ClassInfo struct { ID string `json:"id"` Name string `json:"name"` } var classMap = make(map[string]domain.Object) func main() { app := baize.NewApplication(application.Config{ ApiConfig: application.ApiConfig{ UrlPrefix: "test", Port: "10000", }, }) v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1") // 创建班级 binding.PostBind(v1Router, &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) (string, error) { e := domain.ToConcreteObject[*Class](objects[0]) e.ID = strutils.SimpleUUID() classMap[e.ID] = e return e.ID, nil }, }) // 删除班级 binding.DeleteBind(v1Router, &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) (any, error) { id := domain.Field[string](objects[0], "ID") delete(classMap, id) fmt.Println("Deleted Entity:" + id) return nil, nil }, }) // 修改班级 binding.PutBind(v1Router, &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) (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 } domain.SetField(existEntity, "Name", newName) fmt.Println("Update Entity:" + id) fmt.Println("Name:" + newName) return nil, nil }, }) // 查询班级 binding.GetBind(v1Router, &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) (binding.InfosData[ClassInfo], error) { name := domain.Field[string](objects[0], "Name") classInfos := make([]ClassInfo, 0) pageNo := binding.Field[int](dto, "PageNo") pageSize := binding.Field[int](dto, "PageSize") startCount := 1 if pageNo != 0 && pageSize != 0 { startCount = pageNo*pageSize + 1 } needFindCount := len(classMap) if pageNo != 0 && pageSize != 0 { needFindCount = pageSize } 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++ } return binding.InfosData[ClassInfo]{ Infos: classInfos, TotalCount: int64(len(classMap)), PageNo: pageNo, }, nil }, }) // 通过ID获取班级 binding.GetBind(v1Router, &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) (*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"), } } } return classInfo, 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() }