main.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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:10000/test/v1/class/create"
  18. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"ab0d4a9acce44ddd91ad4746ecd34392", "name":"test-new"}' "http://localhost:10000/test/v1/class/update"
  19. // curl -X GET "http://localhost:10000/test/v1/class/query?name=test-new&pageNo=0&pageSize=1"
  20. // curl -X GET "http://localhost:10000/test/v1/class/get?id=ab0d4a9acce44ddd91ad4746ecd34392"
  21. // curl -X DELETE "http://localhost:10000/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: "10000",
  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().GetDBOperations()
  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. v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1")
  121. binding.SetGlobalInfrastructure(app.Infrastructure())
  122. // 创建班级
  123. binding.PostBind(v1Router, &binding.SimpleBindItem[string]{
  124. Path: "/class/create",
  125. ResponseFunc: binding.SendIDResponse[string],
  126. DTO: &CreateClassJsonBody{},
  127. Objects: []domain.Object{&Class{}},
  128. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  129. e := domain.ToConcreteObject[*Class](objects[0])
  130. e.ID = strutils.SimpleUUID()
  131. err := database.InsertEntity(i.GetDBOperations(), tableName, e)
  132. if err != nil {
  133. return "", err
  134. }
  135. return e.ID, nil
  136. },
  137. })
  138. // 删除班级
  139. binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
  140. Path: "/class/:id/delete",
  141. ResponseFunc: binding.SendMsgResponse,
  142. DTO: &DeleteClassPathParams{},
  143. Objects: []domain.Object{&Class{}},
  144. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  145. e := domain.ToConcreteObject[*Class](objects[0])
  146. err := database.DeleteEntity(i.GetDBOperations(), tableName, e)
  147. if err != nil {
  148. return "", err
  149. }
  150. return nil, nil
  151. },
  152. })
  153. // 修改班级
  154. binding.PutBind(v1Router, &binding.SimpleBindItem[any]{
  155. Path: "/class/update",
  156. ResponseFunc: binding.SendMsgResponse,
  157. DTO: &UpdateClassJsonBody{},
  158. Objects: []domain.Object{&Class{}},
  159. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  160. e := domain.ToConcreteObject[*Class](objects[0])
  161. result, err := database.QueryOne(i.GetDBOperations(), &sql.QueryOneExecuteParams{
  162. TableName: tableName,
  163. Conditions: sql.NewConditions().Equal("id", e.ID),
  164. })
  165. if err != nil {
  166. return nil, err
  167. }
  168. existClass := new(Class)
  169. err = sql.ParseSqlResult(result, existClass)
  170. if err != nil {
  171. return nil, err
  172. }
  173. newEntity := &Class{
  174. DomainIDField: &DomainIDField{ID: existClass.ID},
  175. }
  176. if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
  177. newEntity.Name = e.Name
  178. }
  179. err = database.UpdateEntity(i.GetDBOperations(), tableName, newEntity)
  180. if err != nil {
  181. return "", err
  182. }
  183. return nil, nil
  184. },
  185. })
  186. // 查询班级
  187. binding.GetBind(v1Router, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
  188. Path: "/class/query",
  189. ResponseFunc: binding.SendInfosResponse[ClassInfo],
  190. DTO: &QueryClassesQueryParams{},
  191. Objects: []domain.Object{&Class{}},
  192. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[ClassInfo], error) {
  193. e := domain.ToConcreteObject[*Class](objects[0])
  194. pageNo := binding.Field[int](dto, "PageNo")
  195. pageSize := binding.Field[int](dto, "PageSize")
  196. conditions := sql.NewConditions()
  197. if strutils.IsStringNotEmpty(e.Name) {
  198. conditions.Equal("name", e.Name)
  199. }
  200. results, totalCount, err := database.Query(i.GetDBOperations(), &sql.QueryExecuteParams{
  201. TableName: tableName,
  202. Conditions: conditions,
  203. PageNo: pageNo,
  204. PageSize: pageSize,
  205. })
  206. if err != nil {
  207. return binding.InfosData[ClassInfo]{
  208. Infos: make([]ClassInfo, 0),
  209. }, nil
  210. }
  211. classInfos := make([]ClassInfo, 0)
  212. err = sql.ParseSqlResult(results, &classInfos)
  213. if err != nil {
  214. return binding.InfosData[ClassInfo]{
  215. Infos: make([]ClassInfo, 0),
  216. }, nil
  217. }
  218. return binding.InfosData[ClassInfo]{
  219. Infos: classInfos,
  220. TotalCount: totalCount,
  221. PageNo: pageNo,
  222. }, nil
  223. },
  224. })
  225. // 通过ID获取班级
  226. binding.GetBind(v1Router, &binding.SimpleBindItem[*ClassInfo]{
  227. Path: "/class/get",
  228. ResponseFunc: binding.SendInfoResponse[*ClassInfo],
  229. DTO: &GetClassQueryParams{},
  230. Objects: []domain.Object{&Class{}},
  231. ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  232. e := domain.ToConcreteObject[*Class](objects[0])
  233. result, err := database.QueryOne(i.GetDBOperations(), &sql.QueryOneExecuteParams{
  234. TableName: tableName,
  235. Conditions: sql.NewConditions().Equal("id", e.ID),
  236. })
  237. if err != nil {
  238. return nil, err
  239. }
  240. info := new(ClassInfo)
  241. err = sql.ParseSqlResult(result, info)
  242. if err != nil {
  243. return nil, err
  244. }
  245. return info, nil
  246. },
  247. })
  248. go func() {
  249. err := app.Start()
  250. if err != nil {
  251. panic(err)
  252. }
  253. }()
  254. defer func() {
  255. err := app.Finish()
  256. if err != nil {
  257. panic(err)
  258. }
  259. }()
  260. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  261. _ = death.WaitForDeath()
  262. }