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