main.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/operations"
  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":"ab0d4a9acce44ddd91ad4746ecd34392", "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=ab0d4a9acce44ddd91ad4746ecd34392"
  21. // curl -X DELETE "http://localhost:10100/test/v1/class/ab0d4a9acce44ddd91ad4746ecd34392/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. Operations: &operations.Config{
  70. UserName: "test",
  71. Password: "123456",
  72. Address: "localhost",
  73. Port: "30432",
  74. Database: "test",
  75. MaxConnections: 40,
  76. MaxIdleConnections: 10,
  77. },
  78. },
  79. },
  80. })
  81. defer func() {
  82. baize.DestroyApplication(app)
  83. }()
  84. dbOperations := app.Infrastructure().DBOperations()
  85. err := dbOperations.AutoMigrate(operations.Table{
  86. TableName: tableName,
  87. Columns: []operations.TableColumn{
  88. {
  89. Name: "id",
  90. Type: "varchar(32)",
  91. Comment: "id",
  92. PrimaryKey: true,
  93. },
  94. {
  95. Name: "name",
  96. Type: "varchar(128)",
  97. Comment: "名称",
  98. NotNull: true,
  99. Index: true,
  100. },
  101. {
  102. Name: "created_time",
  103. Type: "timestamp with time zone",
  104. Comment: "创建时间",
  105. NotNull: true,
  106. Index: true,
  107. },
  108. {
  109. Name: "last_updated_time",
  110. Type: "timestamp with time zone",
  111. Comment: "更新时间",
  112. NotNull: true,
  113. Index: true,
  114. },
  115. },
  116. })
  117. if err != nil {
  118. panic(err)
  119. }
  120. app.Api().
  121. PrefixRouter().
  122. RegisterVersionedRouter("v1")
  123. binder := app.Binder("v1")
  124. // 创建班级
  125. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  126. Path: "/class/create",
  127. ResponseFunc: binding.SendIDResponse[string],
  128. DTO: &CreateClassJsonBody{},
  129. Objects: []domain.Object{&Class{}},
  130. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  131. e := domain.ToConcrete[*Class](objects[0])
  132. e.ID = strutils.SimpleUUID()
  133. err := database.InsertEntity(i.DBOperations(), tableName, e)
  134. if err != nil {
  135. return "", err
  136. }
  137. return e.ID, nil
  138. },
  139. })
  140. // 删除班级
  141. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  142. Path: "/class/:id/delete",
  143. ResponseFunc: binding.SendMsgResponse,
  144. DTO: &DeleteClassPathParams{},
  145. Objects: []domain.Object{&Class{}},
  146. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  147. e := domain.ToConcrete[*Class](objects[0])
  148. err := database.DeleteEntity(i.DBOperations(), tableName, e)
  149. if err != nil {
  150. return "", err
  151. }
  152. return nil, nil
  153. },
  154. })
  155. // 修改班级
  156. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  157. Path: "/class/update",
  158. ResponseFunc: binding.SendMsgResponse,
  159. DTO: &UpdateClassJsonBody{},
  160. Objects: []domain.Object{&Class{}},
  161. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  162. e := domain.ToConcrete[*Class](objects[0])
  163. result, err := database.QueryOne(i.DBOperations(), &sql.QueryOneExecuteParams{
  164. TableName: tableName,
  165. Conditions: sql.NewConditions().Equal("id", e.ID),
  166. })
  167. if err != nil {
  168. return nil, err
  169. }
  170. existClass := new(Class)
  171. err = sql.ParseSqlResult(result, existClass)
  172. if err != nil {
  173. return nil, err
  174. }
  175. newEntity := &Class{
  176. DomainIDField: &DomainIDField{ID: existClass.ID},
  177. }
  178. if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
  179. newEntity.Name = e.Name
  180. }
  181. err = database.UpdateEntity(i.DBOperations(), tableName, newEntity)
  182. if err != nil {
  183. return "", err
  184. }
  185. return nil, nil
  186. },
  187. })
  188. // 查询班级
  189. binding.GetBind(binder, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
  190. Path: "/class/query",
  191. ResponseFunc: binding.SendInfosResponse[ClassInfo],
  192. DTO: &QueryClassesQueryParams{},
  193. Objects: []domain.Object{&Class{}},
  194. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
  195. e := domain.ToConcrete[*Class](objects[0])
  196. pageNo := binding.Field[int](dto, "PageNo")
  197. pageSize := binding.Field[int](dto, "PageSize")
  198. conditions := sql.NewConditions()
  199. if strutils.IsStringNotEmpty(e.Name) {
  200. conditions.Equal("name", e.Name)
  201. }
  202. results, totalCount, err := database.Query(i.DBOperations(), &sql.QueryExecuteParams{
  203. TableName: tableName,
  204. Conditions: conditions,
  205. PageNo: pageNo,
  206. PageSize: pageSize,
  207. })
  208. if err != nil {
  209. return binding.InfosData[ClassInfo]{
  210. Infos: make([]ClassInfo, 0),
  211. }, nil
  212. }
  213. classInfos := make([]ClassInfo, 0)
  214. err = sql.ParseSqlResult(results, &classInfos)
  215. if err != nil {
  216. return binding.InfosData[ClassInfo]{
  217. Infos: make([]ClassInfo, 0),
  218. }, nil
  219. }
  220. return binding.InfosData[ClassInfo]{
  221. Infos: classInfos,
  222. TotalCount: totalCount,
  223. PageNo: pageNo,
  224. }, nil
  225. },
  226. })
  227. // 通过ID获取班级
  228. binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
  229. Path: "/class/get",
  230. ResponseFunc: binding.SendInfoResponse[*ClassInfo],
  231. DTO: &GetClassQueryParams{},
  232. Objects: []domain.Object{&Class{}},
  233. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  234. e := domain.ToConcrete[*Class](objects[0])
  235. result, err := database.QueryOne(i.DBOperations(), &sql.QueryOneExecuteParams{
  236. TableName: tableName,
  237. Conditions: sql.NewConditions().Equal("id", e.ID),
  238. })
  239. if err != nil {
  240. return nil, err
  241. }
  242. info := new(ClassInfo)
  243. err = sql.ParseSqlResult(result, info)
  244. if err != nil {
  245. return nil, err
  246. }
  247. return info, nil
  248. },
  249. })
  250. go func() {
  251. err := app.Start()
  252. if err != nil {
  253. panic(err)
  254. }
  255. }()
  256. defer func() {
  257. err := app.Finish()
  258. if err != nil {
  259. panic(err)
  260. }
  261. }()
  262. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  263. _ = death.WaitForDeath()
  264. }