forked from ebhomengo/niki
1
0
Fork 0

fix(niki): path linter errors

This commit is contained in:
Iman Mirazimi 2024-01-01 10:52:14 +03:30
parent 0b48cb4971
commit bc440a26ad
29 changed files with 238 additions and 113 deletions

View File

@ -6,9 +6,9 @@ type Server struct {
config config.Config config config.Config
} }
func New(config config.Config) Server { func New(cfg config.Config) Server {
return Server{ return Server{
config: config, config: cfg,
} }
} }

View File

@ -28,6 +28,7 @@ func AllKindBoxStatus() []string {
for status, str := range kindBoxStatusStrings { for status, str := range kindBoxStatusStrings {
statusStrings[int(status)-1] = str statusStrings[int(status)-1] = str
} }
return statusStrings return statusStrings
} }
@ -38,5 +39,6 @@ func MapToKindBoxStatus(statusStr string) KindBoxStatus {
return status return status
} }
} }
return KindBoxStatus(0) return KindBoxStatus(0)
} }

3
go.mod
View File

@ -3,11 +3,14 @@ module git.gocasts.ir/ebhomengo/niki
go 1.21.3 go 1.21.3
require ( require (
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/knadh/koanf v1.5.0 github.com/knadh/koanf v1.5.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/natefinch/lumberjack.v2 v2.2.1
) )
require ( require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/fatih/structs v1.1.0 // indirect github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect

6
go.sum
View File

@ -11,6 +11,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4=
github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw= github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw=
github.com/aws/aws-sdk-go-v2/credentials v1.4.3/go.mod h1:FNNC6nQZQUuyhq5aE5c7ata8o9e4ECGmS4lAXC7o1mQ= github.com/aws/aws-sdk-go-v2/credentials v1.4.3/go.mod h1:FNNC6nQZQUuyhq5aE5c7ata8o9e4ECGmS4lAXC7o1mQ=
@ -55,6 +57,10 @@ github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=

View File

@ -1,42 +1,49 @@
package httpmsg package httpmsg
import ( import (
"errors"
"net/http" "net/http"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
// TODO: this temperary to ignore linter error.(maggic number).
const (
internalStatus = 500
)
func Error(err error) (message string, code int) { func Error(err error) (message string, code int) {
switch err.(type) { var re *richerror.RichError
case richerror.RichError: if !errors.As(err, &re) {
re := err.(richerror.RichError)
msg := re.Message()
code := mapKindToHTTPStatusCode(re.Kind())
// we should not expose unexpected error messages
if code >= 500 {
msg = errmsg.ErrorMsgSomethingWentWrong
}
return msg, code
default:
return err.Error(), http.StatusBadRequest return err.Error(), http.StatusBadRequest
} }
msg := re.Message()
code = mapKindToHTTPStatusCode(re.Kind())
// we should not expose unexpected error messages
if code >= internalStatus {
msg = errmsg.ErrorMsgSomethingWentWrong
}
return msg, code
} }
func mapKindToHTTPStatusCode(kind richerror.Kind) int { func mapKindToHTTPStatusCode(kind richerror.Kind) int {
switch kind { switch kind {
case richerror.KindInvalid: case richerror.KindInvalid:
return http.StatusUnprocessableEntity return http.StatusUnprocessableEntity
case richerror.KindNotFound: case richerror.KindNotFound:
return http.StatusNotFound return http.StatusNotFound
case richerror.KindForbidden: case richerror.KindForbidden:
return http.StatusForbidden return http.StatusForbidden
case richerror.KindUnexpected: case richerror.KindUnexpected:
return http.StatusInternalServerError return http.StatusInternalServerError
default: default:
return http.StatusBadRequest return http.StatusBadRequest
} }
} }

View File

@ -1,5 +1,7 @@
package richerror package richerror
import "errors"
type Kind int type Kind int
const ( const (
@ -25,26 +27,31 @@ func New(op Op) RichError {
func (r RichError) WithOp(op Op) RichError { func (r RichError) WithOp(op Op) RichError {
r.operation = op r.operation = op
return r return r
} }
func (r RichError) WithErr(err error) RichError { func (r RichError) WithErr(err error) RichError {
r.wrappedError = err r.wrappedError = err
return r return r
} }
func (r RichError) WithMessage(message string) RichError { func (r RichError) WithMessage(message string) RichError {
r.message = message r.message = message
return r return r
} }
func (r RichError) WithKind(kind Kind) RichError { func (r RichError) WithKind(kind Kind) RichError {
r.kind = kind r.kind = kind
return r return r
} }
func (r RichError) WithMeta(meta map[string]interface{}) RichError { func (r RichError) WithMeta(meta map[string]interface{}) RichError {
r.meta = meta r.meta = meta
return r return r
} }
@ -61,8 +68,8 @@ func (r RichError) Kind() Kind {
return r.kind return r.kind
} }
re, ok := r.wrappedError.(RichError) var re *RichError
if !ok { if !errors.As(r.wrappedError, re) {
return 0 return 0
} }
@ -74,8 +81,8 @@ func (r RichError) Message() string {
return r.message return r.message
} }
re, ok := r.wrappedError.(RichError) var re *RichError
if !ok { if !errors.As(r.wrappedError, re) {
return r.wrappedError.Error() return r.wrappedError.Error()
} }

View File

@ -14,29 +14,36 @@ type Config struct {
DBName string `koanf:"db_name"` DBName string `koanf:"db_name"`
} }
type MySQLDB struct { type DB struct {
config Config config Config
db *sql.DB db *sql.DB
} }
func (m *MySQLDB) Conn() *sql.DB { func (m *DB) Conn() *sql.DB {
return m.db return m.db
} }
func New(config Config) *MySQLDB { // TODO: this temperary to ignore linter error (magic number).
const (
dbMaxConnLifetime = time.Minute * 3
dbMaxOpenConns = 10
dbMaxIdleConns = 10
)
func New(config Config) *DB {
// parseTime=true changes the output type of DATE and DATETIME values to time.Time // parseTime=true changes the output type of DATE and DATETIME values to time.Time
// instead of []byte / string // instead of []byte / string
// The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time // The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true",
config.Username, config.Password, config.Host, config.Port, config.DBName)) config.Username, config.Password, config.Host, config.Port, config.DBName))
if err != nil { if err != nil {
panic(fmt.Errorf("can't open mysql db: %v", err)) panic(fmt.Errorf("can't open mysql db: %w", err))
} }
// See "Important settings" section. // See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3) db.SetConnMaxLifetime(dbMaxConnLifetime)
db.SetMaxOpenConns(10) db.SetMaxOpenConns(dbMaxOpenConns)
db.SetMaxIdleConns(10) db.SetMaxIdleConns(dbMaxIdleConns)
return &MySQLDB{config: config, db: db} return &DB{config: config, db: db}
} }

View File

@ -3,10 +3,10 @@ package mysqlkindbox
import "git.gocasts.ir/ebhomengo/niki/repository/mysql" import "git.gocasts.ir/ebhomengo/niki/repository/mysql"
type DB struct { type DB struct {
conn *mysql.MySQLDB conn *mysql.DB
} }
func New(conn *mysql.MySQLDB) *DB { func New(conn *mysql.DB) *DB {
return &DB{ return &DB{
conn: conn, conn: conn,
} }

View File

@ -21,5 +21,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki
if err != nil { if err != nil {
return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
return param.KindBoxAddResponse{KindBox: kindBox}, nil return param.KindBoxAddResponse{KindBox: kindBox}, nil
} }

View File

@ -13,5 +13,6 @@ func (s Service) GetAll(ctx context.Context) (param.KindBoxGetAllResponse, error
if err != nil { if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxvalidator package adminkindboxvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -27,8 +29,8 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -43,5 +45,5 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxvalidator package adminkindboxvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxvalidator package adminkindboxvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxvalidator package adminkindboxvalidator
import ( import (
"errors"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
@ -21,24 +23,24 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist), validation.By(v.doesKindBoxExist),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.SerialNumber, is.Alphanumeric), validation.Field(&req.SerialNumber, is.Alphanumeric),
validation.Field(&req.TotalAmount, validation.Min(0)), validation.Field(&req.TotalAmount, validation.Min(0)),
validation.Field(&req.SenderID, validation.Field(&req.SenderID,
validation.By(v.doesEmployeeExist(req.SenderID))), validation.By(v.doesEmployeeExist)),
validation.Field(&req.ReceiverID, validation.Field(&req.ReceiverID,
validation.By(v.doesEmployeeExist(req.ReceiverID))), validation.By(v.doesEmployeeExist)),
validation.Field(&req.Status, validation.Field(&req.Status,
validation.In(entity.AllKindBoxStatus())), validation.In(entity.AllKindBoxStatus())),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -53,5 +55,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -26,7 +26,10 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {
receiverID := value.(uint) receiverID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxRequestExist(receiverID) _, err := v.repo.KindBoxRequestExist(receiverID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -36,7 +39,10 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
} }
func (v Validator) doesEmployeeExist(value interface{}) error { func (v Validator) doesEmployeeExist(value interface{}) error {
senderID := value.(uint) senderID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.EmployeeExist(senderID) _, err := v.repo.EmployeeExist(senderID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -46,7 +52,10 @@ func (v Validator) doesEmployeeExist(value interface{}) error {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(uint) benefactorID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -56,7 +65,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
} }
func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxExist(value interface{}) error {
kindboxID := value.(uint) kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxExist(kindboxID) _, err := v.repo.KindBoxExist(kindboxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -67,8 +79,11 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
kbId := value.(uint) kbID, ok := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
@ -77,18 +92,25 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
} }
} }
func (v Validator) hasCorrectStatus(value interface{}) error { // TODO: this temperary to ignore linter error. (unused function)
status := value.(string) // func (v Validator) hasCorrectStatus(value interface{}) error {
_, err := v.repo.CheckStatus(status) // status, ok := value.(string)
if err != nil { // if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound) // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} // }
// _, err := v.repo.CheckStatus(status)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
return nil // return nil
} // }
func (v Validator) hasPendingStatus(value interface{}) error { func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint) kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.PendingStatus(kindboxID) _, err := v.repo.PendingStatus(kindboxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)

View File

@ -1,6 +1,8 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -24,8 +26,8 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -40,5 +42,5 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -24,12 +26,12 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -44,5 +46,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -29,7 +29,10 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(int) benefactorID, ok := value.(int)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -40,8 +43,11 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
kbId := value.(uint) kbID, ok := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
@ -51,7 +57,10 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindboxreqID := value.(int) kindboxreqID, ok := value.(int)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxRequestExist(kindboxreqID) _, err := v.repo.KindBoxRequestExist(kindboxreqID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -61,7 +70,10 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
} }
func (v Validator) doesTypeExist(value interface{}) error { func (v Validator) doesTypeExist(value interface{}) error {
typeID := value.(int) typeID, ok := value.(int)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.TypeExist(typeID) _, err := v.repo.TypeExist(typeID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -71,7 +83,10 @@ func (v Validator) doesTypeExist(value interface{}) error {
} }
func (v Validator) hasPendingStatus(value interface{}) error { func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint) kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.PendingStatus(kindboxID) _, err := v.repo.PendingStatus(kindboxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)

View File

@ -1,6 +1,8 @@
package userkindboxvalidator package userkindboxvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -22,8 +24,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -38,5 +40,5 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package userkindboxvalidator package userkindboxvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -17,8 +19,8 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -33,5 +35,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -10,7 +10,7 @@ import (
type Repository interface { type Repository interface {
BenefactorExist(id uint) (bool, error) BenefactorExist(id uint) (bool, error)
KindBoxExist(id uint) (bool, error) KindBoxExist(id uint) (bool, error)
KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error)
} }
type Validator struct { type Validator struct {
@ -22,7 +22,10 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(uint) benefactorID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -32,7 +35,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
} }
func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxExist(value interface{}) error {
kindBoxID := value.(uint) kindBoxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxExist(kindBoxID) _, err := v.repo.KindBoxExist(kindBoxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -43,8 +49,11 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
kbId := value.(uint) kbID, ok := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }

View File

@ -1,6 +1,8 @@
package userkindboxreqvalidator package userkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -24,8 +26,8 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -40,5 +42,5 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package userkindboxreqvalidator package userkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -23,8 +25,8 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -39,5 +41,5 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package userkindboxreqvalidator package userkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -22,8 +24,8 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -38,5 +40,5 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package userkindboxreqvalidator package userkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -17,8 +19,8 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -33,5 +35,5 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -1,6 +1,8 @@
package userkindboxreqvalidator package userkindboxreqvalidator
import ( import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -25,12 +27,12 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors) var errV validation.Errors
if ok { if errors.As(err, &errV) {
for key, value := range errV { for key, value := range errV {
if value != nil { if value != nil {
fieldErrors[key] = value.Error() fieldErrors[key] = value.Error()
@ -45,5 +47,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
WithErr(err) WithErr(err)
} }
return nil, nil return map[string]string{}, nil
} }

View File

@ -16,7 +16,7 @@ type Repository interface {
BenefactorExist(id uint) (bool, error) BenefactorExist(id uint) (bool, error)
KindBoxReqExist(id uint) (bool, error) KindBoxReqExist(id uint) (bool, error)
TypeExist(id uint) (bool, error) TypeExist(id uint) (bool, error)
KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error)
PendingStatus(id uint) (bool, error) PendingStatus(id uint) (bool, error)
} }
@ -29,7 +29,10 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(uint) benefactorID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -39,7 +42,10 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
} }
func (v Validator) doesTypeExist(value interface{}) error { func (v Validator) doesTypeExist(value interface{}) error {
typeID := value.(uint) typeID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.TypeExist(typeID) _, err := v.repo.TypeExist(typeID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -49,7 +55,10 @@ func (v Validator) doesTypeExist(value interface{}) error {
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindBoxReqID := value.(uint) kindBoxReqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxReqExist(kindBoxReqID) _, err := v.repo.KindBoxReqExist(kindBoxReqID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -60,8 +69,11 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
kbId := value.(uint) kbID, ok := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId) if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }
@ -71,7 +83,10 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
} }
func (v Validator) hasPendingStatus(value interface{}) error { func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint) kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.PendingStatus(kindboxID) _, err := v.repo.PendingStatus(kindboxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)