feat: Update querry

This commit is contained in:
tomaz jejcic 2023-09-28 16:41:05 +02:00
parent 18fb246f6b
commit 291f49393c
3 changed files with 37 additions and 32 deletions

View File

@ -13,4 +13,11 @@ const (
HealthOk Health = "ok"
)
const (
MissingQuerryFieldValues QueryError = "Missing field values"
QuerryValueExpired QueryError = "Querry field value is expired"
MissingQuerryCondition QueryError = "Missing querry condition"
MissingQuerryField QueryError = "Querry Field is empty"
)
const DateFormat = "02 Jan 2006"

View File

@ -2,3 +2,4 @@ package constants
type Severity string
type Health string
type QueryError string

View File

@ -1,66 +1,63 @@
package validations
import (
"fmt"
catvo "github.com/bestbytes/catalogue/vo"
"github.com/foomo/contentfulvalidation/constants"
)
func IsAttributeExpired(query *catvo.Query, attributes catvo.Attributes) bool {
func ValidateQuery(query *catvo.Query, attributes catvo.Attributes) (constants.QueryError, bool) {
// TODO is this ok?
isValueExpired := func(value string, def catvo.AttributeDefinition) bool {
isValueExpired := func(value string, def catvo.AttributeDefinition) (constants.QueryError, bool) {
if len(value) < 1 {
return constants.MissingQuerryFieldValues, true
}
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
// thow error
// provide contex on value with this id ....
fmt.Println("Attribute NOT found: ", ok)
return true
return constants.QuerryValueExpired, true
} else {
fmt.Println("Attribute found: ", ok)
return false
return "", false
}
}
areValuesExpired := func(values []string, def catvo.AttributeDefinition) bool {
expired := false
areValuesExpired := func(values []string, def catvo.AttributeDefinition) (constants.QueryError, bool) {
if len(values) < 1 {
return constants.MissingQuerryFieldValues, true
}
for _, v := range values {
if isValueExpired(v, def) {
expired = true
if res, ok := isValueExpired(v, def); ok {
return res, true
}
}
return expired
return "", false
}
for _, e := range query.Elements {
fmt.Println("THE Matcher: ", e.Matcher)
errorMessage := constants.QueryError("")
hasError := false
// @TODO validate if there is even an attribute set, or is empty string
if e.Matcher != nil {
if def, ok := attributes[e.Matcher.Attribute]; ok {
switch {
case e.Matcher.StringIn != nil:
return areValuesExpired(e.Matcher.StringIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringIn.Values, def)
case e.Matcher.StringAllIn != nil:
return areValuesExpired(e.Matcher.StringAllIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringAllIn.Values, def)
case e.Matcher.StringNotIn != nil:
return areValuesExpired(e.Matcher.StringNotIn.Values, def)
errorMessage, hasError = areValuesExpired(e.Matcher.StringNotIn.Values, def)
case e.Matcher.StringEquals != nil:
return isValueExpired(e.Matcher.StringEquals.Value, def)
errorMessage, hasError = isValueExpired(e.Matcher.StringEquals.Value, def)
case e.Matcher.StringNotEquals != nil:
return isValueExpired(e.Matcher.StringNotEquals.Value, def)
errorMessage, hasError = isValueExpired(e.Matcher.StringNotEquals.Value, def)
default:
errorMessage, hasError = constants.MissingQuerryCondition, true
}
} else {
// throw error
fmt.Println("NO attribute within ALL ATTRIBUTES: ")
// TODO uncomment once catalogue attr are in, maybe a different validation for this
// return true
return constants.MissingQuerryField, true
}
}
} else {
fmt.Println("MATCHER is NIL e.matcher: ")
return true
if hasError {
return errorMessage, hasError
}
}
return false
return "", false
}