main.go 6.7 KB

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