Skip to content

Commit c26bbc5

Browse files
committed
Refactor code
1 parent 969b0da commit c26bbc5

37 files changed

+386
-409
lines changed

approvers/approvers.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package approvers
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
)
7+
8+
type ApproversRepository interface {
9+
GetApprovers(ctx context.Context, id string) ([]string, error)
10+
}
11+
type ApproversPort interface {
12+
GetApprovers(ctx context.Context, id string) ([]string, error)
13+
}
14+
15+
func NewApproversAdapter(db *sql.DB, query string) *ApproversAdapter{
16+
return &ApproversAdapter{DB: db, Query: query}
17+
}
18+
type ApproversAdapter struct {
19+
DB *sql.DB
20+
Query string
21+
}
22+
23+
func (a *ApproversAdapter) GetApprovers(ctx context.Context, id string) ([]string, error) {
24+
var ids []string
25+
rows, err := a.DB.QueryContext(ctx, a.Query, id)
26+
defer rows.Close()
27+
28+
for rows.Next() {
29+
var s string
30+
if err := rows.Scan(&s); err != nil {
31+
return nil, err
32+
}
33+
ids = append(ids, s)
34+
}
35+
if err = rows.Err(); err != nil {
36+
return nil, err
37+
}
38+
return ids, nil
39+
}

avro/marshaller.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

compare/compare.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package compare
2+
3+
func Contains[T comparable](elems []T, v T) bool {
4+
for _, s := range elems {
5+
if v == s {
6+
return true
7+
}
8+
}
9+
return false
10+
}

echo/handler.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,26 @@ const (
1414
Update = "update"
1515
Patch = "patch"
1616
)
17-
func CreatePatchAndParams(modelType reflect.Type, status *sv.StatusConfig, logError func(context.Context, string, ...map[string]interface{}), patch func(context.Context, map[string]interface{}) (int64, error), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), build func(context.Context, interface{}) (interface{}, error), action *sv.ActionConfig, options ...func(context.Context, string, string, bool, string) error) (*PatchHandler, *sv.Params) {
17+
func CreatePatchAndParams(modelType reflect.Type, logError func(context.Context, string, ...map[string]interface{}), patch func(context.Context, map[string]interface{}) (int64, error), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), build func(context.Context, interface{}) (interface{}, error), action *sv.ActionConfig, options ...func(context.Context, string, string, bool, string) error) (*PatchHandler, *sv.Params) {
1818
var writeLog func(context.Context, string, string, bool, string) error
1919
if len(options) > 0 {
2020
writeLog = options[0]
2121
}
22-
s := sv.InitializeStatus(status)
2322
a := sv.InitializeAction(action)
2423
resource := sv.BuildResourceName(modelType.Name())
2524
keys, indexes, _ := sv.BuildMapField(modelType)
26-
patchHandler := &PatchHandler{PrimaryKeys: keys, FieldIndexes: indexes, ObjectType: modelType, StatusConf: s, Save: patch, ValidateData: validate, Build: build, LogError: logError, WriteLog: writeLog, ResourceType: resource, Activity: a.Patch}
27-
params := &sv.Params{Keys: keys, Indexes: indexes, ModelType: modelType, Status: s, Resource: resource, Action: a, Error: logError, Log: writeLog, Validate: validate}
25+
patchHandler := &PatchHandler{PrimaryKeys: keys, FieldIndexes: indexes, ObjectType: modelType, Save: patch, ValidateData: validate, Build: build, LogError: logError, WriteLog: writeLog, ResourceType: resource, Activity: a.Patch}
26+
params := &sv.Params{Keys: keys, Indexes: indexes, ModelType: modelType, Resource: resource, Action: a, Error: logError, Log: writeLog, Validate: validate}
2827
return patchHandler, params
2928
}
30-
func NewPatchHandler(patch func(context.Context, map[string]interface{}) (int64, error), modelType reflect.Type, status *sv.StatusConfig, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), build func(context.Context, interface{}) (interface{}, error), action string, options ...func(context.Context, string, string, bool, string) error) *PatchHandler {
29+
func NewPatchHandler(patch func(context.Context, map[string]interface{}) (int64, error), modelType reflect.Type, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), build func(context.Context, interface{}) (interface{}, error), action string, options ...func(context.Context, string, string, bool, string) error) *PatchHandler {
3130
var writeLog func(context.Context, string, string, bool, string) error
3231
if len(options) > 0 {
3332
writeLog = options[0]
3433
}
35-
s := sv.InitializeStatus(status)
3634
resource := sv.BuildResourceName(modelType.Name())
3735
keys, indexes, _ := sv.BuildMapField(modelType)
38-
return &PatchHandler{PrimaryKeys: keys, FieldIndexes: indexes, ObjectType: modelType, StatusConf: s, Save: patch, ValidateData: validate, Build: build, LogError: logError, WriteLog: writeLog, ResourceType: resource, Activity: action}
36+
return &PatchHandler{PrimaryKeys: keys, FieldIndexes: indexes, ObjectType: modelType, Save: patch, ValidateData: validate, Build: build, LogError: logError, WriteLog: writeLog, ResourceType: resource, Activity: action}
3937
}
4038

4139
type PatchHandler struct {
@@ -49,7 +47,6 @@ type PatchHandler struct {
4947
WriteLog func(context.Context, string, string, bool, string) error
5048
ResourceType string
5149
Activity string
52-
StatusConf sv.StatusConfig
5350
}
5451
func (h *PatchHandler) Patch(ctx echo.Context) error {
5552
r := ctx.Request()
@@ -70,7 +67,7 @@ func (h *PatchHandler) Patch(ctx echo.Context) error {
7067
}
7168
if h.ValidateData != nil {
7269
errors, er3 := h.ValidateData(r.Context(), &bodyStruct)
73-
if HasError(ctx, errors, er3, *h.StatusConf.ValidationError, h.LogError, h.WriteLog, h.ResourceType, h.Activity) {
70+
if HasError(ctx, errors, er3, h.LogError, h.WriteLog, h.ResourceType, h.Activity) {
7471
return er3
7572
}
7673
}
@@ -80,7 +77,6 @@ func (h *PatchHandler) Patch(ctx echo.Context) error {
8077

8178
type GenericHandler struct {
8279
*LoadHandler
83-
Status sv.StatusConfig
8480
Action sv.ActionConfig
8581
service sv.SimpleService
8682
builder sv.Builder
@@ -90,26 +86,26 @@ type GenericHandler struct {
9086
}
9187

9288
func NewHandler(genericService sv.SimpleService, modelType reflect.Type, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), options ...func(context.Context, string, string, bool, string) error) *GenericHandler {
93-
return NewHandlerWithConfig(genericService, modelType, nil, modelBuilder, logError, validate, options...)
89+
return NewHandlerWithConfig(genericService, modelType, modelBuilder, logError, validate, options...)
9490
}
9591
func NewHandlerWithKeys(genericService sv.SimpleService, keys []string, modelType reflect.Type, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), options ...func(context.Context, string, string, bool, string) error) *GenericHandler {
9692
var writeLog func(context.Context, string, string, bool, string) error
9793
if len(options) > 0 {
9894
writeLog = options[0]
9995
}
100-
return NewHandlerWithKeysAndLog(genericService, keys, modelType, nil, modelBuilder, logError, validate, writeLog, "", nil)
96+
return NewHandlerWithKeysAndLog(genericService, keys, modelType, modelBuilder, logError, validate, writeLog, "", nil)
10197
}
102-
func NewHandlerWithConfig(genericService sv.SimpleService, modelType reflect.Type, status *sv.StatusConfig, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), options ...func(context.Context, string, string, bool, string) error) *GenericHandler {
98+
func NewHandlerWithConfig(genericService sv.SimpleService, modelType reflect.Type, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), options ...func(context.Context, string, string, bool, string) error) *GenericHandler {
10399
var writeLog func(context.Context, string, string, bool, string) error
104100
if len(options) > 0 && options[0] != nil {
105101
writeLog = options[0]
106102
}
107-
return NewHandlerWithKeysAndLog(genericService, nil, modelType, status, modelBuilder, logError, validate, writeLog, "", nil)
103+
return NewHandlerWithKeysAndLog(genericService, nil, modelType, modelBuilder, logError, validate, writeLog, "", nil)
108104
}
109-
func NewHandlerWithLog(genericService sv.SimpleService, modelType reflect.Type, status *sv.StatusConfig, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), writeLog func(context.Context, string, string, bool, string) error, resource string, conf *sv.ActionConfig) *GenericHandler {
110-
return NewHandlerWithKeysAndLog(genericService, nil, modelType, status, modelBuilder, logError, validate, writeLog, resource, conf)
105+
func NewHandlerWithLog(genericService sv.SimpleService, modelType reflect.Type, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), writeLog func(context.Context, string, string, bool, string) error, resource string, conf *sv.ActionConfig) *GenericHandler {
106+
return NewHandlerWithKeysAndLog(genericService, nil, modelType, modelBuilder, logError, validate, writeLog, resource, conf)
111107
}
112-
func NewHandlerWithKeysAndLog(genericService sv.SimpleService, keys []string, modelType reflect.Type, status *sv.StatusConfig, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), writeLog func(context.Context, string, string, bool, string) error, resource string, conf *sv.ActionConfig) *GenericHandler {
108+
func NewHandlerWithKeysAndLog(genericService sv.SimpleService, keys []string, modelType reflect.Type, modelBuilder sv.Builder, logError func(context.Context, string, ...map[string]interface{}), validate func(context.Context, interface{}) ([]sv.ErrorMessage, error), writeLog func(context.Context, string, string, bool, string) error, resource string, conf *sv.ActionConfig) *GenericHandler {
113109
if keys == nil || len(keys) == 0 {
114110
keys = sv.GetJsonPrimaryKeys(modelType)
115111
}
@@ -121,11 +117,10 @@ func NewHandlerWithKeysAndLog(genericService sv.SimpleService, keys []string, mo
121117
writeLog2 = writeLog
122118
}
123119
c := sv.InitializeAction(conf)
124-
s := sv.InitializeStatus(status)
125120
loadHandler := NewLoadHandlerWithKeysAndLog(genericService.Load, keys, modelType, logError, writeLog2, *c.Load, resource)
126121
_, jsonMapIndex, _ := sv.BuildMapField(modelType)
127122

128-
return &GenericHandler{LoadHandler: loadHandler, service: genericService, Status: s, builder: modelBuilder, Validate: validate, Indexes: jsonMapIndex, Log: writeLog, Action: c}
123+
return &GenericHandler{LoadHandler: loadHandler, service: genericService, builder: modelBuilder, Validate: validate, Indexes: jsonMapIndex, Log: writeLog, Action: c}
129124
}
130125
func (h *GenericHandler) Create(ctx echo.Context) error {
131126
return h.Insert(ctx)
@@ -149,17 +144,15 @@ func (h *GenericHandler) Insert(ctx echo.Context) error {
149144
return ErrorAndLog(ctx, http.StatusInternalServerError, sv.InternalServerError, h.Error, h.Resource, h.Action.Create, er1, h.Log)
150145
}
151146
if len(errors) > 0 {
152-
//result0 := model.ResultInfo{Status: model.StatusError, Errors: MakeErrors(errors)}
153-
result0 := sv.ResultInfo{Status: *h.Status.ValidationError, Errors: errors}
154-
return ReturnAndLog(ctx, http.StatusUnprocessableEntity, result0, h.Log, false, h.Resource, h.Action.Create, "Data Validation Failed")
147+
return ReturnAndLog(ctx, http.StatusUnprocessableEntity, errors, h.Log, false, h.Resource, h.Action.Create, "Data Validation Failed")
155148
}
156149
}
157150
var count int64
158151
var er2 error
159152
count, er2 = h.service.Insert(r.Context(), body)
160153
if count <= 0 && er2 == nil {
161154
if h.builder == nil {
162-
return ReturnAndLog(ctx, http.StatusConflict, sv.ReturnStatus(h.Status.DuplicateKey), h.Log, false, h.Resource, h.Action.Create, "Duplicate Key")
155+
return ReturnAndLog(ctx, http.StatusConflict, 0, h.Log, false, h.Resource, h.Action.Create, "Duplicate Key")
163156
}
164157
i := 0
165158
for count <= 0 && i <= 5 {
@@ -204,7 +197,7 @@ func (h *GenericHandler) Update(ctx echo.Context) error {
204197
}
205198
if h.Validate != nil {
206199
errors, er2 := h.Validate(r.Context(), body)
207-
if HasError(ctx, errors, er2, *h.Status.ValidationError, h.Error, h.Log, h.Resource, h.Action.Update) {
200+
if HasError(ctx, errors, er2, h.Error, h.Log, h.Resource, h.Action.Update) {
208201
return er2
209202
}
210203
}
@@ -230,7 +223,7 @@ func (h *GenericHandler) Patch(ctx echo.Context) error {
230223
}
231224
if h.Validate != nil {
232225
errors, er3 := h.Validate(r.Context(), &bodyStruct)
233-
if HasError(ctx, errors, er3, *h.Status.ValidationError, h.Error, h.Log, h.Resource, h.Action.Patch) {
226+
if HasError(ctx, errors, er3, h.Error, h.Log, h.Resource, h.Action.Patch) {
234227
return er3
235228
}
236229
}
@@ -302,14 +295,13 @@ func BuildMapAndCheckId(ctx echo.Context, obj interface{}, keysJson []string, ma
302295
}
303296
return json, er1
304297
}
305-
func HasError(ctx echo.Context, errors []sv.ErrorMessage, err error, status int, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, resource string, action string) bool {
298+
func HasError(ctx echo.Context, errors []sv.ErrorMessage, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, resource string, action string) bool {
306299
if err != nil {
307300
RespondAndLog(ctx, http.StatusInternalServerError, sv.InternalServerError, err, logError, writeLog, resource, action)
308301
return true
309302
}
310303
if len(errors) > 0 {
311-
result0 := sv.ResultInfo{Status: status, Errors: errors}
312-
ReturnAndLog(ctx, http.StatusUnprocessableEntity, result0, writeLog, false, resource, action, "Data Validation Failed")
304+
ReturnAndLog(ctx, http.StatusUnprocessableEntity, errors, writeLog, false, resource, action, "Data Validation Failed")
313305
return true
314306
}
315307
return false
@@ -358,7 +350,8 @@ func Respond(ctx echo.Context, code int, result interface{}, err error, logError
358350
return Succeed(ctx, code, result, writeLog, resource, action)
359351
}
360352
}
361-
func Return(ctx echo.Context, code int, result sv.ResultInfo, status sv.StatusConfig, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, options ...string) {
353+
/*
354+
func Return222(ctx echo.Context, code int, result sv.ResultInfo, status sv.StatusConfig, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, options ...string) {
362355
var resource, action string
363356
if len(options) > 0 && len(options[0]) > 0 {
364357
resource = options[0]
@@ -386,7 +379,7 @@ func Return(ctx echo.Context, code int, result sv.ResultInfo, status sv.StatusCo
386379
}
387380
}
388381
}
389-
382+
*/
390383
func Result(ctx echo.Context, code int, result interface{}, err error, logError func(context.Context, string, ...map[string]interface{}), opts...interface{}) error {
391384
if err != nil {
392385
if len(opts) > 0 && opts[0] != nil {

echo/load_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ func (h *LoadHandler) Load(ctx echo.Context) error {
6262
return er1
6363
} else {
6464
model, er2 := h.LoadData(r.Context(), id)
65-
return RespondModel(ctx, model, er2, h.Error, h.WriteLog, h.Resource, h.Activity)
65+
return Return(ctx, model, er2, h.Error, h.WriteLog, h.Resource, h.Activity)
6666
}
6767
}
6868

69-
func RespondModel(ctx echo.Context, model interface{}, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error {
69+
func Return(ctx echo.Context, model interface{}, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error {
7070
if err != nil {
7171
return RespondAndLog(ctx, http.StatusInternalServerError, sv.InternalServerError, err, logError, writeLog, resource, action)
7272
} else {

0 commit comments

Comments
 (0)