|
|
@@ -0,0 +1,288 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "git.sxidc.com/go-framework/baize"
|
|
|
+ "git.sxidc.com/go-framework/baize/api"
|
|
|
+ "git.sxidc.com/go-framework/baize/application"
|
|
|
+ "git.sxidc.com/go-framework/baize/binding"
|
|
|
+ "git.sxidc.com/go-framework/baize/domain"
|
|
|
+ "git.sxidc.com/go-tools/utils/strutils"
|
|
|
+ DEATH "github.com/vrecan/death"
|
|
|
+ "syscall"
|
|
|
+)
|
|
|
+
|
|
|
+type CreateClassJsonBody struct {
|
|
|
+ Name string `json:"name" binding:"required"`
|
|
|
+}
|
|
|
+
|
|
|
+type DeleteClassPathParams struct {
|
|
|
+ ID string `uri:"id" binding:"required"`
|
|
|
+}
|
|
|
+
|
|
|
+type UpdateClassJsonBody struct {
|
|
|
+ ID string `json:"id" binding:"required"`
|
|
|
+ Name string `json:"name"`
|
|
|
+}
|
|
|
+
|
|
|
+type QueryClassesQueryParams struct {
|
|
|
+ Name string `form:"id"`
|
|
|
+ PageNo int `form:"pageNo"`
|
|
|
+ PageSize int `form:"pageSize"`
|
|
|
+}
|
|
|
+
|
|
|
+type GetClassQueryParams struct {
|
|
|
+ ID string `form:"id" binding:"required"`
|
|
|
+}
|
|
|
+
|
|
|
+type Class struct {
|
|
|
+ ID string
|
|
|
+ Name string
|
|
|
+}
|
|
|
+
|
|
|
+type ClassInfo struct {
|
|
|
+ ID string `json:"id"`
|
|
|
+ Name string `json:"name"`
|
|
|
+}
|
|
|
+
|
|
|
+var classMap = make(map[string]*Class, 0)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ app := baize.NewApplication(application.Config{
|
|
|
+ ApiConfig: application.ApiConfig{
|
|
|
+ UrlPrefix: "test",
|
|
|
+ Port: "10000",
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1")
|
|
|
+
|
|
|
+ // 创建班级
|
|
|
+ binding.PostBind(v1Router, &binding.SimpleBindItem[string]{
|
|
|
+ Path: "/class/create",
|
|
|
+ ResponseFunc: binding.SendIDResponse[string],
|
|
|
+ DTO: &CreateClassJsonBody{},
|
|
|
+ FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
|
|
|
+ jsonBody, err := binding.ToConcreteDTO[*CreateClassJsonBody](dto)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return []domain.Object{
|
|
|
+ &Class{
|
|
|
+ Name: jsonBody.Name,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (string, error) {
|
|
|
+ e, err := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ e.ID = strutils.SimpleUUID()
|
|
|
+ classMap[e.ID] = e
|
|
|
+
|
|
|
+ return e.ID, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ // 删除班级
|
|
|
+ binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
|
|
|
+ Path: "/class/delete",
|
|
|
+ ResponseFunc: binding.SendMsgResponse,
|
|
|
+ DTO: &DeleteClassPathParams{},
|
|
|
+ FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
|
|
|
+ pathParams, err := binding.ToConcreteDTO[*DeleteClassPathParams](dto)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return []domain.Object{
|
|
|
+ &Class{
|
|
|
+ ID: pathParams.ID,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (any, error) {
|
|
|
+ e, err := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ delete(classMap, e.ID)
|
|
|
+
|
|
|
+ fmt.Println("Deleted Entity:" + e.ID)
|
|
|
+ return nil, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ // 修改班级
|
|
|
+ binding.PutBind(v1Router, &binding.SimpleBindItem[any]{
|
|
|
+ Path: "/class/update",
|
|
|
+ ResponseFunc: binding.SendMsgResponse,
|
|
|
+ DTO: &UpdateClassJsonBody{},
|
|
|
+ FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
|
|
|
+ jsonBody, err := binding.ToConcreteDTO[*UpdateClassJsonBody](dto)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return []domain.Object{
|
|
|
+ &Class{
|
|
|
+ ID: jsonBody.ID,
|
|
|
+ Name: jsonBody.Name,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (any, error) {
|
|
|
+ e, err := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ existEntity, ok := classMap[e.ID]
|
|
|
+ if !ok {
|
|
|
+ fmt.Println("Update Entity:" + e.ID)
|
|
|
+ fmt.Println("Not Find")
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ existEntity.Name = e.Name
|
|
|
+ classMap[e.ID] = existEntity
|
|
|
+
|
|
|
+ fmt.Println("Update Entity:" + existEntity.ID)
|
|
|
+ fmt.Println("Name:" + existEntity.Name)
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ // 查询班级
|
|
|
+ binding.GetBind(v1Router, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
|
|
|
+ Path: "/class/query",
|
|
|
+ ResponseFunc: binding.SendInfosResponse[ClassInfo],
|
|
|
+ DTO: &QueryClassesQueryParams{},
|
|
|
+ FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
|
|
|
+ queryParams, err := binding.ToConcreteDTO[*QueryClassesQueryParams](dto)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return []domain.Object{
|
|
|
+ &Class{
|
|
|
+ Name: queryParams.Name,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (binding.InfosData[ClassInfo], error) {
|
|
|
+ classInfos := make([]ClassInfo, 0)
|
|
|
+
|
|
|
+ e, err := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ if err != nil {
|
|
|
+ return binding.InfosData[ClassInfo]{
|
|
|
+ Infos: classInfos,
|
|
|
+ TotalCount: 0,
|
|
|
+ PageNo: 0,
|
|
|
+ }, err
|
|
|
+ }
|
|
|
+
|
|
|
+ queryParams, err := binding.ToConcreteDTO[*QueryClassesQueryParams](dto)
|
|
|
+ if err != nil {
|
|
|
+ return binding.InfosData[ClassInfo]{
|
|
|
+ Infos: classInfos,
|
|
|
+ TotalCount: 0,
|
|
|
+ PageNo: 0,
|
|
|
+ }, err
|
|
|
+ }
|
|
|
+
|
|
|
+ startCount := 1
|
|
|
+ if queryParams.PageNo != 0 && queryParams.PageSize != 0 {
|
|
|
+ startCount = queryParams.PageNo*queryParams.PageSize + 1
|
|
|
+ }
|
|
|
+
|
|
|
+ endCount := len(classMap)
|
|
|
+ if queryParams.PageNo != 0 && queryParams.PageSize != 0 {
|
|
|
+ endCount = queryParams.PageSize
|
|
|
+ }
|
|
|
+
|
|
|
+ count := 1
|
|
|
+ for _, existEntity := range classMap {
|
|
|
+ if count >= endCount {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ if count >= startCount {
|
|
|
+ if existEntity.Name == e.Name {
|
|
|
+ classInfos = append(classInfos, ClassInfo{
|
|
|
+ ID: existEntity.ID,
|
|
|
+ Name: existEntity.Name,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ count++
|
|
|
+ }
|
|
|
+
|
|
|
+ return binding.InfosData[ClassInfo]{
|
|
|
+ Infos: classInfos,
|
|
|
+ TotalCount: int64(len(classMap)),
|
|
|
+ PageNo: queryParams.PageNo,
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ // 通过ID获取班级
|
|
|
+ binding.GetBind(v1Router, &binding.SimpleBindItem[*ClassInfo]{
|
|
|
+ Path: "/class/get",
|
|
|
+ ResponseFunc: binding.SendInfoResponse[*ClassInfo],
|
|
|
+ DTO: &GetClassQueryParams{},
|
|
|
+ FormDomainObjectsFunc: func(c *api.Context, dto binding.DTO) ([]domain.Object, error) {
|
|
|
+ queryParams, err := binding.ToConcreteDTO[*GetClassQueryParams](dto)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return []domain.Object{
|
|
|
+ &Class{
|
|
|
+ ID: queryParams.ID,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ ServiceFunc: func(c *api.Context, dto binding.DTO, objects []domain.Object) (*ClassInfo, error) {
|
|
|
+ e, err := domain.ToConcreteObject[*Class](objects[0])
|
|
|
+ if err != nil {
|
|
|
+ return &ClassInfo{}, err
|
|
|
+ }
|
|
|
+
|
|
|
+ classInfo := new(ClassInfo)
|
|
|
+ for _, existEntity := range classMap {
|
|
|
+ if existEntity.ID == e.ID {
|
|
|
+ classInfo = &ClassInfo{
|
|
|
+ ID: existEntity.ID,
|
|
|
+ Name: existEntity.Name,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return classInfo, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ err := app.Start()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ err := app.Finish()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
|
|
|
+ _ = death.WaitForDeath()
|
|
|
+}
|