From 281adb0af520d86e49b05a8f63a99c88f451d902 Mon Sep 17 00:00:00 2001 From: Hamed Xamani Date: Tue, 15 Oct 2024 17:54:53 +0330 Subject: [PATCH] feat(admin): Add status filter in get all refer times(#193) --- .../http_server/admin/refer_time/get_all.go | 18 ++++++++- docs/docs.go | 19 +++++++++ docs/swagger.json | 19 +++++++++ docs/swagger.yaml | 13 +++++++ param/admin/refer_time/get_all.go | 12 ++++-- repository/mysql/refer_time/get_all.go | 8 ++-- service/admin/refer_time/get_all.go | 14 ++++++- service/admin/refer_time/service.go | 11 ++++-- service/benefactor/refer_time/get_all.go | 4 +- service/benefactor/refer_time/service.go | 2 +- service/service.go | 10 ++--- validator/admin/refer_time/get_all.go | 39 +++++++++++++++++++ validator/admin/refer_time/validator.go | 32 +++++++++++++++ 13 files changed, 180 insertions(+), 21 deletions(-) create mode 100644 validator/admin/refer_time/get_all.go create mode 100644 validator/admin/refer_time/validator.go diff --git a/delivery/http_server/admin/refer_time/get_all.go b/delivery/http_server/admin/refer_time/get_all.go index aaf0b357..cda4577a 100644 --- a/delivery/http_server/admin/refer_time/get_all.go +++ b/delivery/http_server/admin/refer_time/get_all.go @@ -5,6 +5,7 @@ import ( refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" echo "github.com/labstack/echo/v4" ) @@ -13,6 +14,7 @@ import ( // @Tags Admins ReferTimes // @Accept json // @Produce json +// @Param filter_status query entity.ReferTimeStatus false "Filter by KindBoxReq status" Format(enum) // @Success 200 {object} adminrefertimeparam.GetAllReferTimeResponse // @Failure 400 {string} "Bad request" // @Security AuthBearerAdmin @@ -20,12 +22,24 @@ import ( func (h Handler) GetAll(c echo.Context) error { var req refertimeparam.GetAllReferTimeRequest - listCities, err := h.adminReferTimeSvc.GetAll(c.Request().Context(), req) + if err := c.Bind(&req); err != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + req.Filter = queryparam.GetFilterParams(c) + + resp, err := h.adminReferTimeSvc.GetAll(c.Request().Context(), req) if err != nil { msg, code := httpmsg.Error(err) + if resp.FieldErrors != nil { + return c.JSON(code, httpmsg.ErrorResponse{ + Message: msg, + Errors: resp.FieldErrors, + }) + } return echo.NewHTTPError(code, msg) } - return c.JSON(http.StatusOK, listCities) + return c.JSON(http.StatusOK, resp) } diff --git a/docs/docs.go b/docs/docs.go index 9928af1e..5a35ab0c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1166,6 +1166,19 @@ const docTemplate = `{ "Admins ReferTimes" ], "summary": "Get all refer times", + "parameters": [ + { + "enum": [ + "active", + "inactive" + ], + "type": "string", + "format": "enum", + "description": "Filter by KindBoxReq status", + "name": "filter_status", + "in": "query" + } + ], "responses": { "200": { "description": "OK", @@ -3420,6 +3433,12 @@ const docTemplate = `{ "items": { "$ref": "#/definitions/entity.ReferTime" } + }, + "field_errors": { + "type": "object", + "additionalProperties": { + "type": "string" + } } } }, diff --git a/docs/swagger.json b/docs/swagger.json index e77cbde2..c6341ee6 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1155,6 +1155,19 @@ "Admins ReferTimes" ], "summary": "Get all refer times", + "parameters": [ + { + "enum": [ + "active", + "inactive" + ], + "type": "string", + "format": "enum", + "description": "Filter by KindBoxReq status", + "name": "filter_status", + "in": "query" + } + ], "responses": { "200": { "description": "OK", @@ -3409,6 +3422,12 @@ "items": { "$ref": "#/definitions/entity.ReferTime" } + }, + "field_errors": { + "type": "object", + "additionalProperties": { + "type": "string" + } } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 367c1946..944409be 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -402,6 +402,10 @@ definitions: items: $ref: '#/definitions/entity.ReferTime' type: array + field_errors: + additionalProperties: + type: string + type: object type: object adminserviceparam.Data: properties: @@ -1789,6 +1793,15 @@ paths: get: consumes: - application/json + parameters: + - description: Filter by KindBoxReq status + enum: + - active + - inactive + format: enum + in: query + name: filter_status + type: string produces: - application/json responses: diff --git a/param/admin/refer_time/get_all.go b/param/admin/refer_time/get_all.go index 73a6e365..929b64ce 100644 --- a/param/admin/refer_time/get_all.go +++ b/param/admin/refer_time/get_all.go @@ -1,9 +1,15 @@ package adminrefertimeparam -import "git.gocasts.ir/ebhomengo/niki/entity" +import ( + "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/param" +) -type GetAllReferTimeRequest struct{} +type GetAllReferTimeRequest struct { + Filter param.FilterRequest +} type GetAllReferTimeResponse struct { - Data []entity.ReferTime `json:"data"` + Data []entity.ReferTime `json:"data"` + FieldErrors map[string]string `json:"field_errors,omitempty"` } diff --git a/repository/mysql/refer_time/get_all.go b/repository/mysql/refer_time/get_all.go index ea1e4e3f..6ff1095b 100644 --- a/repository/mysql/refer_time/get_all.go +++ b/repository/mysql/refer_time/get_all.go @@ -9,18 +9,18 @@ import ( "git.gocasts.ir/ebhomengo/niki/repository/mysql" ) -func (d *DB) GetAll(ctx context.Context) ([]entity.ReferTime, error) { +func (d *DB) GetAll(ctx context.Context, status entity.ReferTimeStatus) ([]entity.ReferTime, error) { const op = "mysqlrefertime.GetAll" - query := `SELECT * FROM refer_times where status = 'active';` - //nolint + query := `SELECT * FROM refer_times where status = ?;` + stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyReferTimeGetAll, query) if err != nil { return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } - rows, err := stmt.QueryContext(ctx) + rows, err := stmt.QueryContext(ctx, status) if err != nil { return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/service/admin/refer_time/get_all.go b/service/admin/refer_time/get_all.go index 4f80a07e..e2aa27e2 100644 --- a/service/admin/refer_time/get_all.go +++ b/service/admin/refer_time/get_all.go @@ -3,14 +3,24 @@ package adminrefertimeservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (s Service) GetAll(ctx context.Context, _ param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) { +func (s Service) GetAll(ctx context.Context, req param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) { const op = "adminrefertimeservice.GetAllReferTime" + status := entity.ReferTimeActiveStatus - referTimes, err := s.repo.GetAll(ctx) + if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil { + return param.GetAllReferTimeResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) + } + + if str, ok := req.Filter["status"].(string); ok { + status = entity.ReferTimeStatus(str) + } + + referTimes, err := s.repo.GetAll(ctx, status) if err != nil { return param.GetAllReferTimeResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } diff --git a/service/admin/refer_time/service.go b/service/admin/refer_time/service.go index d48c611d..8ed1ca99 100644 --- a/service/admin/refer_time/service.go +++ b/service/admin/refer_time/service.go @@ -4,16 +4,21 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" + validator "git.gocasts.ir/ebhomengo/niki/validator/admin/refer_time" ) type Service struct { repo Repository + vld validator.Validator } type Repository interface { Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) - GetAll(ctx context.Context) ([]entity.ReferTime, error) + GetAll(ctx context.Context, status entity.ReferTimeStatus) ([]entity.ReferTime, error) } -func New(repo Repository) Service { - return Service{repo: repo} +func New(repo Repository, vld validator.Validator) Service { + return Service{ + repo: repo, + vld: vld, + } } diff --git a/service/benefactor/refer_time/get_all.go b/service/benefactor/refer_time/get_all.go index 6a155cd7..19d5eb75 100644 --- a/service/benefactor/refer_time/get_all.go +++ b/service/benefactor/refer_time/get_all.go @@ -3,14 +3,16 @@ package benefactorrefertimeservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) GetAll(ctx context.Context, _ param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) { const op = "benefactorrefertimeservice.GetAllReferTime" + status := entity.ReferTimeActiveStatus - referTimes, err := s.repo.GetAll(ctx) + referTimes, err := s.repo.GetAll(ctx, status) if err != nil { return param.GetAllReferTimeResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } diff --git a/service/benefactor/refer_time/service.go b/service/benefactor/refer_time/service.go index 79c5801a..d87d19e5 100644 --- a/service/benefactor/refer_time/service.go +++ b/service/benefactor/refer_time/service.go @@ -11,7 +11,7 @@ type Service struct { } type Repository interface { Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) - GetAll(ctx context.Context) ([]entity.ReferTime, error) + GetAll(ctx context.Context, status entity.ReferTimeStatus) ([]entity.ReferTime, error) } func New(repo Repository) Service { diff --git a/service/service.go b/service/service.go index 5f53a888..b75bb027 100644 --- a/service/service.go +++ b/service/service.go @@ -34,6 +34,7 @@ import ( adminbenefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/benefactor" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" + adminrefertimevalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/refer_time" agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box" agentkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box_req" benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address" @@ -76,7 +77,8 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont var ( AdminAuthSvc = auth.New(cfg.AdminAuth) AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo) - AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo) + AdminReferTimeVld = adminrefertimevalidator.New() + AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo, AdminReferTimeVld) AdminAddressSvc = adminaddressservice.New(addressRepo) AdminBenefactorVld = adminbenefactorvalidator.New() AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld) @@ -107,8 +109,6 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont BenefactorKindBoxReqSvc = benefactorkindboxreqservice.New(kindBoxReqRepo, BenefactorKindBoxReqVld) BenefactorKindBoxVld = benefactorkindboxvalidator.New(kindBoxRepo, BenefactorSvc, BenefactorAddressSvc, BenefactorReferTimeSvc) BenefactorKindBoxSvc = benefactorkindboxservice.New(kindBoxRepo, BenefactorKindBoxVld) - benefactorReferTimeSvc = benefactorrefertimeservice.New(referTimeRepo) - adminReferTimeSvc = adminrefertimeservice.New(referTimeRepo) ) NotificationSvc := notification.New(cfg.NotificationSvc, smsAdapter, AdminKindBoxReqSvc, AdminBenefactorSvc, AdminSvc, AdminKindBoxSvc) @@ -128,7 +128,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont BenefactorAddressSvc: BenefactorAddressSvc, BenefactorSvc: BenefactorSvc, NotificationSvc: NotificationSvc, - BenefactorReferTimeSvc: benefactorReferTimeSvc, - AdminReferTimeSvc: adminReferTimeSvc, + BenefactorReferTimeSvc: BenefactorReferTimeSvc, + AdminReferTimeSvc: AdminReferTimeSvc, } } diff --git a/validator/admin/refer_time/get_all.go b/validator/admin/refer_time/get_all.go new file mode 100644 index 00000000..0175fad9 --- /dev/null +++ b/validator/admin/refer_time/get_all.go @@ -0,0 +1,39 @@ +package adminrefertimevalidator + +import ( + "errors" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +func (v Validator) ValidateGetAll(req param.GetAllReferTimeRequest) (map[string]string, error) { + const op = "adminrefertimevalidator.ValidateGetAll" + + validFields := []string{"status"} + + if err := validation.ValidateStruct(&req, + validation.Field(&req.Filter, validation.By(v.AreFilterFieldsValid(validFields))), + ); err != nil { + fieldErrors := make(map[string]string) + + var errV validation.Errors + if errors.As(err, &errV) { + for key, value := range errV { + if value != nil { + fieldErrors[key] = value.Error() + } + } + } + + return fieldErrors, richerror.New(op). + WithMessage(errmsg.ErrorMsgInvalidInput). + WithKind(richerror.KindInvalid). + WithMeta(map[string]interface{}{"req": req}). + WithErr(err) + } + + return map[string]string{}, nil +} diff --git a/validator/admin/refer_time/validator.go b/validator/admin/refer_time/validator.go new file mode 100644 index 00000000..dcfe0169 --- /dev/null +++ b/validator/admin/refer_time/validator.go @@ -0,0 +1,32 @@ +package adminrefertimevalidator + +import ( + "fmt" + "slices" + + params "git.gocasts.ir/ebhomengo/niki/param" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +type Validator struct{} + +func New() Validator { + return Validator{} +} + +func (v Validator) AreFilterFieldsValid(validFilters []string) validation.RuleFunc { + return func(value interface{}) error { + filters, ok := value.(params.FilterRequest) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + for filter := range filters { + if !slices.Contains(validFilters, filter) { + return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid) + } + } + + return nil + } +}