main.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/binding/request"
  8. "git.sxidc.com/go-framework/baize/binding/response"
  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"
  12. "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
  13. "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
  14. "git.sxidc.com/go-tools/utils/strutils"
  15. DEATH "github.com/vrecan/death"
  16. "syscall"
  17. "time"
  18. )
  19. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:10100/test/v1/class/create"
  20. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"ab0d4a9acce44ddd91ad4746ecd34392", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
  21. // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=0&pageSize=1"
  22. // curl -X GET "http://localhost:10100/test/v1/class/get?id=ab0d4a9acce44ddd91ad4746ecd34392"
  23. // curl -X DELETE "http://localhost:10100/test/v1/class/ab0d4a9acce44ddd91ad4746ecd34392/delete"
  24. type CreateClassJsonBody struct {
  25. Name string `json:"name" binding:"required" assign:"toField:Name"`
  26. }
  27. type DeleteClassPathParams struct {
  28. ID string `uri:"id" binding:"required" assign:"toField:ID"`
  29. }
  30. type UpdateClassJsonBody struct {
  31. ID string `json:"id" binding:"required" assign:"toField:ID"`
  32. Name string `json:"name" assign:"toField:Name"`
  33. }
  34. type QueryClassesQueryParams struct {
  35. Name string `form:"name" assign:"toField:Name"`
  36. PageNo int `form:"pageNo" assign:"-"`
  37. PageSize int `form:"pageSize" assign:"-"`
  38. }
  39. type GetClassQueryParams struct {
  40. ID string `form:"id" binding:"required" assign:"toField:ID"`
  41. }
  42. type DomainIDField struct {
  43. ID string `sqlmapping:"column:id"`
  44. }
  45. type Class struct {
  46. *DomainIDField
  47. Name string `sqlmapping:"column:name"`
  48. CreatedTime time.Time
  49. LastUpdatedTime *time.Time
  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. CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  58. LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  59. }
  60. const (
  61. tableName = "test.classes"
  62. )
  63. func main() {
  64. app := baize.NewApplication(application.Config{
  65. ApiConfig: application.ApiConfig{
  66. UrlPrefix: "test",
  67. Port: "10100",
  68. },
  69. InfrastructureConfig: application.InfrastructureConfig{
  70. Database: infrastructure.DatabaseConfig{
  71. Operations: &operations.Config{
  72. UserName: "test",
  73. Password: "123456",
  74. Address: "localhost",
  75. Port: "30432",
  76. Database: "test",
  77. MaxConnections: 40,
  78. MaxIdleConnections: 10,
  79. },
  80. },
  81. },
  82. })
  83. defer func() {
  84. baize.DestroyApplication(app)
  85. }()
  86. dbOperations := app.Infrastructure().DBOperations()
  87. err := dbOperations.AutoMigrate(operations.Table{
  88. TableName: tableName,
  89. Columns: []operations.TableColumn{
  90. {
  91. Name: "id",
  92. Type: "varchar(32)",
  93. Comment: "id",
  94. PrimaryKey: true,
  95. },
  96. {
  97. Name: "name",
  98. Type: "varchar(128)",
  99. Comment: "名称",
  100. NotNull: true,
  101. Index: true,
  102. },
  103. {
  104. Name: "created_time",
  105. Type: "timestamp with time zone",
  106. Comment: "创建时间",
  107. NotNull: true,
  108. Index: true,
  109. },
  110. {
  111. Name: "last_updated_time",
  112. Type: "timestamp with time zone",
  113. Comment: "更新时间",
  114. NotNull: true,
  115. Index: true,
  116. },
  117. },
  118. })
  119. if err != nil {
  120. panic(err)
  121. }
  122. app.Api().
  123. PrefixRouter().
  124. RegisterVersionedRouter("v1")
  125. binder := app.Binder("v1")
  126. // 创建班级
  127. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  128. Path: "/class/create",
  129. ResponseFunc: response.SendIDResponse[string],
  130. DTO: &CreateClassJsonBody{},
  131. Objects: []domain.Object{&Class{}},
  132. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  133. e := domain.ToConcrete[*Class](objects[0])
  134. e.ID = strutils.SimpleUUID()
  135. err := database.InsertEntity(i.DBOperations(), tableName, e)
  136. if err != nil {
  137. return "", err
  138. }
  139. return e.ID, nil
  140. },
  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: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  149. e := domain.ToConcrete[*Class](objects[0])
  150. err := database.DeleteEntity(i.DBOperations(), tableName, e)
  151. if err != nil {
  152. return "", err
  153. }
  154. return nil, nil
  155. },
  156. })
  157. // 修改班级
  158. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  159. Path: "/class/update",
  160. ResponseFunc: response.SendMsgResponse,
  161. DTO: &UpdateClassJsonBody{},
  162. Objects: []domain.Object{&Class{}},
  163. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  164. e := domain.ToConcrete[*Class](objects[0])
  165. result, err := database.QueryOne(i.DBOperations(), &sql.QueryOneExecuteParams{
  166. TableName: tableName,
  167. Conditions: sql.NewConditions().Equal("id", e.ID),
  168. })
  169. if err != nil {
  170. return nil, err
  171. }
  172. existClass := new(Class)
  173. err = sql.ParseSqlResult(result, existClass)
  174. if err != nil {
  175. return nil, err
  176. }
  177. newEntity := &Class{
  178. DomainIDField: &DomainIDField{ID: existClass.ID},
  179. }
  180. if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
  181. newEntity.Name = e.Name
  182. }
  183. err = database.UpdateEntity(i.DBOperations(), tableName, newEntity)
  184. if err != nil {
  185. return "", err
  186. }
  187. return nil, nil
  188. },
  189. })
  190. // 查询班级
  191. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[ClassInfo]]{
  192. Path: "/class/query",
  193. ResponseFunc: response.SendInfosResponse[ClassInfo],
  194. DTO: &QueryClassesQueryParams{},
  195. Objects: []domain.Object{&Class{}},
  196. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[ClassInfo], error) {
  197. e := domain.ToConcrete[*Class](objects[0])
  198. pageNo := dto.Field[int](dto, "PageNo")
  199. pageSize := dto.Field[int](dto, "PageSize")
  200. conditions := sql.NewConditions()
  201. if strutils.IsStringNotEmpty(e.Name) {
  202. conditions.Equal("name", e.Name)
  203. }
  204. results, totalCount, err := database.Query(i.DBOperations(), &sql.QueryExecuteParams{
  205. TableName: tableName,
  206. Conditions: conditions,
  207. PageNo: pageNo,
  208. PageSize: pageSize,
  209. })
  210. if err != nil {
  211. return response.InfosData[ClassInfo]{
  212. Infos: make([]ClassInfo, 0),
  213. }, nil
  214. }
  215. classInfos := make([]ClassInfo, 0)
  216. err = sql.ParseSqlResult(results, &classInfos)
  217. if err != nil {
  218. return response.InfosData[ClassInfo]{
  219. Infos: make([]ClassInfo, 0),
  220. }, nil
  221. }
  222. return response.InfosData[ClassInfo]{
  223. Infos: classInfos,
  224. TotalCount: totalCount,
  225. PageNo: pageNo,
  226. }, nil
  227. },
  228. })
  229. // 通过ID获取班级
  230. binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
  231. Path: "/class/get",
  232. ResponseFunc: response.SendInfoResponse[*ClassInfo],
  233. DTO: &GetClassQueryParams{},
  234. Objects: []domain.Object{&Class{}},
  235. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  236. e := domain.ToConcrete[*Class](objects[0])
  237. result, err := database.QueryOne(i.DBOperations(), &sql.QueryOneExecuteParams{
  238. TableName: tableName,
  239. Conditions: sql.NewConditions().Equal("id", e.ID),
  240. })
  241. if err != nil {
  242. return nil, err
  243. }
  244. info := new(ClassInfo)
  245. err = sql.ParseSqlResult(result, info)
  246. if err != nil {
  247. return nil, err
  248. }
  249. return info, nil
  250. },
  251. })
  252. go func() {
  253. err := app.Start()
  254. if err != nil {
  255. panic(err)
  256. }
  257. }()
  258. defer func() {
  259. err := app.Finish()
  260. if err != nil {
  261. panic(err)
  262. }
  263. }()
  264. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  265. _ = death.WaitForDeath()
  266. }