作者 yangfu

控制器优化

@@ -7,5 +7,6 @@ import ( @@ -7,5 +7,6 @@ import (
7 ) 7 )
8 8
9 func main() { 9 func main() {
  10 + beego.BConfig.CopyRequestBody = true
10 beego.Run() 11 beego.Run()
11 } 12 }
1 package service 1 package service
2 2
3 import ( 3 import (
  4 + "context"
4 "fmt" 5 "fmt"
5 "github.com/linmadan/egglib-go/core/application" 6 "github.com/linmadan/egglib-go/core/application"
6 "github.com/linmadan/egglib-go/utils/tool_funs" 7 "github.com/linmadan/egglib-go/utils/tool_funs"
@@ -11,6 +12,7 @@ import ( @@ -11,6 +12,7 @@ import (
11 ) 12 )
12 13
13 type UsersService struct { 14 type UsersService struct {
  15 + ctx context.Context
14 } 16 }
15 17
16 // 创建 18 // 创建
@@ -208,5 +210,8 @@ func (usersService *UsersService) UpdateUsers(updateUsersCommand *command.Update @@ -208,5 +210,8 @@ func (usersService *UsersService) UpdateUsers(updateUsersCommand *command.Update
208 210
209 func NewUsersService(options map[string]interface{}) *UsersService { 211 func NewUsersService(options map[string]interface{}) *UsersService {
210 newUsersService := &UsersService{} 212 newUsersService := &UsersService{}
  213 + if ctx, ok := options["context"]; ok {
  214 + newUsersService.ctx = ctx.(context.Context)
  215 + }
211 return newUsersService 216 return newUsersService
212 } 217 }
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 ) 9 )
10 10
11 func init() { 11 func init() {
12 - beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter()) 12 + //beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter())
13 beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger)) 13 beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger))
14 beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false) 14 beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false)
15 } 15 }
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
4 - "encoding/json"  
5 -  
6 - "github.com/astaxie/beego"  
7 "github.com/linmadan/egglib-go/web/beego/utils" 4 "github.com/linmadan/egglib-go/web/beego/utils"
8 "github.com/tiptok/godevp/pkg/application/users/command" 5 "github.com/tiptok/godevp/pkg/application/users/command"
9 "github.com/tiptok/godevp/pkg/application/users/query" 6 "github.com/tiptok/godevp/pkg/application/users/query"
@@ -11,72 +8,44 @@ import ( @@ -11,72 +8,44 @@ import (
11 ) 8 )
12 9
13 type UsersController struct { 10 type UsersController struct {
14 - beego.Controller 11 + utils.Controller
15 } 12 }
16 13
17 func (controller *UsersController) CreateUsers() { 14 func (controller *UsersController) CreateUsers() {
18 usersService := service.NewUsersService(nil) 15 usersService := service.NewUsersService(nil)
19 createUsersCommand := &command.CreateUsersCommand{} 16 createUsersCommand := &command.CreateUsersCommand{}
20 - json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createUsersCommand) 17 + controller.Unmarshal(createUsersCommand)
21 data, err := usersService.CreateUsers(createUsersCommand) 18 data, err := usersService.CreateUsers(createUsersCommand)
22 - var response utils.JsonResponse  
23 - if err != nil {  
24 - response = utils.ResponseError(controller.Ctx, err)  
25 - } else {  
26 - response = utils.ResponseData(controller.Ctx, data)  
27 - }  
28 - controller.Data["json"] = response  
29 - controller.ServeJSON() 19 + controller.Response(data, err)
30 } 20 }
31 21
32 func (controller *UsersController) UpdateUsers() { 22 func (controller *UsersController) UpdateUsers() {
33 usersService := service.NewUsersService(nil) 23 usersService := service.NewUsersService(nil)
34 updateUsersCommand := &command.UpdateUsersCommand{} 24 updateUsersCommand := &command.UpdateUsersCommand{}
35 - json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateUsersCommand) 25 + controller.Unmarshal(updateUsersCommand)
36 Id, _ := controller.GetInt64(":Id") 26 Id, _ := controller.GetInt64(":Id")
37 updateUsersCommand.UsersId = Id 27 updateUsersCommand.UsersId = Id
38 data, err := usersService.UpdateUsers(updateUsersCommand) 28 data, err := usersService.UpdateUsers(updateUsersCommand)
39 - var response utils.JsonResponse  
40 - if err != nil {  
41 - response = utils.ResponseError(controller.Ctx, err)  
42 - } else {  
43 - response = utils.ResponseData(controller.Ctx, data)  
44 - }  
45 - controller.Data["json"] = response  
46 - controller.ServeJSON() 29 + controller.Response(data, err)
47 } 30 }
48 31
49 func (controller *UsersController) GetUsers() { 32 func (controller *UsersController) GetUsers() {
50 - usersService := service.NewUsersService(nil) 33 + usersService := service.NewUsersService(map[string]interface{}{"context": controller.Ctx.Request.Context()})
51 getUsersQuery := &query.GetUsersQuery{} 34 getUsersQuery := &query.GetUsersQuery{}
52 Id, _ := controller.GetInt64(":Id") 35 Id, _ := controller.GetInt64(":Id")
53 getUsersQuery.UsersId = Id 36 getUsersQuery.UsersId = Id
54 data, err := usersService.GetUsers(getUsersQuery) 37 data, err := usersService.GetUsers(getUsersQuery)
55 - var response utils.JsonResponse  
56 - if err != nil {  
57 - response = utils.ResponseError(controller.Ctx, err)  
58 - } else {  
59 - response = utils.ResponseData(controller.Ctx, data)  
60 - }  
61 - controller.Data["json"] = response  
62 - controller.ServeJSON() 38 + controller.Response(data, err)
63 } 39 }
64 40
65 func (controller *UsersController) RemoveUsers() { 41 func (controller *UsersController) RemoveUsers() {
66 usersService := service.NewUsersService(nil) 42 usersService := service.NewUsersService(nil)
67 removeUsersCommand := &command.RemoveUsersCommand{} 43 removeUsersCommand := &command.RemoveUsersCommand{}
68 - json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), removeUsersCommand) 44 + controller.Unmarshal(removeUsersCommand)
69 Id, _ := controller.GetInt64(":Id") 45 Id, _ := controller.GetInt64(":Id")
70 removeUsersCommand.UsersId = Id 46 removeUsersCommand.UsersId = Id
71 data, err := usersService.RemoveUsers(removeUsersCommand) 47 data, err := usersService.RemoveUsers(removeUsersCommand)
72 - var response utils.JsonResponse  
73 - if err != nil {  
74 - response = utils.ResponseError(controller.Ctx, err)  
75 - } else {  
76 - response = utils.ResponseData(controller.Ctx, data)  
77 - }  
78 - controller.Data["json"] = response  
79 - controller.ServeJSON() 48 + controller.Response(data, err)
80 } 49 }
81 50
82 func (controller *UsersController) ListUsers() { 51 func (controller *UsersController) ListUsers() {
@@ -87,12 +56,5 @@ func (controller *UsersController) ListUsers() { @@ -87,12 +56,5 @@ func (controller *UsersController) ListUsers() {
87 limit, _ := controller.GetInt("limit") 56 limit, _ := controller.GetInt("limit")
88 listUsersQuery.Limit = limit 57 listUsersQuery.Limit = limit
89 data, err := usersService.ListUsers(listUsersQuery) 58 data, err := usersService.ListUsers(listUsersQuery)
90 - var response utils.JsonResponse  
91 - if err != nil {  
92 - response = utils.ResponseError(controller.Ctx, err)  
93 - } else {  
94 - response = utils.ResponseData(controller.Ctx, data)  
95 - }  
96 - controller.Data["json"] = response  
97 - controller.ServeJSON() 59 + controller.Response(data, err)
98 } 60 }