main.go 7.0 KB

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