main.go 6.8 KB

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