laravel-orion icon indicating copy to clipboard operation
laravel-orion copied to clipboard

:sparkles: aggregates support + filterable includes / aggregates

Open GautierDele opened this issue 3 years ago • 4 comments

closes #127

Hello,

Here you can find the PR to allow aggregates. This also includes filtering on aggregates / filters.

Two way of doing this: Using query params (no filters):

localhost/api/users/search?include=posts&aggregateAvg=posts.id

Using post params (search):

{
    "aggregate": [
        {
            "relation": "posts",
            "type": "count",
            "filters": [
                {"field" : "created_at", "operator" : ">=", "value" : "2020-01-01"},
                {"nested": [
                    {"field": "id", "operator": ">", "value": 20},
                    {"field": "id", "operator": ">", "value": 21, "type": "or"}
                ]}
            ]
        },
        {
            "relation": "posts.id",
            "type": "avg",
            "filters": [
                {"field" : "created_at", "operator" : ">=", "value" : "2020-01-01"},
                {"nested": [
                    {"field": "id", "operator": ">", "value": 20},
                    {"field": "id", "operator": ">", "value": 21, "type": "or"}
                ]}
            ]
        },
        {
            "relation": "posts.id",
            "type": "sum"
        }
    ],
    "include": [
        {
            "relation": "posts",
            "filters": [
                {"field" : "created_at", "operator" : ">=", "value" : "2020-01-01"},
                {"nested": [
                    {"field": "id", "operator": ">", "value": 10},
                    {"field": "id", "operator": ">", "value": 11, "type": "or"}
                ]} 
            ]
        }
    ]
}

Nested filters are also possible on aggregates / includes.

Filters works natively with "filterableBy" present in controller.

I took time to refactor some of my previous code. I also rewrote the "include" part to make it compatible with post params and remove some duplicated code.

TODO IN PROGRESS:

  • [x] Documentation
  • [x] Testing

Please do not merge without these, i'm currently working on it

GautierDele avatar Oct 13 '22 19:10 GautierDele

@alexzarbn Do you have any idea why I have some checks (depending on PHP version and Laravel) that gives query params to

$request->post()

This was a requirement on my side to keep harmonisation, i'm quite confused this happens I didn't know about it

GautierDele avatar Oct 13 '22 21:10 GautierDele

@alexzarbn PR is almost ready i need to update tests for older versions, add documentation and change one bribe of logic in the app (see @TODO in code). Could you make your code review while waiting since this feature is heavy ?

Thanks, Gautier

GautierDele avatar Oct 15 '22 21:10 GautierDele

Hi @GautierDele,

Thank you so much for the PR! The implementation looks great, let me review it in depth right now ~

alexzarbn avatar Oct 16 '22 08:10 alexzarbn

Can I have your help with those tests @alexzarbn i did set up an environment on my side with php 7.4 and Laravel 6 using your sample projet and i can't manage to reproduce the error giving by automation ... image Can you reproduce those non assertions on your side ?

GautierDele avatar Oct 16 '22 17:10 GautierDele

All ready on my side @alexzarbn, waiting for your feedback 😄

GautierDele avatar Oct 17 '22 22:10 GautierDele

Awesome, merging ✨

alexzarbn avatar Oct 18 '22 13:10 alexzarbn