0x4a52466c696e74 2 mesi fa
parent
commit
6fcbf80fbf
4 ha cambiato i file con 33 aggiunte e 4 eliminazioni
  1. 1 0
      conditions.go
  2. 2 0
      fielder.go
  3. 16 4
      rest_gorm/request_list.go
  4. 14 0
      z_test.go

+ 1 - 0
conditions.go

@@ -10,6 +10,7 @@ const (
 	OperatorMoreEqual = ">="
 	OperatorLessEqual = "<="
 	OperatorLike      = "like"
+	OperatorNot       = "not"
 )
 
 func (s ConditionOperator) IsValid() bool {

+ 2 - 0
fielder.go

@@ -3,6 +3,7 @@ package rest
 import (
 	"errors"
 	"fmt"
+	"log"
 	"reflect"
 	"strings"
 
@@ -320,6 +321,7 @@ func FieldsAny(obj any, files RequestFiles, names FieldList) (any, IErrorArgs) {
 // Fields позволяет получить значения объекта в json
 func Fields(obj any, files RequestFiles, names FieldList) (json.Map, IErrorArgs) {
 	sVal := reflect.ValueOf(obj)
+	log.Println(sVal)
 	rVal, err := fieldVal(sVal.Elem(), "", files, names)
 	if err != nil {
 		return nil, err

+ 16 - 4
rest_gorm/request_list.go

@@ -36,11 +36,23 @@ func (s *List) Result(pg *gorm.DB, fields rest.FieldNamesList, res any) (count i
 			err = rest.ErrorFiled(cond.Field, "Unexpected field")
 			return
 		}
-		q := fmt.Sprintf("%s %s ?", CamelToSnake(cond.Field), cond.Operator)
-		if i == 0 || cond.Logic == rest.LogicAND {
-			pg = pg.Where(q, cond.Value)
+		if cond.Value == nil {
+			q := fmt.Sprintf("%s %s ?", CamelToSnake(cond.Field), cond.Operator)
+			if i == 0 || cond.Logic == rest.LogicAND {
+				pg = pg.Where(q, cond.Value)
+			} else {
+				pg = pg.Or(q, cond.Value)
+			}
 		} else {
-			pg = pg.Or(q, cond.Value)
+			if cond.Operator != "" && cond.Operator != rest.OperatorNot {
+				err = rest.ErrorFiled(cond.Field, "Expected empty operator or not")
+			}
+			q := fmt.Sprintf("%s is %s null", CamelToSnake(cond.Field), cond.Operator)
+			if i == 0 || cond.Logic == rest.LogicAND {
+				pg = pg.Where(q, cond.Value)
+			} else {
+				pg = pg.Or(q, cond.Value)
+			}
 		}
 	}
 

+ 14 - 0
z_test.go

@@ -403,6 +403,7 @@ type User struct {
 }
 
 type AuthEmail struct {
+	Category  json.Map
 	Email     string `rest:"required" example:"mail@mail.ml"`
 	ThemeName string `rest:"required"`
 	UUser     *User
@@ -432,6 +433,7 @@ func (s *AuthEmail) Execute(req IRequestIn) IRequestOut {
 
 func TestSerializeEmail(t *testing.T) {
 	m := json.Map{
+		"Category":  nil,
 		"email":     "flint@77i.su",
 		"themeName": "th-name",
 		"UUser": json.Map{
@@ -453,3 +455,15 @@ func TestSerializeEmail(t *testing.T) {
 	//t.Log(*r.UUser.ParentID)
 
 }
+
+func TestFielderMap(t *testing.T) {
+	m := json.Map{
+		"one": 1,
+		"two": 2,
+	}
+	rm, err := Fields(m, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	rm.LogPretty()
+}