0 | module HTTP.API.Server.Query
 1 |
 2 | import Data.List
 3 | import HTTP.API.Server.Interface
 4 |
 5 | %default total
 6 |
 7 | parameters {auto loc : HTTPLocal}
 8 |   convertQ :
 9 |        (fs : List QField)
10 |     -> All Decode (QueryConstraintTypes fs)
11 |     -> Queries
12 |     -> Either DecodeErr (HList (QueryTypes fs))
13 |   convertQ []               []        qs = Right []
14 |   convertQ ((n ?? t) :: xs) (y :: ys) qs = Prelude.do
15 |     let Just (QVal bs) := lookup n qs
16 |           | Nothing     => Left (Msg $ missingQueryParameter $ toString n)
17 |           | Just QEmpty => Left (Msg $ missingQueryValue $ toString n)
18 |     v  <- decodeAs t bs
19 |     vs <- convertQ xs ys qs
20 |     Right $ v::vs
21 |   convertQ (QBool n :: xs) ys qs = Prelude.do
22 |     vs <- convertQ xs ys qs
23 |     Right $ isJust (lookup n qs) :: vs
24 |
25 |   public export
26 |   Serve ReqQuery where
27 |     InTypes q               = QueryTypes q.fields
28 |     OutTypes _              = []
29 |     Constraint q            = All Decode (QueryConstraintTypes q.fields)
30 |     outs q                  = []
31 |     canHandle _ r           = True
32 |     adjResponse _ _ _       = pure
33 |     fromRequest q r         =
34 |       either
35 |         (throw . decodeErr badRequest400)
36 |         pure
37 |         (convertQ q.fields con r.uri.queries)
38 |
39 |