forked from ebhomengo/niki
feat(admin): Add status filter in get all refer times(#193)
This commit is contained in:
parent
300b69b449
commit
281adb0af5
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
19
docs/docs.go
19
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
FieldErrors map[string]string `json:"field_errors,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue