Browse Source

condition methods

0x4a52466c696e74 1 tháng trước cách đây
mục cha
commit
49bfae2af7
2 tập tin đã thay đổi với 26 bổ sung2 xóa
  1. 20 1
      conditions.go
  2. 6 1
      rest_gorm/request_list.go

+ 20 - 1
conditions.go

@@ -54,17 +54,36 @@ func (s ConditionLogic) IsValid() bool {
 	return true
 }
 
+const (
+	MethodEmpty = ""
+	MethodLower = "lower"
+	MethodUpper = "upper"
+)
+
+type ConditionMethod string
+
+func (s ConditionMethod) IsValid() bool {
+	switch s {
+	case MethodEmpty:
+	case MethodLower:
+	default:
+		return false
+	}
+	return true
+}
+
 //////////////////////////////////////////////////////////////////////////////
 
 type Condition struct {
 	Field    string            `json:"Field"`
 	Logic    ConditionLogic    `json:"Logic"`
 	Operator ConditionOperator `json:"Operator"`
+	Method   ConditionMethod   `json:"Method"`
 	Value    any               `json:"Value"`
 }
 
 func (s *Condition) IsValid() bool {
-	return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid()
+	return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid() && s.Method.IsValid()
 }
 
 //////////////////////////////////////////////////////////////////////////////

+ 6 - 1
rest_gorm/request_list.go

@@ -39,7 +39,12 @@ func (s *List) Result(pg *gorm.DB, fields rest.FieldNamesList, res any) (count i
 			return
 		}
 		if cond.Value != nil {
-			q := fmt.Sprintf("%s %s ?", CamelToSnake(cond.Field), cond.Operator)
+			var q string
+			if cond.Method == rest.MethodEmpty {
+				q = fmt.Sprintf("%s %s ?", CamelToSnake(cond.Field), cond.Operator)
+			} else {
+				q = fmt.Sprintf("%s(%s) %s %s(?)", cond.Method, CamelToSnake(cond.Field), cond.Operator, cond.Method)
+			}
 			if i == 0 || cond.Logic == rest.LogicAND {
 				pg = pg.Where(q, cond.Value)
 			} else {