作者 yangfu

直接注册路由

@@ -35,6 +35,6 @@ require ( @@ -35,6 +35,6 @@ require (
35 github.com/yudai/pp v2.0.1+incompatible // indirect 35 github.com/yudai/pp v2.0.1+incompatible // indirect
36 ) 36 )
37 37
38 -//replace github.com/linmadan/egglib-go v0.0.0-20201125083542-25358a549edb => F:\go\src\learn_project\egglib-go 38 +replace github.com/linmadan/egglib-go v0.0.0-20201125083542-25358a549edb => F:\go\src\learn_project\egglib-go
39 39
40 -replace github.com/linmadan/egglib-go v0.0.0-20201125083542-25358a549edb => github.com/tiptok/egglib-go v0.0.0-20210223074108-a6d275d0bbfb 40 +//replace github.com/linmadan/egglib-go v0.0.0-20201125083542-25358a549edb => github.com/tiptok/egglib-go v0.0.0-20210223074108-a6d275d0bbfb
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "github.com/astaxie/beego/context"
4 "github.com/linmadan/egglib-go/web/beego/utils" 5 "github.com/linmadan/egglib-go/web/beego/utils"
5 "github.com/tiptok/godevp/pkg/application/users/command" 6 "github.com/tiptok/godevp/pkg/application/users/command"
6 "github.com/tiptok/godevp/pkg/application/users/query" 7 "github.com/tiptok/godevp/pkg/application/users/query"
@@ -8,53 +9,58 @@ import ( @@ -8,53 +9,58 @@ import (
8 ) 9 )
9 10
10 type UsersController struct { 11 type UsersController struct {
11 - utils.Controller 12 + //utils.Controller
12 } 13 }
13 14
14 -func (controller *UsersController) CreateUsers() { 15 +func (controller *UsersController) CreateUsers(ctx *context.Context) {
  16 + contextController := utils.NewController(ctx)
15 usersService := service.NewUsersService(nil) 17 usersService := service.NewUsersService(nil)
16 createUsersCommand := &command.CreateUsersCommand{} 18 createUsersCommand := &command.CreateUsersCommand{}
17 - controller.Unmarshal(createUsersCommand) 19 + contextController.Unmarshal(createUsersCommand)
18 data, err := usersService.CreateUsers(createUsersCommand) 20 data, err := usersService.CreateUsers(createUsersCommand)
19 - controller.Response(data, err) 21 + contextController.Response(data, err)
20 } 22 }
21 23
22 -func (controller *UsersController) UpdateUsers() { 24 +func (controller *UsersController) UpdateUsers(ctx *context.Context) {
  25 + contextController := utils.NewController(ctx)
23 usersService := service.NewUsersService(nil) 26 usersService := service.NewUsersService(nil)
24 updateUsersCommand := &command.UpdateUsersCommand{} 27 updateUsersCommand := &command.UpdateUsersCommand{}
25 - controller.Unmarshal(updateUsersCommand)  
26 - Id, _ := controller.GetInt64(":Id") 28 + contextController.Unmarshal(updateUsersCommand)
  29 + Id, _ := contextController.GetInt64(":Id")
27 updateUsersCommand.UsersId = Id 30 updateUsersCommand.UsersId = Id
28 data, err := usersService.UpdateUsers(updateUsersCommand) 31 data, err := usersService.UpdateUsers(updateUsersCommand)
29 - controller.Response(data, err) 32 + contextController.Response(data, err)
30 } 33 }
31 34
32 -func (controller *UsersController) GetUsers() {  
33 - usersService := service.NewUsersService(map[string]interface{}{"context": controller.Ctx.Request.Context()}) 35 +func (controller *UsersController) GetUsers(ctx *context.Context) {
  36 + contextController := utils.NewController(ctx)
  37 + usersService := service.NewUsersService(map[string]interface{}{"context": ctx.Request.Context()})
34 getUsersQuery := &query.GetUsersQuery{} 38 getUsersQuery := &query.GetUsersQuery{}
35 - Id, _ := controller.GetInt64(":Id") 39 + Id, _ := contextController.GetInt64(":Id")
36 getUsersQuery.UsersId = Id 40 getUsersQuery.UsersId = Id
37 data, err := usersService.GetUsers(getUsersQuery) 41 data, err := usersService.GetUsers(getUsersQuery)
38 - controller.Response(data, err) 42 + contextController.Response(data, err)
39 } 43 }
40 44
41 -func (controller *UsersController) RemoveUsers() { 45 +func (controller *UsersController) RemoveUsers(ctx *context.Context) {
  46 + contextController := utils.NewController(ctx)
42 usersService := service.NewUsersService(nil) 47 usersService := service.NewUsersService(nil)
43 removeUsersCommand := &command.RemoveUsersCommand{} 48 removeUsersCommand := &command.RemoveUsersCommand{}
44 - controller.Unmarshal(removeUsersCommand)  
45 - Id, _ := controller.GetInt64(":Id") 49 + contextController.Unmarshal(removeUsersCommand)
  50 + Id, _ := contextController.GetInt64(":Id")
46 removeUsersCommand.UsersId = Id 51 removeUsersCommand.UsersId = Id
47 data, err := usersService.RemoveUsers(removeUsersCommand) 52 data, err := usersService.RemoveUsers(removeUsersCommand)
48 - controller.Response(data, err) 53 + contextController.Response(data, err)
49 } 54 }
50 55
51 -func (controller *UsersController) ListUsers() { 56 +func (controller *UsersController) ListUsers(ctx *context.Context) {
  57 + contextController := utils.NewController(ctx)
52 usersService := service.NewUsersService(nil) 58 usersService := service.NewUsersService(nil)
53 listUsersQuery := &query.ListUsersQuery{} 59 listUsersQuery := &query.ListUsersQuery{}
54 - offset, _ := controller.GetInt("offset") 60 + offset, _ := contextController.GetInt("offset")
55 listUsersQuery.Offset = offset 61 listUsersQuery.Offset = offset
56 - limit, _ := controller.GetInt("limit") 62 + limit, _ := contextController.GetInt("limit")
57 listUsersQuery.Limit = limit 63 listUsersQuery.Limit = limit
58 data, err := usersService.ListUsers(listUsersQuery) 64 data, err := usersService.ListUsers(listUsersQuery)
59 - controller.Response(data, err) 65 + contextController.Response(data, err)
60 } 66 }
@@ -6,9 +6,16 @@ import ( @@ -6,9 +6,16 @@ import (
6 ) 6 )
7 7
8 func init() { 8 func init() {
9 - beego.Router("/v1/user/", &controllers.UsersController{}, "Post:CreateUsers")  
10 - beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Put:UpdateUsers")  
11 - beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Get:GetUsers")  
12 - beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Delete:RemoveUsers")  
13 - beego.Router("/v1/user/", &controllers.UsersController{}, "Get:ListUsers") 9 + //beego.Router("/v1/user/", &controllers.UsersController{}, "Post:CreateUsers")
  10 + //beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Put:UpdateUsers")
  11 + //beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Get:GetUsers")
  12 + //beego.Router("/v1/user/:Id", &controllers.UsersController{}, "Delete:RemoveUsers")
  13 + //beego.Router("/v1/user/", &controllers.UsersController{}, "Get:ListUsers")
  14 +
  15 + controller := &controllers.UsersController{}
  16 + beego.Post("/v1/user/", controller.CreateUsers)
  17 + beego.Put("/v1/user/:Id", controller.UpdateUsers)
  18 + beego.Get("/v1/user/:Id", controller.GetUsers)
  19 + beego.Delete("/v1/user/:Id", controller.RemoveUsers)
  20 + beego.Get("/v1/user/", controller.ListUsers)
14 } 21 }