main.go 7.5 KB

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