Define a QueryBuilder by filtering then sorting

dharmatech

Hey y'all,

I posted the following to stackoverflow yesterday:

Define a QueryBuilder by filtering then sorting

No answers so far, so I thought I'd check here so see if anyone has any suggestions. :-)

The section Update 1 shows an approach which works, however there's some code duplication. Any suggestions on making it more idiomatic are welcome.

dharmatech

This works and is very close to ideal it seems:

let sortClause q = (case sortOrder of
                    (Just "NameAsc") -> orderByAsc #lastName
                    (Just "NameDsc") -> orderByDesc #lastName
                    (Just "DateAsc") -> orderByAsc  #enrollmentDate
                    (Just "DateDsc") -> orderByDesc #enrollmentDate
                    Nothing -> orderByAsc #lastName
                    _ -> orderByAsc #lastName)   

students <- case searchString' of

    Nothing -> query @Student 
        |> queryOr
            (filterWhereILike (#lastName, "%"))
            (filterWhereILike (#firstMidName, "%"))
        |> sortClause (query @Student)
        |> fetch

    (Just str) -> query @Student 
        |> queryOr
            (filterWhereILike (#lastName, "%" <> str <> "%"))
            (filterWhereILike (#firstMidName, "%" <> str <> "%"))
        |> sortClause (query @Student)
        |> fetch  

The only issue there is that I have to include a queryOr in the Nothing branch just to make the types line up:

    Nothing -> query @Student 
        |> queryOr
            (filterWhereILike (#lastName, "%"))
            (filterWhereILike (#firstMidName, "%"))

Any suggestions around settings this up so that this extra queryOr is not needed are welcome. :-)

marc PRO

Just responded on SO :)