main.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package main
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize"
  5. "git.sxidc.com/go-framework/baize/api"
  6. "git.sxidc.com/go-framework/baize/application"
  7. "git.sxidc.com/go-framework/baize/binding"
  8. "git.sxidc.com/go-framework/baize/domain"
  9. "git.sxidc.com/go-framework/baize/infrastructure"
  10. "git.sxidc.com/go-tools/utils/strutils"
  11. DEATH "github.com/vrecan/death"
  12. "syscall"
  13. )
  14. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:10000/test/v1/class/create"
  15. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"47748950c1c74e8c9cd9dc913996fc8b", "name":"test-new"}' "http://localhost:10000/test/v1/class/update"
  16. // curl -X GET "http://localhost:10000/test/v1/class/query?name=test-new&pageNo=0&pageSize=1"
  17. // curl -X GET "http://localhost:10000/test/v1/class/get?id=47748950c1c74e8c9cd9dc913996fc8b"
  18. // curl -X DELETE "http://localhost:10000/test/v1/class/47748950c1c74e8c9cd9dc913996fc8b/delete"
  19. type CreateClassJsonBody struct {
  20. Name string `json:"name" binding:"required" assign:"toField:Name"`
  21. }
  22. type DeleteClassPathParams struct {
  23. ID string `uri:"id" binding:"required" assign:"toField:ID"`
  24. }
  25. type UpdateClassJsonBody struct {
  26. ID string `json:"id" binding:"required" assign:"toField:ID"`
  27. Name string `json:"name" assign:"toField:Name"`
  28. }
  29. type QueryClassesQueryParams struct {
  30. Name string `form:"name" assign:"toField:Name"`
  31. PageNo int `form:"pageNo" assign:"-"`
  32. PageSize int `form:"pageSize" assign:"-"`
  33. }
  34. type GetClassQueryParams struct {
  35. ID string `form:"id" binding:"required" assign:"toField:ID"`
  36. }
  37. type Class struct {
  38. ID string
  39. Name string
  40. }
  41. type ClassInfo struct {
  42. ID string `json:"id"`
  43. Name string `json:"name"`
  44. }
  45. var classMap = make(map[string]domain.Object)
  46. func main() {
  47. app := baize.NewApplication(application.Config{
  48. ApiConfig: application.ApiConfig{
  49. UrlPrefix: "test",
  50. Port: "10000",
  51. },
  52. })
  53. v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1")
  54. // 创建班级
  55. binding.PostBind(v1Router, &binding.SimpleBindItem[string]{
  56. Path: "/class/create",
  57. ResponseFunc: binding.SendIDResponse[string],
  58. DTO: &CreateClassJsonBody{},
  59. Objects: []domain.Object{&Class{}},
  60. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  61. e := domain.ToConcreteObject[*Class](objects[0])
  62. e.ID = strutils.SimpleUUID()
  63. classMap[e.ID] = e
  64. return e.ID, nil
  65. },
  66. })
  67. // 删除班级
  68. binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
  69. Path: "/class/:id/delete",
  70. ResponseFunc: binding.SendMsgResponse,
  71. DTO: &DeleteClassPathParams{},
  72. Objects: []domain.Object{&Class{}},
  73. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  74. id := domain.Field[string](objects[0], "ID")
  75. delete(classMap, id)
  76. fmt.Println("Deleted Entity:" + id)
  77. return nil, nil
  78. },
  79. })
  80. // 修改班级
  81. binding.PutBind(v1Router, &binding.SimpleBindItem[any]{
  82. Path: "/class/update",
  83. ResponseFunc: binding.SendMsgResponse,
  84. DTO: &UpdateClassJsonBody{},
  85. Objects: []domain.Object{&Class{}},
  86. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  87. id := domain.Field[string](objects[0], "ID")
  88. newName := domain.Field[string](objects[0], "Name")
  89. existEntity, ok := classMap[id]
  90. if !ok {
  91. fmt.Println("Update Entity:" + id)
  92. fmt.Println("Not Find")
  93. return nil, nil
  94. }
  95. domain.SetField(existEntity, "Name", newName)
  96. fmt.Println("Update Entity:" + id)
  97. fmt.Println("Name:" + newName)
  98. return nil, nil
  99. },
  100. })
  101. // 查询班级
  102. binding.GetBind(v1Router, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
  103. Path: "/class/query",
  104. ResponseFunc: binding.SendInfosResponse[ClassInfo],
  105. DTO: &QueryClassesQueryParams{},
  106. Objects: []domain.Object{&Class{}},
  107. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
  108. name := domain.Field[string](objects[0], "Name")
  109. classInfos := make([]ClassInfo, 0)
  110. pageNo := binding.Field[int](dto, "PageNo")
  111. pageSize := binding.Field[int](dto, "PageSize")
  112. startCount := 1
  113. if pageNo != 0 && pageSize != 0 {
  114. startCount = pageNo*pageSize + 1
  115. }
  116. needFindCount := len(classMap)
  117. if pageNo != 0 && pageSize != 0 {
  118. needFindCount = pageSize
  119. }
  120. count := 1
  121. findCount := 0
  122. for _, existEntity := range classMap {
  123. existID := domain.Field[string](existEntity, "ID")
  124. existName := domain.Field[string](existEntity, "Name")
  125. if findCount >= needFindCount {
  126. break
  127. }
  128. if count >= startCount {
  129. find := false
  130. if strutils.IsStringNotEmpty(name) {
  131. if existName == name {
  132. find = true
  133. findCount++
  134. }
  135. } else {
  136. find = true
  137. findCount++
  138. }
  139. if find {
  140. classInfos = append(classInfos, ClassInfo{
  141. ID: existID,
  142. Name: existName,
  143. })
  144. }
  145. }
  146. count++
  147. }
  148. return binding.InfosData[ClassInfo]{
  149. Infos: classInfos,
  150. TotalCount: int64(len(classMap)),
  151. PageNo: pageNo,
  152. }, nil
  153. },
  154. })
  155. // 通过ID获取班级
  156. binding.GetBind(v1Router, &binding.SimpleBindItem[*ClassInfo]{
  157. Path: "/class/get",
  158. ResponseFunc: binding.SendInfoResponse[*ClassInfo],
  159. DTO: &GetClassQueryParams{},
  160. Objects: []domain.Object{&Class{}},
  161. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  162. id := domain.Field[string](objects[0], "ID")
  163. classInfo := new(ClassInfo)
  164. for _, existEntity := range classMap {
  165. if domain.Field[string](existEntity, "ID") == id {
  166. classInfo = &ClassInfo{
  167. ID: domain.Field[string](existEntity, "ID"),
  168. Name: domain.Field[string](existEntity, "Name"),
  169. }
  170. }
  171. }
  172. return classInfo, nil
  173. },
  174. })
  175. go func() {
  176. err := app.Start()
  177. if err != nil {
  178. panic(err)
  179. }
  180. }()
  181. defer func() {
  182. err := app.Finish()
  183. if err != nil {
  184. panic(err)
  185. }
  186. }()
  187. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  188. _ = death.WaitForDeath()
  189. }