Skip to content

Commit

Permalink
Use Marc's query union solution
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Nov 20, 2023
1 parent 83cae66 commit bf0305c
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Web/Controller/Projects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ module Web.Controller.Projects where
import Web.Controller.Prelude
import Web.View.Projects.Index
import Web.View.Projects.New
import qualified IHP.QueryBuilder as QueryBuilder


instance Controller ProjectsController where
action ProjectsAction = do
let values :: [(ProjectType, Int)] = [(ProjectTypeOngoing, 3), (ProjectTypeNotStarted, 2)]

valuePairToCondition :: (ProjectType, Int) -> (QueryBuilder "projects" -> QueryBuilder "projects")
valuePairToCondition (projectType, participants) subQuery =
subQuery
|> queryOr
(\qb -> qb |> filterWhere (#projectType, projectType))
(\qb -> qb |> filterWhere (#participants, participants))
valuePairToCondition :: (ProjectType, Int) -> QueryBuilder "projects"
valuePairToCondition (projectType, participants) =
query @Project
|> filterWhere (#projectType, projectType)
|> filterWhere (#participants, participants)

conditions = foldl' (\queryBuilder condition -> condition queryBuilder) (query @Project) (map valuePairToCondition values)
theQuery = buildOrQuery (map valuePairToCondition values)

projects <- fetch conditions
projects <- fetch theQuery
render IndexView { .. }

action NewProjectAction = do
Expand All @@ -38,3 +38,13 @@ instance Controller ProjectsController where

buildProject project = project
|> fill @'["projectType", "participants"]

buildOrQuery :: [QueryBuilder "projects"] -> NoJoinQueryBuilderWrapper "projects"
buildOrQuery (query:rest) = queryUnion query (buildOrQuery rest)
buildOrQuery [] =
let
inner =
query @Project
|> filterWhereSql (#id, "IS NULL") -- Always false condition
in
queryUnion inner inner -- Terrible hack to make the types work out

0 comments on commit bf0305c

Please sign in to comment.