main.go 6.9 KB

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