Skip to content

Commit c9dda8d

Browse files
committed
Apply new version
1 parent af98a9e commit c9dda8d

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

configs/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ status:
2929
version_error: 2
3030
validation_error: 4
3131
error: 4
32+
33+
action:
34+
create: create
35+
update: update
36+
patch: patch
37+
delete: delete

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ require (
77
github.com/core-go/health v0.4.7
88
github.com/core-go/log v0.0.7
99
github.com/core-go/mongo v0.2.8
10-
github.com/core-go/search v0.2.1
11-
github.com/core-go/service v0.1.5
10+
github.com/core-go/search v0.2.2
11+
github.com/core-go/service v0.1.6
1212
github.com/core-go/sql v0.2.27
1313
github.com/go-playground/validator/v10 v10.9.0 // indirect
1414
github.com/go-sql-driver/mysql v1.6.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ github.com/core-go/log v0.0.7 h1:81MypmjviFMHGabaHFy7CL2Neh5BbW+EuQtWRB6DBG8=
3434
github.com/core-go/log v0.0.7/go.mod h1:nLtakp+tnvjYestj7RVMXHnZ3GMfMpeZvl9Gzo7xz6o=
3535
github.com/core-go/mongo v0.2.8 h1:IDmhSD3/DQrnYB7WajRCe2hw9NvyWikm5gvGo1WdAnc=
3636
github.com/core-go/mongo v0.2.8/go.mod h1:ujZ+OILNHPNhPMUOtxu5K/HC/VK+O3gilL367B/eONk=
37-
github.com/core-go/search v0.2.1 h1:QzGRCyk2PXlOSP+cvxiE5wc4uvZ0OJDJyF13urUba+Y=
38-
github.com/core-go/search v0.2.1/go.mod h1:wgb0haY0xDvg1msrqkr3RTtYH6zXYANprVpWZUbRLRM=
39-
github.com/core-go/service v0.1.5 h1:+OQ0PNXnWXwtniNbZjtdidCu5TPkLg4nrZMGd97OmC0=
40-
github.com/core-go/service v0.1.5/go.mod h1:LUwlp3n/9D6/w5dHoUtcndhDA73Z6dZeB7esgD0hcKs=
37+
github.com/core-go/search v0.2.2 h1:i1mWQYOU7oRFqbHDHtPBDD8HQm5XfAY9j40Eb0g+6jA=
38+
github.com/core-go/search v0.2.2/go.mod h1:wgb0haY0xDvg1msrqkr3RTtYH6zXYANprVpWZUbRLRM=
39+
github.com/core-go/service v0.1.6 h1:lGIUg8fwNiWlP3xba6yzoLmgsQ/tQjn5FrKQt7N3n1w=
40+
github.com/core-go/service v0.1.6/go.mod h1:LUwlp3n/9D6/w5dHoUtcndhDA73Z6dZeB7esgD0hcKs=
4141
github.com/core-go/sql v0.2.27 h1:t71RNQQy+GFqtV6JmMQ36cfTxpyYczuyTH7gKeGWdWM=
4242
github.com/core-go/sql v0.2.27/go.mod h1:8hRTaaPVqey2TKPhKQyG/Gc1MVB5o2WcsslleHqPAGw=
4343
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=

internal/app/app.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ func NewApp(ctx context.Context, root Root) (*ApplicationContext, error) {
3333
}
3434
logError := log.ErrorMsg
3535
status := sv.InitializeStatus(root.Status)
36+
action := sv.InitializeAction(root.Action)
3637
validator := v.NewValidator()
3738

3839
var userRepository sv.Repository
39-
var searchUser func(context.Context, interface{}, interface{}, int64,...int64) (int64, string, error)
40+
var searchUser func(context.Context, interface{}, interface{}, int64, ...int64) (int64, string, error)
4041
userType := reflect.TypeOf(User{})
4142
if root.Provider != "mongo" {
4243
userQueryBuilder := query.NewBuilder(db, "users", userType)
@@ -56,7 +57,7 @@ func NewApp(ctx context.Context, root Root) (*ApplicationContext, error) {
5657
userRepository = mgo.NewRepository(mongoDb, "users", userType)
5758
}
5859
userService := NewUserService(userRepository)
59-
userHandler := NewUserHandler(searchUser, userService, status, validator.Validate, logError)
60+
userHandler := NewUserHandler(searchUser, userService, status, logError, validator.Validate, &action)
6061

6162
sqlChecker := q.NewHealthChecker(db)
6263
mongoChecker := mgo.NewHealthChecker(mongoDb)

internal/app/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ type Root struct {
1616
Log log.Config `mapstructure:"log"`
1717
MiddleWare mid.LogConfig `mapstructure:"middleware"`
1818
Status *sv.StatusConfig `mapstructure:"status"`
19+
Action *sv.ActionConfig `mapstructure:"action"`
1920
}

internal/usecase/user/user_handler.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@ type UserHandler interface {
1717
Delete(w http.ResponseWriter, r *http.Request)
1818
}
1919

20-
func NewUserHandler(find func(context.Context, interface{}, interface{}, int64, ...int64) (int64, string, error), service UserService, status sv.StatusConfig, validate func(ctx context.Context, model interface{}) ([]sv.ErrorMessage, error), logError func(context.Context, string)) UserHandler {
20+
func NewUserHandler(find func(context.Context, interface{}, interface{}, int64, ...int64) (int64, string, error), service UserService, status sv.StatusConfig, logError func(context.Context, string), validate func(ctx context.Context, model interface{}) ([]sv.ErrorMessage, error), action *sv.ActionConfig) UserHandler {
2121
searchModelType := reflect.TypeOf(UserFilter{})
2222
modelType := reflect.TypeOf(User{})
23-
keys, indexes, _ := sv.BuildHandlerParams(modelType)
24-
searchHandler := search.NewJSONSearchHandler(find, modelType, searchModelType, logError, nil)
25-
return &userHandler{SearchHandler: searchHandler, service: service, keys: keys, indexes: indexes, modelType: modelType, Status: status, Validate: validate, Error: logError}
23+
params := sv.CreateParams(modelType, &status, logError, validate, action)
24+
searchHandler := search.NewSearchHandler(find, modelType, searchModelType, logError, params.Log)
25+
return &userHandler{service: service, SearchHandler: searchHandler, Params: params}
2626
}
2727

2828
type userHandler struct {
29+
service UserService
2930
*search.SearchHandler
30-
service UserService
31-
keys []string
32-
indexes map[string]int
33-
modelType reflect.Type
34-
Status sv.StatusConfig
35-
Validate func(ctx context.Context, model interface{}) ([]sv.ErrorMessage, error)
36-
Error func(context.Context, string)
31+
*sv.Params
3732
}
3833

3934
func (h *userHandler) Load(w http.ResponseWriter, r *http.Request) {
@@ -48,38 +43,38 @@ func (h *userHandler) Create(w http.ResponseWriter, r *http.Request) {
4843
er1 := sv.Decode(w, r, &user)
4944
if er1 == nil {
5045
errors, er2 := h.Validate(r.Context(), &user)
51-
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, nil) {
46+
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, h.Log, h.Resource, h.Action.Create) {
5247
result, er3 := h.service.Create(r.Context(), &user)
53-
sv.AfterCreated(w, r, &user, result, er3, h.Status, h.Error, nil)
48+
sv.AfterCreated(w, r, &user, result, er3, h.Status, h.Error, h.Log, h.Resource, h.Action.Create)
5449
}
5550
}
5651
}
5752
func (h *userHandler) Update(w http.ResponseWriter, r *http.Request) {
5853
var user User
59-
er1 := sv.DecodeAndCheckId(w, r, &user, h.keys, h.indexes)
54+
er1 := sv.DecodeAndCheckId(w, r, &user, h.Keys, h.Indexes)
6055
if er1 == nil {
6156
errors, er2 := h.Validate(r.Context(), &user)
62-
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, nil) {
57+
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, h.Log, h.Resource, h.Action.Update) {
6358
result, er3 := h.service.Update(r.Context(), &user)
64-
sv.HandleResult(w, r, &user, result, er3, h.Status, h.Error, nil)
59+
sv.HandleResult(w, r, &user, result, er3, h.Status, h.Error, h.Log, h.Resource, h.Action.Update)
6560
}
6661
}
6762
}
6863
func (h *userHandler) Patch(w http.ResponseWriter, r *http.Request) {
6964
var user User
70-
r, json, er1 := sv.BuildMapAndCheckId(w, r, &user, h.keys, h.indexes)
65+
r, json, er1 := sv.BuildMapAndCheckId(w, r, &user, h.Keys, h.Indexes)
7166
if er1 == nil {
7267
errors, er2 := h.Validate(r.Context(), &user)
73-
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, nil) {
68+
if !sv.HasError(w, r, errors, er2, *h.Status.ValidationError, h.Error, h.Log, h.Resource, h.Action.Patch) {
7469
result, er3 := h.service.Patch(r.Context(), json)
75-
sv.HandleResult(w, r, json, result, er3, h.Status, h.Error, nil)
70+
sv.HandleResult(w, r, json, result, er3, h.Status, h.Error, h.Log, h.Resource, h.Action.Patch)
7671
}
7772
}
7873
}
7974
func (h *userHandler) Delete(w http.ResponseWriter, r *http.Request) {
8075
id := sv.GetRequiredParam(w, r)
8176
if len(id) > 0 {
8277
result, err := h.service.Delete(r.Context(), id)
83-
sv.HandleDelete(w, r, result, err, h.Error, nil)
78+
sv.HandleDelete(w, r, result, err, h.Error, h.Log, h.Resource, h.Action.Delete)
8479
}
8580
}

0 commit comments

Comments
 (0)