main.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package main
  2. import (
  3. "git.sxidc.com/go-framework/baize"
  4. "git.sxidc.com/go-framework/baize/application"
  5. "git.sxidc.com/go-framework/baize/binding"
  6. "git.sxidc.com/go-framework/baize/binding/request"
  7. "git.sxidc.com/go-framework/baize/binding/response"
  8. "git.sxidc.com/go-framework/baize/binding/service/entity_crud"
  9. "git.sxidc.com/go-framework/baize/domain"
  10. "git.sxidc.com/go-framework/baize/infrastructure"
  11. "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
  12. DEATH "github.com/vrecan/death"
  13. "syscall"
  14. "time"
  15. )
  16. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
  17. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"10ec51a6485b4fadb9fc1b4f1360b8d0", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
  18. // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
  19. // curl -X GET "http://localhost:10100/test/v1/class/get?id=10ec51a6485b4fadb9fc1b4f1360b8d0"
  20. // curl -X DELETE "http://localhost:10100/test/v1/class/10ec51a6485b4fadb9fc1b4f1360b8d0/delete"
  21. type CreateClassJsonBody struct {
  22. Name string `json:"name" binding:"required" assign:"toField:Name"`
  23. StudentNum int `json:"studentNum" binding:"required" assign:"toField:StudentNum"`
  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. StudentNum int `json:"studentNum" assign:"toField:StudentNum"`
  32. }
  33. type QueryClassesQueryParams struct {
  34. Name string `form:"name" assign:"toField:Name"`
  35. StudentNum int `form:"studentNum" assign:"toField:StudentNum"`
  36. request.BaseQuery
  37. }
  38. type GetClassQueryParams struct {
  39. ID string `form:"id" binding:"required" assign:"toField:ID"`
  40. }
  41. type Class struct {
  42. domain.BaseEntity
  43. Name string `sqlmapping:"column:name"`
  44. StudentNum int `sqlmapping:"column:student_num"`
  45. CreatedTime time.Time
  46. LastUpdatedTime *time.Time
  47. }
  48. func (class *Class) DomainCNName() string {
  49. return "班级"
  50. }
  51. type InfoIDField struct {
  52. ID string `json:"id" sqlresult:"column:id"`
  53. }
  54. type ClassInfo struct {
  55. *InfoIDField
  56. Name string `json:"name" sqlresult:"column:name"`
  57. StudentNum int `json:"studentNum" sqlresult:"column:student_num"`
  58. CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  59. LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  60. }
  61. const (
  62. tableName = "test.classes"
  63. )
  64. func main() {
  65. app := baize.NewApplication(application.Config{
  66. ApiConfig: application.ApiConfig{
  67. UrlPrefix: "test",
  68. Port: "10100",
  69. },
  70. InfrastructureConfig: application.InfrastructureConfig{
  71. Database: infrastructure.DatabaseConfig{
  72. Operations: &operations.Config{
  73. UserName: "test",
  74. Password: "123456",
  75. Address: "localhost",
  76. Port: "30432",
  77. Database: "test",
  78. MaxConnections: 40,
  79. MaxIdleConnections: 10,
  80. },
  81. },
  82. },
  83. })
  84. defer func() {
  85. baize.DestroyApplication(app)
  86. }()
  87. dbOperations := app.Infrastructure().DBOperations()
  88. err := dbOperations.AutoMigrate(operations.Table{
  89. TableName: tableName,
  90. Columns: []operations.TableColumn{
  91. {
  92. Name: "id",
  93. Type: "varchar(32)",
  94. Comment: "id",
  95. PrimaryKey: true,
  96. },
  97. {
  98. Name: "name",
  99. Type: "varchar(128)",
  100. Comment: "名称",
  101. NotNull: true,
  102. Index: true,
  103. },
  104. {
  105. Name: "student_num",
  106. Type: "integer",
  107. Comment: "学生数量",
  108. NotNull: true,
  109. Index: true,
  110. },
  111. {
  112. Name: "created_time",
  113. Type: "timestamp with time zone",
  114. Comment: "创建时间",
  115. NotNull: true,
  116. Index: true,
  117. },
  118. {
  119. Name: "last_updated_time",
  120. Type: "timestamp with time zone",
  121. Comment: "更新时间",
  122. NotNull: true,
  123. Index: true,
  124. },
  125. },
  126. })
  127. if err != nil {
  128. panic(err)
  129. }
  130. app.Api().
  131. PrefixRouter().
  132. RegisterVersionedRouter("v1")
  133. binder := app.Binder("v1")
  134. // 创建班级
  135. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  136. Path: "/class/create",
  137. ResponseFunc: response.SendIDResponse[string],
  138. DTO: &CreateClassJsonBody{},
  139. Objects: []domain.Object{&Class{}},
  140. ServiceFunc: entity_crud.CommonEntityCreate(tableName, infrastructure.DBExecutorOperations, nil),
  141. })
  142. // 删除班级
  143. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  144. Path: "/class/:id/delete",
  145. ResponseFunc: response.SendMsgResponse,
  146. DTO: &DeleteClassPathParams{},
  147. Objects: []domain.Object{&Class{}},
  148. ServiceFunc: entity_crud.CommonEntityDelete(tableName, infrastructure.DBExecutorOperations, nil),
  149. })
  150. // 修改班级
  151. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  152. Path: "/class/update",
  153. ResponseFunc: response.SendMsgResponse,
  154. DTO: &UpdateClassJsonBody{},
  155. Objects: []domain.Object{&Class{}},
  156. ServiceFunc: entity_crud.CommonEntityUpdate(tableName, infrastructure.DBExecutorOperations, nil),
  157. })
  158. // 查询班级
  159. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[ClassInfo]]{
  160. Path: "/class/query",
  161. ResponseFunc: response.SendInfosResponse[ClassInfo],
  162. DTO: &QueryClassesQueryParams{},
  163. Objects: []domain.Object{&Class{}},
  164. ServiceFunc: entity_crud.CommonEntityQuery[ClassInfo](tableName, infrastructure.DBExecutorOperations, nil, nil),
  165. })
  166. // 通过ID获取班级
  167. binding.GetBind(binder, &binding.SimpleBindItem[ClassInfo]{
  168. Path: "/class/get",
  169. ResponseFunc: response.SendInfoResponse[ClassInfo],
  170. DTO: &GetClassQueryParams{},
  171. Objects: []domain.Object{&Class{}},
  172. ServiceFunc: entity_crud.CommonEntityQueryByID[ClassInfo](tableName, infrastructure.DBExecutorOperations, nil),
  173. })
  174. go func() {
  175. err := app.Start()
  176. if err != nil {
  177. panic(err)
  178. }
  179. }()
  180. defer func() {
  181. err := app.Finish()
  182. if err != nil {
  183. panic(err)
  184. }
  185. }()
  186. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  187. _ = death.WaitForDeath()
  188. }