main.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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":"1fb7faa332af42fd9a41856320c0d4af", "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=1fb7faa332af42fd9a41856320c0d4af"
  17. // curl -X DELETE "http://localhost:10000/test/v1/class/1fb7faa332af42fd9a41856320c0d4af/delete"
  18. type CreateClassJsonBody struct {
  19. Name string `json:"name" binding:"required"`
  20. }
  21. type DeleteClassPathParams struct {
  22. ID string `uri:"id" binding:"required"`
  23. }
  24. type UpdateClassJsonBody struct {
  25. ID string `json:"id" binding:"required"`
  26. Name string `json:"name"`
  27. }
  28. type QueryClassesQueryParams struct {
  29. Name string `form:"id"`
  30. PageNo int `form:"pageNo"`
  31. PageSize int `form:"pageSize"`
  32. }
  33. type GetClassQueryParams struct {
  34. ID string `form:"id" binding:"required"`
  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. FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
  59. return []domain.Object{
  60. &Class{
  61. Name: binding.Field[string](dto, "Name"),
  62. },
  63. }, nil
  64. },
  65. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (string, error) {
  66. id := strutils.SimpleUUID()
  67. domain.SetField(objects[0], "ID", id)
  68. classMap[id] = objects[0]
  69. return id, nil
  70. },
  71. })
  72. // 删除班级
  73. binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
  74. Path: "/class/:id/delete",
  75. ResponseFunc: binding.SendMsgResponse,
  76. DTO: &DeleteClassPathParams{},
  77. FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
  78. return []domain.Object{
  79. &Class{
  80. ID: binding.Field[string](dto, "ID"),
  81. },
  82. }, nil
  83. },
  84. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (any, error) {
  85. id := domain.Field[string](objects[0], "ID")
  86. delete(classMap, id)
  87. fmt.Println("Deleted Entity:" + id)
  88. return nil, nil
  89. },
  90. })
  91. // 修改班级
  92. binding.PutBind(v1Router, &binding.SimpleBindItem[any]{
  93. Path: "/class/update",
  94. ResponseFunc: binding.SendMsgResponse,
  95. DTO: &UpdateClassJsonBody{},
  96. FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
  97. return []domain.Object{
  98. &Class{
  99. ID: binding.Field[string](dto, "ID"),
  100. Name: binding.Field[string](dto, "Name"),
  101. },
  102. }, nil
  103. },
  104. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (any, error) {
  105. id := domain.Field[string](objects[0], "ID")
  106. newName := domain.Field[string](objects[0], "Name")
  107. existEntity, ok := classMap[id]
  108. if !ok {
  109. fmt.Println("Update Entity:" + id)
  110. fmt.Println("Not Find")
  111. return nil, nil
  112. }
  113. domain.SetField(existEntity, "Name", newName)
  114. fmt.Println("Update Entity:" + id)
  115. fmt.Println("Name:" + newName)
  116. return nil, nil
  117. },
  118. })
  119. // 查询班级
  120. binding.GetBind(v1Router, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
  121. Path: "/class/query",
  122. ResponseFunc: binding.SendInfosResponse[ClassInfo],
  123. DTO: &QueryClassesQueryParams{},
  124. FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
  125. return []domain.Object{
  126. &Class{
  127. Name: binding.Field[string](dto, "Name"),
  128. },
  129. }, nil
  130. },
  131. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (binding.InfosData[ClassInfo], error) {
  132. name := domain.Field[string](objects[0], "Name")
  133. classInfos := make([]ClassInfo, 0)
  134. pageNo := binding.Field[int](dto, "PageNo")
  135. pageSize := binding.Field[int](dto, "PageSize")
  136. startCount := 1
  137. if pageNo != 0 && pageSize != 0 {
  138. startCount = pageNo*pageSize + 1
  139. }
  140. needFindCount := len(classMap)
  141. if pageNo != 0 && pageSize != 0 {
  142. needFindCount = pageSize
  143. }
  144. count := 1
  145. findCount := 0
  146. for _, existEntity := range classMap {
  147. existID := domain.Field[string](existEntity, "ID")
  148. existName := domain.Field[string](existEntity, "Name")
  149. if findCount >= needFindCount {
  150. break
  151. }
  152. if count >= startCount {
  153. find := false
  154. if strutils.IsStringNotEmpty(name) {
  155. if existName == name {
  156. find = true
  157. findCount++
  158. }
  159. } else {
  160. find = true
  161. findCount++
  162. }
  163. if find {
  164. classInfos = append(classInfos, ClassInfo{
  165. ID: existID,
  166. Name: existName,
  167. })
  168. }
  169. }
  170. count++
  171. }
  172. return binding.InfosData[ClassInfo]{
  173. Infos: classInfos,
  174. TotalCount: int64(len(classMap)),
  175. PageNo: pageNo,
  176. }, nil
  177. },
  178. })
  179. // 通过ID获取班级
  180. binding.GetBind(v1Router, &binding.SimpleBindItem[*ClassInfo]{
  181. Path: "/class/get",
  182. ResponseFunc: binding.SendInfoResponse[*ClassInfo],
  183. DTO: &GetClassQueryParams{},
  184. FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
  185. return []domain.Object{
  186. &Class{
  187. ID: binding.Field[string](dto, "ID"),
  188. },
  189. }, nil
  190. },
  191. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (*ClassInfo, error) {
  192. id := domain.Field[string](objects[0], "ID")
  193. classInfo := new(ClassInfo)
  194. for _, existEntity := range classMap {
  195. if domain.Field[string](existEntity, "ID") == id {
  196. classInfo = &ClassInfo{
  197. ID: domain.Field[string](existEntity, "ID"),
  198. Name: domain.Field[string](existEntity, "Name"),
  199. }
  200. }
  201. }
  202. return classInfo, nil
  203. },
  204. })
  205. go func() {
  206. err := app.Start()
  207. if err != nil {
  208. panic(err)
  209. }
  210. }()
  211. defer func() {
  212. err := app.Finish()
  213. if err != nil {
  214. panic(err)
  215. }
  216. }()
  217. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  218. _ = death.WaitForDeath()
  219. }