main.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/data_service"
  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":"975750d39d674a57af2e49c670033ee8", "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=975750d39d674a57af2e49c670033ee8"
  23. // curl -X DELETE "http://localhost:10100/test/v1/class/975750d39d674a57af2e49c670033ee8/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. DataService: &data_service.Config{
  72. Token: "8qe+uPgpQ2JWxu3lSyOx5NjX+INp5WsnoD1ZWb7PBm4=",
  73. BaseUrl: "http://localhost:10000",
  74. GrpcAddress: "localhost:10001",
  75. Namespace: "baize",
  76. DataSource: "baize-binding",
  77. TimeoutSec: 30,
  78. },
  79. },
  80. },
  81. })
  82. defer func() {
  83. baize.DestroyApplication(app)
  84. }()
  85. app.Api().
  86. PrefixRouter().
  87. RegisterVersionedRouter("v1")
  88. binder := app.Binder("v1")
  89. // 创建班级
  90. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  91. Path: "/class/create",
  92. ResponseFunc: response.SendIDResponse[string],
  93. DTO: &CreateClassJsonBody{},
  94. Objects: []domain.Object{&Class{}},
  95. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  96. e := domain.ToConcrete[*Class](objects[0])
  97. e.ID = strutils.SimpleUUID()
  98. err := database.InsertEntity(i.DataService(), tableName, e)
  99. if err != nil {
  100. return "", err
  101. }
  102. return e.ID, nil
  103. },
  104. })
  105. // 删除班级
  106. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  107. Path: "/class/:id/delete",
  108. ResponseFunc: response.SendMsgResponse,
  109. DTO: &DeleteClassPathParams{},
  110. Objects: []domain.Object{&Class{}},
  111. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  112. e := domain.ToConcrete[*Class](objects[0])
  113. err := database.DeleteEntity(i.DataService(), tableName, e)
  114. if err != nil {
  115. return "", err
  116. }
  117. return nil, nil
  118. },
  119. })
  120. // 修改班级
  121. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  122. Path: "/class/update",
  123. ResponseFunc: response.SendMsgResponse,
  124. DTO: &UpdateClassJsonBody{},
  125. Objects: []domain.Object{&Class{}},
  126. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  127. e := domain.ToConcrete[*Class](objects[0])
  128. result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
  129. TableName: tableName,
  130. Conditions: sql.NewConditions().Equal("id", e.ID),
  131. })
  132. if err != nil {
  133. return nil, err
  134. }
  135. existClass := new(Class)
  136. err = sql.ParseSqlResult(result, existClass)
  137. if err != nil {
  138. return nil, err
  139. }
  140. newEntity := &Class{
  141. DomainIDField: &DomainIDField{ID: existClass.ID},
  142. }
  143. if strutils.IsStringNotEmpty(e.Name) && e.Name != existClass.Name {
  144. newEntity.Name = e.Name
  145. }
  146. err = database.UpdateEntity(i.DataService(), tableName, newEntity)
  147. if err != nil {
  148. return "", err
  149. }
  150. return nil, nil
  151. },
  152. })
  153. // 查询班级
  154. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[ClassInfo]]{
  155. Path: "/class/query",
  156. ResponseFunc: response.SendInfosResponse[ClassInfo],
  157. DTO: &QueryClassesQueryParams{},
  158. Objects: []domain.Object{&Class{}},
  159. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[ClassInfo], error) {
  160. e := domain.ToConcrete[*Class](objects[0])
  161. pageNo := dto.Field[int](dto, "PageNo")
  162. pageSize := dto.Field[int](dto, "PageSize")
  163. conditions := sql.NewConditions()
  164. if strutils.IsStringNotEmpty(e.Name) {
  165. conditions.Equal("name", e.Name)
  166. }
  167. results, totalCount, err := database.Query(i.DataService(), &sql.QueryExecuteParams{
  168. TableName: tableName,
  169. Conditions: conditions,
  170. PageNo: pageNo,
  171. PageSize: pageSize,
  172. })
  173. if err != nil {
  174. return response.InfosData[ClassInfo]{
  175. Infos: make([]ClassInfo, 0),
  176. }, nil
  177. }
  178. classInfos := make([]ClassInfo, 0)
  179. err = sql.ParseSqlResult(results, &classInfos)
  180. if err != nil {
  181. return response.InfosData[ClassInfo]{
  182. Infos: make([]ClassInfo, 0),
  183. }, nil
  184. }
  185. return response.InfosData[ClassInfo]{
  186. Infos: classInfos,
  187. TotalCount: totalCount,
  188. PageNo: pageNo,
  189. }, nil
  190. },
  191. })
  192. // 通过ID获取班级
  193. binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
  194. Path: "/class/get",
  195. ResponseFunc: response.SendInfoResponse[*ClassInfo],
  196. DTO: &GetClassQueryParams{},
  197. Objects: []domain.Object{&Class{}},
  198. ServiceFunc: func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (*ClassInfo, error) {
  199. e := domain.ToConcrete[*Class](objects[0])
  200. result, err := database.QueryOne(i.DataService(), &sql.QueryOneExecuteParams{
  201. TableName: tableName,
  202. Conditions: sql.NewConditions().Equal("id", e.ID),
  203. })
  204. if err != nil {
  205. return nil, err
  206. }
  207. info := new(ClassInfo)
  208. err = sql.ParseSqlResult(result, info)
  209. if err != nil {
  210. return nil, err
  211. }
  212. return info, nil
  213. },
  214. })
  215. go func() {
  216. err := app.Start()
  217. if err != nil {
  218. panic(err)
  219. }
  220. }()
  221. defer func() {
  222. err := app.Finish()
  223. if err != nil {
  224. panic(err)
  225. }
  226. }()
  227. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  228. _ = death.WaitForDeath()
  229. }