Skip to content

Commit

Permalink
refactor: change Handler to Controller, remove store from Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
colin404 committed Jul 8, 2021
1 parent f1d5fed commit 2289615
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 174 deletions.
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/policy/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// Create creates a new ladon policy.
// It will convert the policy to string and store it in the storage.
func (p *PolicyHandler) Create(c *gin.Context) {
func (p *PolicyController) Create(c *gin.Context) {
log.L(c).Info("create policy function called.")

var r v1.Policy
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/policy/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Delete deletes the policy by the policy identifier.
func (p *PolicyHandler) Delete(c *gin.Context) {
func (p *PolicyController) Delete(c *gin.Context) {
log.L(c).Info("delete policy function called.")

if err := p.srv.Policies().Delete(c, c.GetString(middleware.UsernameKey), c.Param("name"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// DeleteCollection delete policies by policy names.
func (p *PolicyHandler) DeleteCollection(c *gin.Context) {
func (p *PolicyController) DeleteCollection(c *gin.Context) {
log.L(c).Info("batch delete policy function called.")

if err := p.srv.Policies().DeleteCollection(c, c.GetString(middleware.UsernameKey),
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/policy/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Get return policy by the policy identifier.
func (p *PolicyHandler) Get(c *gin.Context) {
func (p *PolicyController) Get(c *gin.Context) {
log.L(c).Info("get policy function called.")

pol, err := p.srv.Policies().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{})
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/policy/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// List return all policies.
func (p *PolicyHandler) List(c *gin.Context) {
func (p *PolicyController) List(c *gin.Context) {
log.L(c).Info("list policy function called.")

var r metav1.ListOptions
Expand Down
16 changes: 7 additions & 9 deletions internal/apiserver/controller/v1/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (
"github.com/marmotedu/iam/internal/apiserver/store"
)

// PolicyHandler create a policy handler used to handle request for policy resource.
type PolicyHandler struct {
srv srvv1.Service
store store.Factory
// PolicyController create a policy handler used to handle request for policy resource.
type PolicyController struct {
srv srvv1.Service
}

// NewPolicyHandler creates a policy handler.
func NewPolicyHandler(store store.Factory) *PolicyHandler {
return &PolicyHandler{
srv: srvv1.NewService(store),
store: store,
// NewPolicyController creates a policy handler.
func NewPolicyController(store store.Factory) *PolicyController {
return &PolicyController{
srv: srvv1.NewService(store),
}
}
6 changes: 3 additions & 3 deletions internal/apiserver/controller/v1/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// Update updates policy by the policy identifier.
func (p *PolicyHandler) Update(c *gin.Context) {
func (p *PolicyController) Update(c *gin.Context) {
log.L(c).Info("update policy function called.")

var r v1.Policy
Expand All @@ -27,9 +27,9 @@ func (p *PolicyHandler) Update(c *gin.Context) {
return
}

pol, err := p.store.Policies().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{})
pol, err := p.srv.Policies().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{})
if err != nil {
core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil)
core.WriteResponse(c, err, nil)

return
}
Expand Down
10 changes: 5 additions & 5 deletions internal/apiserver/controller/v1/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
const maxSecretCount = 10

// Create add new secret key pairs to the storage.
func (s *SecretHandler) Create(c *gin.Context) {
func (s *SecretController) Create(c *gin.Context) {
log.L(c).Info("create secret function called.")

var r v1.Secret
Expand All @@ -39,18 +39,18 @@ func (s *SecretHandler) Create(c *gin.Context) {

username := c.GetString(middleware.UsernameKey)

sec, err := s.store.Secrets().List(c, username, metav1.ListOptions{
secrets, err := s.srv.Secrets().List(c, username, metav1.ListOptions{
Offset: pointer.ToInt64(0),
Limit: pointer.ToInt64(-1),
})
if err != nil {
core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil)
core.WriteResponse(c, err, nil)

return
}

if sec.TotalCount >= maxSecretCount {
core.WriteResponse(c, errors.WithCode(code.ErrReachMaxCount, "secret count: %d", sec.TotalCount), nil)
if secrets.TotalCount >= maxSecretCount {
core.WriteResponse(c, errors.WithCode(code.ErrReachMaxCount, "secret count: %d", secrets.TotalCount), nil)

return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/secret/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Delete delete a secret by the secret identifier.
func (s *SecretHandler) Delete(c *gin.Context) {
func (s *SecretController) Delete(c *gin.Context) {
log.L(c).Info("delete secret function called.")
opts := metav1.DeleteOptions{Unscoped: true}
if err := s.srv.Secrets().Delete(c, c.GetString(middleware.UsernameKey), c.Param("name"), opts); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// DeleteCollection delete secrets by secret names.
func (s *SecretHandler) DeleteCollection(c *gin.Context) {
func (s *SecretController) DeleteCollection(c *gin.Context) {
log.L(c).Info("batch delete policy function called.")

if err := s.srv.Policies().DeleteCollection(
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/secret/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Get get an policy by the secret identifier.
func (s *SecretHandler) Get(c *gin.Context) {
func (s *SecretController) Get(c *gin.Context) {
log.L(c).Info("get secret function called.")

secret, err := s.srv.Secrets().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{})
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// List list all the secrets.
func (s *SecretHandler) List(c *gin.Context) {
func (s *SecretController) List(c *gin.Context) {
log.L(c).Info("list secret function called.")
var r metav1.ListOptions
if err := c.ShouldBindQuery(&r); err != nil {
Expand Down
16 changes: 7 additions & 9 deletions internal/apiserver/controller/v1/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (
"github.com/marmotedu/iam/internal/apiserver/store"
)

// SecretHandler create a secret handler used to handle request for secret resource.
type SecretHandler struct {
srv srvv1.Service
store store.Factory
// SecretController create a secret handler used to handle request for secret resource.
type SecretController struct {
srv srvv1.Service
}

// NewSecretHandler creates a secret handler.
func NewSecretHandler(store store.Factory) *SecretHandler {
return &SecretHandler{
srv: srvv1.NewService(store),
store: store,
// NewSecretController creates a secret handler.
func NewSecretController(store store.Factory) *SecretController {
return &SecretController{
srv: srvv1.NewService(store),
}
}
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// Update update a key by the secret key identifier.
func (s *SecretHandler) Update(c *gin.Context) {
func (s *SecretController) Update(c *gin.Context) {
log.L(c).Info("update secret function called.")

var r v1.Secret
Expand Down
6 changes: 3 additions & 3 deletions internal/apiserver/controller/v1/user/change_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ChangePasswordRequest struct {
}

// ChangePassword change the user's password by the user identifier.
func (u *UserHandler) ChangePassword(c *gin.Context) {
func (u *UserController) ChangePassword(c *gin.Context) {
log.L(c).Info("change password function called.")

var r ChangePasswordRequest
Expand All @@ -37,9 +37,9 @@ func (u *UserHandler) ChangePassword(c *gin.Context) {
return
}

user, err := u.store.Users().Get(c, c.Param("name"), metav1.GetOptions{})
user, err := u.srv.Users().Get(c, c.Param("name"), metav1.GetOptions{})
if err != nil {
core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil)
core.WriteResponse(c, err, nil)

return
}
Expand Down
21 changes: 7 additions & 14 deletions internal/apiserver/controller/v1/user/change_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ import (
v1 "github.com/marmotedu/api/apiserver/v1"

srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1"
"github.com/marmotedu/iam/internal/apiserver/store"
_ "github.com/marmotedu/iam/pkg/validator"
)

func TestUserHandler_ChangePassword(t *testing.T) {
func TestUserController_ChangePassword(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

user := &v1.User{
Password: "$2a$10$KqZhl5WStpa2K.ddEyzyf.zXllEXP4gIG8xQUgMhU1ZvMUn/Ta5um",
}
mockFactory := store.NewMockFactory(ctrl)
mockUserStore := store.NewMockUserStore(ctrl)
mockUserStore.EXPECT().Get(gomock.Any(), gomock.Eq("colin"), gomock.Any()).Return(user, nil)
mockFactory.EXPECT().Users().Return(mockUserStore)

mockService := srvv1.NewMockService(ctrl)
mockUserSrv := srvv1.NewMockUserSrv(ctrl)
mockUserSrv.EXPECT().Get(gomock.Any(), gomock.Eq("colin"), gomock.Any()).Return(user, nil)
mockUserSrv.EXPECT().ChangePassword(gomock.Any(), gomock.Any()).Return(nil)
mockService.EXPECT().Users().Return(mockUserSrv)
mockService.EXPECT().Users().Return(mockUserSrv).Times(2)

c, _ := gin.CreateTestContext(httptest.NewRecorder())
body := bytes.NewBufferString(`{"oldPassword":"Admin@2020","newPassword":"Colin@2021"}`)
Expand All @@ -43,8 +39,7 @@ func TestUserHandler_ChangePassword(t *testing.T) {
c.Request.Header.Set("Content-Type", "application/json")

type fields struct {
srv srvv1.Service
store store.Factory
srv srvv1.Service
}
type args struct {
c *gin.Context
Expand All @@ -57,8 +52,7 @@ func TestUserHandler_ChangePassword(t *testing.T) {
{
name: "default",
fields: fields{
srv: mockService,
store: mockFactory,
srv: mockService,
},
args: args{
c: c,
Expand All @@ -67,9 +61,8 @@ func TestUserHandler_ChangePassword(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserHandler{
srv: tt.fields.srv,
store: tt.fields.store,
u := &UserController{
srv: tt.fields.srv,
}
u.ChangePassword(tt.args.c)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// Create add new user to the storage.
func (u *UserHandler) Create(c *gin.Context) {
func (u *UserController) Create(c *gin.Context) {
log.L(c).Info("user create function called.")

var r v1.User
Expand Down
15 changes: 5 additions & 10 deletions internal/apiserver/controller/v1/user/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ import (
"github.com/golang/mock/gomock"

srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1"
"github.com/marmotedu/iam/internal/apiserver/store"
)

func TestUserHandler_Create(t *testing.T) {
func TestUserController_Create(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := srvv1.NewMockService(ctrl)
mockUserSrv := srvv1.NewMockUserSrv(ctrl)
mockUserSrv.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockService.EXPECT().Users().Return(mockUserSrv)
mockFactory := store.NewMockFactory(ctrl)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
body := bytes.NewBufferString(
`{"metadata":{"name":"admin"},"nickname":"admin","email":"[email protected]","password":"Admin@2020","phone":"1812884xxx"}`,
Expand All @@ -34,8 +32,7 @@ func TestUserHandler_Create(t *testing.T) {
c.Request.Header.Set("Content-Type", "application/json")

type fields struct {
srv srvv1.Service
store store.Factory
srv srvv1.Service
}
type args struct {
c *gin.Context
Expand All @@ -48,8 +45,7 @@ func TestUserHandler_Create(t *testing.T) {
{
name: "default",
fields: fields{
srv: mockService,
store: mockFactory,
srv: mockService,
},
args: args{
c: c,
Expand All @@ -58,9 +54,8 @@ func TestUserHandler_Create(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserHandler{
srv: tt.fields.srv,
store: tt.fields.store,
u := &UserController{
srv: tt.fields.srv,
}
u.Create(tt.args.c)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Delete delete an user by the user identifier.
// Only administrator can call this function.
func (u *UserHandler) Delete(c *gin.Context) {
func (u *UserController) Delete(c *gin.Context) {
log.L(c).Info("delete user function called.")

if err := u.srv.Users().Delete(c, c.Param("name"), metav1.DeleteOptions{Unscoped: true}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/controller/v1/user/delete_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// DeleteCollection batch delete users by multiple usernames.
// Only administrator can call this function.
func (u *UserHandler) DeleteCollection(c *gin.Context) {
func (u *UserController) DeleteCollection(c *gin.Context) {
log.L(c).Info("batch delete user function called.")

usernames := c.QueryArray("name")
Expand Down
Loading

0 comments on commit 2289615

Please sign in to comment.