|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + d "github.com/core-go/diff" |
| 6 | + "github.com/labstack/echo/v4" |
| 7 | + "net/http" |
| 8 | + "reflect" |
| 9 | +) |
| 10 | + |
| 11 | +const internalServerError = "Internal Server Error" |
| 12 | + |
| 13 | +type ApprHandler struct { |
| 14 | + ApprService d.ApprService |
| 15 | + Keys []string |
| 16 | + ModelType reflect.Type |
| 17 | + Error func(context.Context, string) |
| 18 | + Indexes map[string]int |
| 19 | + Offset int |
| 20 | + Log func(ctx context.Context, resource string, action string, success bool, desc string) error |
| 21 | + Resource string |
| 22 | + Action1 string |
| 23 | + Action2 string |
| 24 | +} |
| 25 | + |
| 26 | +func NewApprHandler(apprService d.ApprService, modelType reflect.Type, logError func(context.Context, string), option ...int) *ApprHandler { |
| 27 | + offset := 1 |
| 28 | + if len(option) > 0 && option[0] >= 0 { |
| 29 | + offset = option[0] |
| 30 | + } |
| 31 | + return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, nil) |
| 32 | +} |
| 33 | +func NewApprHandlerWithLogs(apprService d.ApprService, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler { |
| 34 | + return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, writeLog, options...) |
| 35 | +} |
| 36 | +func NewApprHandlerWithKeys(apprService d.ApprService, modelType reflect.Type, logError func(context.Context, string), idNames []string, option ...int) *ApprHandler { |
| 37 | + offset := 1 |
| 38 | + if len(option) > 0 && option[0] >= 0 { |
| 39 | + offset = option[0] |
| 40 | + } |
| 41 | + return NewApprHandlerWithKeysAndLog(apprService, idNames, modelType, offset, logError, nil) |
| 42 | +} |
| 43 | +func NewApprHandlerWithKeysAndLog(apprService d.ApprService, keys []string, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler { |
| 44 | + if offset < 0 { |
| 45 | + offset = 1 |
| 46 | + } |
| 47 | + if keys == nil || len(keys) == 0 { |
| 48 | + keys = d.GetJsonPrimaryKeys(modelType) |
| 49 | + } |
| 50 | + indexes := d.GetIndexes(modelType) |
| 51 | + var resource, action1, action2 string |
| 52 | + if len(options) > 0 && len(options[0]) > 0 { |
| 53 | + action1 = options[0] |
| 54 | + } else { |
| 55 | + action1 = "approve" |
| 56 | + } |
| 57 | + if len(options) > 1 && len(options[1]) > 0 { |
| 58 | + action2 = options[1] |
| 59 | + } else { |
| 60 | + action2 = "reject" |
| 61 | + } |
| 62 | + if len(options) > 2 && len(options[2]) > 0 { |
| 63 | + resource = options[2] |
| 64 | + } else { |
| 65 | + resource = d.BuildResourceName(modelType.Name()) |
| 66 | + } |
| 67 | + return &ApprHandler{Log: writeLog, ApprService: apprService, ModelType: modelType, Keys: keys, Indexes: indexes, Offset: offset, Error: logError, Resource: resource, Action1: action1, Action2: action2} |
| 68 | +} |
| 69 | + |
| 70 | +func (c *ApprHandler) Approve() echo.HandlerFunc { |
| 71 | + return func(ctx echo.Context) error { |
| 72 | + r := ctx.Request() |
| 73 | + id, er1 := d.BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset) |
| 74 | + if er1 != nil { |
| 75 | + ctx.String(http.StatusBadRequest, er1.Error()) |
| 76 | + return er1 |
| 77 | + } else { |
| 78 | + result, er2 := c.ApprService.Approve(r.Context(), id) |
| 79 | + if er2 != nil { |
| 80 | + return handleError(ctx, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action1, er2, c.Log) |
| 81 | + } else { |
| 82 | + return succeed(ctx, http.StatusOK, result, c.Log, c.Resource, c.Action1) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (c *ApprHandler) Reject() echo.HandlerFunc { |
| 89 | + return func(ctx echo.Context) error { |
| 90 | + r := ctx.Request() |
| 91 | + id, er1 := d.BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset) |
| 92 | + if er1 != nil { |
| 93 | + ctx.String(http.StatusBadRequest, er1.Error()) |
| 94 | + return er1 |
| 95 | + } else { |
| 96 | + result, er2 := c.ApprService.Reject(r.Context(), id) |
| 97 | + if er2 != nil { |
| 98 | + return handleError(ctx, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action2, er2, c.Log) |
| 99 | + } else { |
| 100 | + return succeed(ctx, http.StatusOK, result, c.Log, c.Resource, c.Action2) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func respond(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error { |
| 107 | + err := ctx.JSON(code, result) |
| 108 | + if writeLog != nil { |
| 109 | + writeLog(ctx.Request().Context(), resource, action, success, desc) |
| 110 | + } |
| 111 | + return err |
| 112 | +} |
| 113 | +func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string), resource string, action string, err error, writeLog func(context.Context, string, string, bool, string) error) error { |
| 114 | + if logError != nil { |
| 115 | + logError(ctx.Request().Context(), err.Error()) |
| 116 | + } |
| 117 | + respond(ctx, code, result, writeLog, resource, action, false, err.Error()) |
| 118 | + return err |
| 119 | +} |
| 120 | +func succeed(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error { |
| 121 | + return respond(ctx, code, result, writeLog, resource, action, true, "") |
| 122 | +} |
0 commit comments