kendo-gridservicestackautoquery-servicestackservicestack-autoquery

Transform Request to Autoquery friendly


We are working with a 3rd party grid (telerik kendo) that has paging/sorting/filtering built in. It will send the requests in a certain way when making the GET call and I'm trying to determine if there is a way to translate these requests to AutoQuery friendly requests.

Query string params

Sort Pattern:

sort[{0}][field] and sort[{0}][dir]

Filtering:

filter[filters][{0}][field]
filter[filters][{0}][operator]
filter[filters][{0}][value]

So this which is populated in the querystring:

filter[filters][0][field]
filter[filters][0][operator]
filter[filters][0][value]

would need to be translated to.

FieldName=1  //      filter[filters][0][field]+filter[filters][0][operator]+filter[filters][0][value] in a nutshell (not exactly true)

Should I manipulate the querystring object in a plugin by removing the filters (or just adding the ones I need) ? Is there a better option here?

I'm not sure there is a clean way to do this on the kendo side either.


Solution

  • I will explain the two routes I'm going down, I hope to see a better answer.

    First, I tried to modify the querystring in a request filter, but could not. I ended up having to run the autoqueries manually by getting the params and modifying them before calling AutoQuery.Execute. Something like this:

    var requestparams = Request.ToAutoQueryParams();
    var q = AutoQueryDb.CreateQuery(requestobject, requestparams);
    AutoQueryDb.Execute(requestobject, q);
    

    I wish there was a more global way to do this. The extension method just loops over all the querystring params and adds the ones that I need.

    After doing the above work, I wasn't very happy with the result so I investigated doing it differently and ended up with the following:

    Register the Kendo grid filter operations to their equivalent Service Stack auto query ones:

     var aq = new AutoQueryFeature { MaxLimit = 100, EnableAutoQueryViewer=true };
            aq.ImplicitConventions.Add("%neq", aq.ImplicitConventions["%NotEqualTo"]);
            aq.ImplicitConventions.Add("%eq", "{Field} = {Value}");
    

    Next, on the grid's read operation, we need to reformat the the querystring:

    read: {
                                    url: "/api/stuff?format=json&isGrid=true",
                                    data: function (options) {
                                        if (options.sort && options.sort.length > 0) {
                                            options.OrderBy = (options.sort[0].dir == "desc" ? "-" : "") + options.sort[0].field;
                                        }
                                        if (options.filter && options.filter.filters.length > 0) {
                                            for (var i = 0; i < options.filter.filters.length; i++) {
                                                var f = options.filter.filters[i];
                                                console.log(f);
                                                options[f.field + f.operator] = f.value;
                                            }
                                        }
    
                                    }
    

    Now, the grid will send the operations in a Autoquery friendly manner.