main.go 6.0 KB

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