| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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":"1fb7faa332af42fd9a41856320c0d4af", "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=1fb7faa332af42fd9a41856320c0d4af"
- // curl -X DELETE "http://localhost:10000/test/v1/class/1fb7faa332af42fd9a41856320c0d4af/delete"
- type CreateClassJsonBody struct {
- Name string `json:"name" binding:"required"`
- }
- type DeleteClassPathParams struct {
- ID string `uri:"id" binding:"required"`
- }
- type UpdateClassJsonBody struct {
- ID string `json:"id" binding:"required"`
- Name string `json:"name"`
- }
- type QueryClassesQueryParams struct {
- Name string `form:"id"`
- PageNo int `form:"pageNo"`
- PageSize int `form:"pageSize"`
- }
- type GetClassQueryParams struct {
- ID string `form:"id" binding:"required"`
- }
- 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{},
- FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
- return []domain.Object{
- &Class{
- Name: binding.Field[string](dto, "Name"),
- },
- }, nil
- },
- ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (string, error) {
- id := strutils.SimpleUUID()
- domain.SetField(objects[0], "ID", id)
- classMap[id] = objects[0]
- return id, nil
- },
- })
- // 删除班级
- binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
- Path: "/class/:id/delete",
- ResponseFunc: binding.SendMsgResponse,
- DTO: &DeleteClassPathParams{},
- FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
- return []domain.Object{
- &Class{
- ID: binding.Field[string](dto, "ID"),
- },
- }, nil
- },
- 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{},
- FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
- return []domain.Object{
- &Class{
- ID: binding.Field[string](dto, "ID"),
- Name: binding.Field[string](dto, "Name"),
- },
- }, nil
- },
- 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{},
- FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
- return []domain.Object{
- &Class{
- Name: binding.Field[string](dto, "Name"),
- },
- }, nil
- },
- 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{},
- FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
- return []domain.Object{
- &Class{
- ID: binding.Field[string](dto, "ID"),
- },
- }, nil
- },
- 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()
- }
|