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
}
func New(config config.Config) Server {
func New(cfg config.Config) Server {
return Server{
config: config,
config: cfg,
}
}

View File

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

3
go.mod
View File

@ -3,11 +3,14 @@ module git.gocasts.ir/ebhomengo/niki
go 1.21.3
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
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // 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-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/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/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw=
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.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
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-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=

View File

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

View File

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

View File

@ -14,29 +14,36 @@ type Config struct {
DBName string `koanf:"db_name"`
}
type MySQLDB struct {
type DB struct {
config Config
db *sql.DB
}
func (m *MySQLDB) Conn() *sql.DB {
func (m *DB) Conn() *sql.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
// instead of []byte / string
// 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",
config.Username, config.Password, config.Host, config.Port, config.DBName))
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.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(dbMaxConnLifetime)
db.SetMaxOpenConns(dbMaxOpenConns)
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"
type DB struct {
conn *mysql.MySQLDB
conn *mysql.DB
}
func New(conn *mysql.MySQLDB) *DB {
func New(conn *mysql.DB) *DB {
return &DB{
conn: conn,
}

View File

@ -21,5 +21,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki
if err != nil {
return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
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 {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
}

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
package adminkindboxvalidator
import (
"errors"
"git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
@ -27,18 +29,18 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
validation.Field(&req.TotalAmount, validation.Min(0)),
validation.Field(&req.SenderID,
validation.By(v.doesEmployeeExist(req.SenderID))),
validation.By(v.doesEmployeeExist)),
validation.Field(&req.ReceiverID,
validation.By(v.doesEmployeeExist(req.ReceiverID))),
validation.By(v.doesEmployeeExist)),
validation.Field(&req.Status,
validation.In(entity.AllKindBoxStatus())),
); err != nil {
fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors)
if ok {
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
@ -53,5 +55,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
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 {
receiverID := value.(uint)
receiverID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxRequestExist(receiverID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -36,7 +39,10 @@ func (v Validator) doesKindBoxRequestExist(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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -46,7 +52,10 @@ func (v Validator) doesEmployeeExist(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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -56,7 +65,10 @@ func (v Validator) doesBenefactorExist(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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -67,8 +79,11 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
kbID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
@ -77,18 +92,25 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
}
}
func (v Validator) hasCorrectStatus(value interface{}) error {
status := value.(string)
_, err := v.repo.CheckStatus(status)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
// TODO: this temperary to ignore linter error. (unused function)
// func (v Validator) hasCorrectStatus(value interface{}) error {
// status, ok := value.(string)
// if !ok {
// 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 {
kindboxID := value.(uint)
kindboxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
package adminkindboxreqvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
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.Field(&req.TypeID,
validation.By(v.doesTypeExist(req.TypeID))),
validation.By(v.doesTypeExist)),
); err != nil {
fieldErrors := make(map[string]string)
errV, ok := err.(validation.Errors)
if ok {
var errV validation.Errors
if errors.As(err, &errV) {
for key, value := range errV {
if value != nil {
fieldErrors[key] = value.Error()
@ -44,5 +46,5 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
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 {
benefactorID := value.(int)
benefactorID, ok := value.(int)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -40,8 +43,11 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
kbID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
@ -51,7 +57,10 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R
}
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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -61,7 +70,10 @@ func (v Validator) doesKindBoxRequestExist(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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -71,7 +83,10 @@ func (v Validator) doesTypeExist(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)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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