main.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package main
  2. import (
  3. "git.sxidc.com/go-framework/baize"
  4. "git.sxidc.com/go-framework/baize/api"
  5. "git.sxidc.com/go-framework/baize/application"
  6. "git.sxidc.com/go-framework/baize/binding"
  7. "git.sxidc.com/go-framework/baize/domain"
  8. "git.sxidc.com/go-framework/baize/infrastructure"
  9. "git.sxidc.com/go-framework/baize/infrastructure/database"
  10. "git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
  11. "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. DEATH "github.com/vrecan/death"
  14. "syscall"
  15. "time"
  16. )
  17. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:10100/test/v1/class/create"
  18. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"975750d39d674a57af2e49c670033ee8", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
  19. // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=0&pageSize=1"
  20. // curl -X GET "http://localhost:10100/test/v1/class/get?id=975750d39d674a57af2e49c670033ee8"
  21. // curl -X DELETE "http://localhost:10100/test/v1/class/975750d39d674a57af2e49c670033ee8/delete"
  22. type CreateClassJsonBody struct {
  23. Name string `json:"name" binding:"required" assign:"toField:Name"`
  24. }
  25. type DeleteClassPathParams struct {
  26. ID string `uri:"id" binding:"required" assign:"toField:ID"`
  27. }
  28. type UpdateClassJsonBody struct {
  29. ID string `json:"id" binding:"required" assign:"toField:ID"`
  30. Name string `json:"name" assign:"toField:Name"`
  31. }
  32. type QueryClassesQueryParams struct {
  33. Name string `form:"name" assign:"toField:Name"`
  34. PageNo int `form:"pageNo" assign:"-"`
  35. PageSize int `form:"pageSize" assign:"-"`
  36. }
  37. type GetClassQueryParams struct {
  38. ID string `form:"id" binding:"required" assign:"toField:ID"`
  39. }
  40. type DomainIDField struct {
  41. ID string `sqlmapping:"column:id"`
  42. }
  43. type Class struct {
  44. *DomainIDField
  45. Name string `sqlmapping:"column:name"`
  46. CreatedTime time.Time
  47. LastUpdatedTime *time.Time
  48. }
  49. type InfoIDField struct {
  50. ID string `json:"id" sqlresult:"column:id"`
  51. }
  52. type ClassInfo struct {
  53. *InfoIDField
  54. Name string `json:"name" sqlresult:"column:name"`
  55. CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  56. LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  57. }
  58. const (
  59. tableName = "test.classes"
  60. )
  61. func main() {
  62. app := baize.NewApplication(application.Config{
  63. ApiConfig: application.ApiConfig{
  64. UrlPrefix: "test",
  65. Port: "10100",
  66. },
  67. InfrastructureConfig: application.InfrastructureConfig{
  68. Database: infrastructure.DatabaseConfig{
  69. DataService: &data_service.Config{
  70. Token: "8qe+uPgpQ2JWxu3lSyOx5NjX+INp5WsnoD1ZWb7PBm4=",
  71. BaseUrl: "http://localhost:10000",
  72. GrpcAddress: "localhost:10001",
  73. Namespace: "baize",
  74. DataSource: "baize-binding",
  75. TimeoutSec: 30,
  76. },
  77. },
  78. },
  79. })
  80. defer func() {
  81. baize.DestroyApplication(app)
  82. }()
  83. app.Api().
  84. PrefixRouter().
  85. RegisterVersionedRouter("v1")
  86. binder := app.Binder("v1")
  87. // 创建班级
  88. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  89. Path: "/class/create",
  90. ResponseFunc: binding.SendIDResponse[string],
  91. DTO: &CreateClassJsonBody{},
  92. Objects: []domain.Object{&Class{}},
  93. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  94. e := domain.ToConcrete[*Class](objects[0])
  95. e.ID = strutils.SimpleUUID()
  96. err := database.InsertEntity(i.DataService(), tableName, e)
  97. if err != nil {
  98. return "", err
  99. }
  100. return e.ID, nil
  101. },
  102. })
  103. // 删除班级
  104. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  105. Path: "/class/:id/delete",
  106. ResponseFunc: binding.SendMsgResponse,
  107. DTO: &DeleteClassPathParams{},
  108. Objects: []domain.Object{&Class{}},
  109. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  110. e := domain.ToConcrete[*Class](objects[0])
  111. err := database.DeleteEntity(i.DataService(), tableName, e)
  112. if err != nil {
  113. return "", err
  114. }
  115. return nil, nil
  116. },
  117. })
  118. // 修改班级
  119. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  120. Path: "/class/update",
  121. ResponseFunc: binding.SendMsgResponse,
  122. DTO: &UpdateClassJsonBody{},
  123. Objects: []domain.Object{&Class{}},
  124. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  125. e := domain.ToConcrete[*Class](objects[0])
  126. result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
  127. TableName: tableName,
  128. Conditions: sql.NewConditions().Equal("id", e.ID),
  129. })
  130. if err != nil {
  131. return nil, err
  132. }
  133. existClass := new(Class)
  134. err = sql.ParseSqlResult(result, existClass)
  135. if err != nil {
  136. return nil, err
  137. }
  138. newEntity := &Class{
  139. DomainIDField: &DomainIDField{ID: existClass.ID},
  140. }
  141. if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
  142. newEntity.Name = e.Name
  143. }
  144. err = database.UpdateEntity(i.DataService(), tableName, newEntity)
  145. if err != nil {
  146. return "", err
  147. }
  148. return nil, nil
  149. },
  150. })
  151. // 查询班级
  152. binding.GetBind(binder, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
  153. Path: "/class/query",
  154. ResponseFunc: binding.SendInfosResponse[ClassInfo],
  155. DTO: &QueryClassesQueryParams{},
  156. Objects: []domain.Object{&Class{}},
  157. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
  158. e := domain.ToConcrete[*Class](objects[0])
  159. pageNo := binding.Field[int](dto, "PageNo")
  160. pageSize := binding.Field[int](dto, "PageSize")
  161. conditions := sql.NewConditions()
  162. if strutils.IsStringNotEmpty(e.Name) {
  163. conditions.Equal("name", e.Name)
  164. }
  165. results, totalCount, err := database.Query(i.DataService(), &sql.QueryExecuteParams{
  166. TableName: tableName,
  167. Conditions: conditions,
  168. PageNo: pageNo,
  169. PageSize: pageSize,
  170. })
  171. if err != nil {
  172. return binding.InfosData[ClassInfo]{
  173. Infos: make([]ClassInfo, 0),
  174. }, nil
  175. }
  176. classInfos := make([]ClassInfo, 0)
  177. err = sql.ParseSqlResult(results, &classInfos)
  178. if err != nil {
  179. return binding.InfosData[ClassInfo]{
  180. Infos: make([]ClassInfo, 0),
  181. }, nil
  182. }
  183. return binding.InfosData[ClassInfo]{
  184. Infos: classInfos,
  185. TotalCount: totalCount,
  186. PageNo: pageNo,
  187. }, nil
  188. },
  189. })
  190. // 通过ID获取班级
  191. binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
  192. Path: "/class/get",
  193. ResponseFunc: binding.SendInfoResponse[*ClassInfo],
  194. DTO: &GetClassQueryParams{},
  195. Objects: []domain.Object{&Class{}},
  196. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  197. e := domain.ToConcrete[*Class](objects[0])
  198. result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
  199. TableName: tableName,
  200. Conditions: sql.NewConditions().Equal("id", e.ID),
  201. })
  202. if err != nil {
  203. return nil, err
  204. }
  205. info := new(ClassInfo)
  206. err = sql.ParseSqlResult(result, info)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return info, nil
  211. },
  212. })
  213. go func() {
  214. err := app.Start()
  215. if err != nil {
  216. panic(err)
  217. }
  218. }()
  219. defer func() {
  220. err := app.Finish()
  221. if err != nil {
  222. panic(err)
  223. }
  224. }()
  225. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  226. _ = death.WaitForDeath()
  227. }