|
|
@@ -197,7 +197,14 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
|
|
|
|
|
|
type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
|
|
|
|
|
|
-func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
|
|
|
+type CustomCondition struct {
|
|
|
+ Conditions *sql.Conditions
|
|
|
+ OrderBy string
|
|
|
+}
|
|
|
+
|
|
|
+type FormCustomConditionFunc func(c *api.Context, params request.Params, e entity.Entity) (*CustomCondition, error)
|
|
|
+
|
|
|
+func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback, formCustomConditionFunc FormCustomConditionFunc) binding.ServiceFunc[response.InfosData[O]] {
|
|
|
return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
|
|
|
errResponse := response.InfosData[O]{
|
|
|
Infos: make([]O, 0),
|
|
|
@@ -231,21 +238,31 @@ func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O]
|
|
|
return errResponse, err
|
|
|
}
|
|
|
|
|
|
- for _, field := range fields {
|
|
|
- hasDeal := false
|
|
|
- if conditionFieldCallback != nil {
|
|
|
- hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
|
|
|
- if err != nil {
|
|
|
- return errResponse, err
|
|
|
+ if formCustomConditionFunc == nil {
|
|
|
+ for _, field := range fields {
|
|
|
+ hasDeal := false
|
|
|
+ if conditionFieldCallback != nil {
|
|
|
+ hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if !hasDeal {
|
|
|
- fieldValue := reflect.ValueOf(field.Value)
|
|
|
- if !fieldValue.IsZero() {
|
|
|
- conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ if !hasDeal {
|
|
|
+ fieldValue := reflect.ValueOf(field.Value)
|
|
|
+ if !fieldValue.IsZero() {
|
|
|
+ conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ customCondition, err := formCustomConditionFunc(c, params, e)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ conditions = customCondition.Conditions
|
|
|
+ orderBy = customCondition.OrderBy
|
|
|
}
|
|
|
|
|
|
err = callbackBeforeQuery(callbacks, c, params, e, i)
|