{-# LANGUAGE UndecidableInstances #-}
module IHP.DataSync.REST.Controller where

import IHP.ControllerPrelude hiding (OrderByClause)
import IHP.DataSync.REST.Types
import Data.Aeson
import Data.Aeson.TH
import qualified Database.PostgreSQL.Simple.ToField as PG
import qualified Database.PostgreSQL.Simple.Types as PG
import qualified Database.PostgreSQL.Simple as PG
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Vector as Vector
import qualified Data.ByteString.Char8 as ByteString
import qualified Control.Exception as Exception
import IHP.DataSync.RowLevelSecurity
import IHP.DataSync.DynamicQuery
import IHP.DataSync.Types
import Network.HTTP.Types (status400)
import IHP.DataSync.DynamicQueryCompiler
import qualified Data.Text as Text
import qualified Data.Scientific as Scientific

import qualified Data.ByteString.Char8 as ByteString
import qualified Data.ByteString.Builder as ByteString
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encoding.Internal as Aeson

import qualified IHP.GraphQL.Types as GraphQL
import qualified IHP.GraphQL.Parser as GraphQL
import qualified IHP.GraphQL.Compiler as GraphQL
import IHP.GraphQL.JSON ()
import qualified Data.Attoparsec.Text as Attoparsec

instance (
    PG.ToField (PrimaryKey (GetTableName CurrentUserRecord))
    , Show (PrimaryKey (GetTableName CurrentUserRecord))
    , HasNewSessionUrl CurrentUserRecord
    , Typeable CurrentUserRecord
    , HasField "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord))
    ) => Controller ApiController where
    action :: ApiController -> IO ()
action CreateRecordAction { Text
$sel:table:CreateRecordAction :: ApiController -> Text
table :: Text
table } = do
        (?modelContext::ModelContext) => Text -> IO TableWithRLS
Text -> IO TableWithRLS
ensureRLSEnabled Text
table

        let payload :: Value
payload = Value
(?context::ControllerContext) => Value
requestBodyJSON

        case Value
payload of
            Object Object
hashMap -> do
                let query :: Query
query = Query
"INSERT INTO ? ? VALUES ? RETURNING *"
                let columns :: [Text]
columns = Object
hashMap
                        Object -> (Object -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Text]
forall k v. HashMap k v -> [k]
HashMap.keys
                        [Text] -> ([Text] -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
fieldNameToColumnName

                let values :: [Action]
values = Object
hashMap
                        Object -> (Object -> [Value]) -> [Value]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Value]
forall k v. HashMap k v -> [v]
HashMap.elems
                        [Value] -> ([Value] -> [Action]) -> [Action]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Value -> Action) -> [Value] -> [Action]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Action
aesonValueToPostgresValue

                let params :: (Identifier, In [Identifier], In [Action])
params = (Text -> Identifier
PG.Identifier Text
table, [Identifier] -> In [Identifier]
forall a. a -> In a
PG.In ((Text -> Identifier) -> [Text] -> [Identifier]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Identifier
PG.Identifier [Text]
columns), [Action] -> In [Action]
forall a. a -> In a
PG.In [Action]
values)

                Either EnhancedSqlError [[Field]]
result :: Either EnhancedSqlError [[Field]] <- IO [[Field]] -> IO (Either EnhancedSqlError [[Field]])
forall e a. Exception e => IO a -> IO (Either e a)
Exception.try do
                    Query -> (Identifier, In [Identifier], In [Action]) -> IO [[Field]]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS Query
query (Identifier, In [Identifier], In [Action])
params

                case Either EnhancedSqlError [[Field]]
result of
                    Left EnhancedSqlError
error -> EnhancedSqlError -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderErrorJson EnhancedSqlError
error
                    Right [[Field]]
result -> [[Field]] -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson [[Field]]
result

            Array Array
objects -> do
                let query :: Query
query = Query
"INSERT INTO ? ? ? RETURNING *"
                let columns :: [Text]
columns = Array
objects
                        Array -> (Array -> [Value]) -> [Value]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Array -> [Value]
forall a. Vector a -> [a]
Vector.toList
                        [Value] -> ([Value] -> Maybe Value) -> Maybe Value
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> [Value] -> Maybe Value
forall a. [a] -> Maybe a
head
                        Maybe Value -> (Maybe Value -> Value) -> Value
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> \case
                            Just Value
value -> Value
value
                            Maybe Value
Nothing -> Text -> Value
forall a. Text -> a
error Text
"Atleast one record is required"
                        Value -> (Value -> Object) -> Object
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> \case
                            Object Object
hashMap -> Object
hashMap
                            Value
otherwise -> Text -> Object
forall a. Text -> a
error Text
"Expected object"
                        Object -> (Object -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Text]
forall k v. HashMap k v -> [k]
HashMap.keys
                        [Text] -> ([Text] -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
fieldNameToColumnName

                let values :: [[Action]]
values = Array
objects
                        Array -> (Array -> [Value]) -> [Value]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Array -> [Value]
forall a. Vector a -> [a]
Vector.toList
                        [Value] -> ([Value] -> [[Action]]) -> [[Action]]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Value -> [Action]) -> [Value] -> [[Action]]
forall a b. (a -> b) -> [a] -> [b]
map (\Value
object ->
                                Value
object
                                Value -> (Value -> Object) -> Object
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> \case
                                    Object Object
hashMap -> Object
hashMap
                                    Value
otherwise -> Text -> Object
forall a. Text -> a
error Text
"Expected object"
                                Object -> (Object -> [Value]) -> [Value]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Value]
forall k v. HashMap k v -> [v]
HashMap.elems
                                [Value] -> ([Value] -> [Action]) -> [Action]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Value -> Action) -> [Value] -> [Action]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Action
aesonValueToPostgresValue
                            )


                let params :: (Identifier, In [Identifier], Values [Action])
params = (Text -> Identifier
PG.Identifier Text
table, [Identifier] -> In [Identifier]
forall a. a -> In a
PG.In ((Text -> Identifier) -> [Text] -> [Identifier]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Identifier
PG.Identifier [Text]
columns), [QualifiedIdentifier] -> [[Action]] -> Values [Action]
forall a. [QualifiedIdentifier] -> [a] -> Values a
PG.Values [] [[Action]]
values)

                [[Field]]
result :: [[Field]] <- Query
-> (Identifier, In [Identifier], Values [Action]) -> IO [[Field]]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS Query
query (Identifier, In [Identifier], Values [Action])
params
                [[Field]] -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson [[Field]]
result



    action UpdateRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table, UUID
$sel:id:CreateRecordAction :: ApiController -> UUID
id :: UUID
id } = do
        (?modelContext::ModelContext) => Text -> IO TableWithRLS
Text -> IO TableWithRLS
ensureRLSEnabled Text
table

        let payload :: Object
payload = Value
(?context::ControllerContext) => Value
requestBodyJSON
                Value -> (Value -> Object) -> Object
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> \case
                    Object Object
hashMap -> Object
hashMap

        let columns :: [Identifier]
columns = Object
payload
                Object -> (Object -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Text]
forall k v. HashMap k v -> [k]
HashMap.keys
                [Text] -> ([Text] -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
fieldNameToColumnName
                [Text] -> ([Text] -> [Identifier]) -> [Identifier]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Text -> Identifier) -> [Text] -> [Identifier]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Identifier
PG.Identifier

        let values :: [Action]
values = Object
payload
                Object -> (Object -> [Value]) -> [Value]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Object -> [Value]
forall k v. HashMap k v -> [v]
HashMap.elems
                [Value] -> ([Value] -> [Action]) -> [Action]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Value -> Action) -> [Value] -> [Action]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Action
aesonValueToPostgresValue

        let keyValues :: [(Identifier, Action)]
keyValues = [Identifier] -> [Action] -> [(Identifier, Action)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Identifier]
columns [Action]
values

        let setCalls :: ByteString
setCalls = [(Identifier, Action)]
keyValues
                [(Identifier, Action)]
-> ([(Identifier, Action)] -> [ByteString]) -> [ByteString]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> ((Identifier, Action) -> ByteString)
-> [(Identifier, Action)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (\(Identifier, Action)
_ -> ByteString
"? = ?")
                [ByteString] -> ([ByteString] -> ByteString) -> ByteString
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> ByteString -> [ByteString] -> ByteString
ByteString.intercalate ByteString
", "
        let query :: ByteString
query = ByteString
"UPDATE ? SET " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
setCalls ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" WHERE id = ? RETURNING *"

        let params :: [Action]
params = [Identifier -> Action
forall a. ToField a => a -> Action
PG.toField (Text -> Identifier
PG.Identifier Text
table)]
                [Action] -> [Action] -> [Action]
forall a. Semigroup a => a -> a -> a
<> ([[Action]] -> [Action]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (((Identifier, Action) -> [Action])
-> [(Identifier, Action)] -> [[Action]]
forall a b. (a -> b) -> [a] -> [b]
map (\(Identifier
key, Action
value) -> [Identifier -> Action
forall a. ToField a => a -> Action
PG.toField Identifier
key, Action
value]) [(Identifier, Action)]
keyValues))
                [Action] -> [Action] -> [Action]
forall a. Semigroup a => a -> a -> a
<> [UUID -> Action
forall a. ToField a => a -> Action
PG.toField UUID
id]

        [[Field]]
result :: [[Field]] <- Query -> [Action] -> IO [[Field]]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS (ByteString -> Query
PG.Query ByteString
query) [Action]
params

        Maybe [Field] -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson ([[Field]] -> Maybe [Field]
forall a. [a] -> Maybe a
head [[Field]]
result)

    -- DELETE /api/:table/:id
    action DeleteRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table, UUID
id :: UUID
$sel:id:CreateRecordAction :: ApiController -> UUID
id } = do
        (?modelContext::ModelContext) => Text -> IO TableWithRLS
Text -> IO TableWithRLS
ensureRLSEnabled Text
table

        Query -> (Identifier, UUID) -> IO Int64
forall parameters userId.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId) =>
Query -> parameters -> IO Int64
sqlExecWithRLS Query
"DELETE FROM ? WHERE id = ?" (Text -> Identifier
PG.Identifier Text
table, UUID
id)

        Bool -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson Bool
True

    -- GET /api/:table/:id
    action ShowRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table, UUID
id :: UUID
$sel:id:CreateRecordAction :: ApiController -> UUID
id } = do
        (?modelContext::ModelContext) => Text -> IO TableWithRLS
Text -> IO TableWithRLS
ensureRLSEnabled Text
table

        [[Field]]
result :: [[Field]] <- Query -> (Identifier, UUID) -> IO [[Field]]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS Query
"SELECT * FROM ? WHERE id = ?" (Text -> Identifier
PG.Identifier Text
table, UUID
id)

        Maybe [Field] -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson ([[Field]] -> Maybe [Field]
forall a. [a] -> Maybe a
head [[Field]]
result)

    -- GET /api/:table
    -- GET /api/:table?orderBy=createdAt
    -- GET /api/:table?fields=id,title
    action ListRecordsAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table } = do
        (?modelContext::ModelContext) => Text -> IO TableWithRLS
Text -> IO TableWithRLS
ensureRLSEnabled Text
table

        let (Query
theQuery, [Action]
theParams) = DynamicSQLQuery -> (Query, [Action])
compileQuery ((?context::ControllerContext) => Text -> DynamicSQLQuery
Text -> DynamicSQLQuery
buildDynamicQueryFromRequest Text
table)
        [[Field]]
result :: [[Field]] <- Query -> [Action] -> IO [[Field]]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS Query
theQuery [Action]
theParams

        [[Field]] -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson [[Field]]
result

    action ApiController
GraphQLQueryAction = do
        GraphQLRequest
graphQLRequest :: GraphQL.GraphQLRequest <- case Value -> Result GraphQLRequest
forall a. FromJSON a => Value -> Result a
fromJSON Value
(?context::ControllerContext) => Value
requestBodyJSON of
                Error String
errorMessage -> Text -> IO GraphQLRequest
forall a. Text -> a
error (String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs String
errorMessage)
                Data.Aeson.Success GraphQLRequest
value -> GraphQLRequest -> IO GraphQLRequest
forall (f :: * -> *) a. Applicative f => a -> f a
pure GraphQLRequest
value

        let [(Query
theQuery, [Action]
theParams)] = Variables -> Document -> [(Query, [Action])]
GraphQL.compileDocument (Proxy "variables" -> GraphQLRequest -> Variables
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "variables" (Proxy "variables")
Proxy "variables"
#variables GraphQLRequest
graphQLRequest) (Proxy "query" -> GraphQLRequest -> Document
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "query" (Proxy "query")
Proxy "query"
#query GraphQLRequest
graphQLRequest)

        [PG.Only UndecodedJSON
graphQLResult] <- Query -> [Action] -> IO [Only UndecodedJSON]
forall parameters userId result.
(?modelContext::ModelContext, ToRow parameters,
 ?context::ControllerContext,
 userId ~ Id' (GetTableName CurrentUserRecord),
 Show (PrimaryKey (GetTableName CurrentUserRecord)),
 HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord,
 ?context::ControllerContext,
 HasField
   "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord)),
 ToField userId, FromRow result) =>
Query -> parameters -> IO [result]
sqlQueryWithRLS Query
theQuery [Action]
theParams

        UndecodedJSON -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
json -> IO ()
renderJson (UndecodedJSON
graphQLResult :: UndecodedJSON)

buildDynamicQueryFromRequest :: Text -> DynamicSQLQuery
buildDynamicQueryFromRequest Text
table = DynamicSQLQuery :: Text
-> SelectedColumns
-> Maybe ConditionExpression
-> [OrderByClause]
-> Maybe ByteString
-> Maybe Int
-> Maybe Int
-> DynamicSQLQuery
DynamicSQLQuery
    { Text
$sel:table:DynamicSQLQuery :: Text
table :: Text
table
    , $sel:selectedColumns:DynamicSQLQuery :: SelectedColumns
selectedColumns = SelectedColumns -> ByteString -> SelectedColumns
forall a.
(?context::ControllerContext, ParamReader a) =>
a -> ByteString -> a
paramOrDefault SelectedColumns
SelectAll ByteString
"fields"
    , $sel:whereCondition:DynamicSQLQuery :: Maybe ConditionExpression
whereCondition = Maybe ConditionExpression
forall a. Maybe a
Nothing
    , $sel:orderByClause:DynamicSQLQuery :: [OrderByClause]
orderByClause = ByteString -> [OrderByClause]
forall valueType.
(?context::ControllerContext, NFData valueType,
 ParamReader valueType) =>
ByteString -> [valueType]
paramList ByteString
"orderBy"
    , $sel:distinctOnColumn:DynamicSQLQuery :: Maybe ByteString
distinctOnColumn = ByteString -> Maybe ByteString
forall paramType.
(?context::ControllerContext, ParamReader (Maybe paramType)) =>
ByteString -> Maybe paramType
paramOrNothing ByteString
"distinctOnColumn"
    , $sel:limit:DynamicSQLQuery :: Maybe Int
limit = ByteString -> Maybe Int
forall paramType.
(?context::ControllerContext, ParamReader (Maybe paramType)) =>
ByteString -> Maybe paramType
paramOrNothing ByteString
"limit"
    , $sel:offset:DynamicSQLQuery :: Maybe Int
offset = ByteString -> Maybe Int
forall paramType.
(?context::ControllerContext, ParamReader (Maybe paramType)) =>
ByteString -> Maybe paramType
paramOrNothing ByteString
"offset"
    }

instance ParamReader SelectedColumns where
    readParameter :: ByteString -> Either ByteString SelectedColumns
readParameter ByteString
byteString = SelectedColumns -> Either ByteString SelectedColumns
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SelectedColumns -> Either ByteString SelectedColumns)
-> SelectedColumns -> Either ByteString SelectedColumns
forall a b. (a -> b) -> a -> b
$
        ByteString
byteString
            ByteString -> (ByteString -> Text) -> Text
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs
            Text -> (Text -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Char -> Bool) -> Text -> [Text]
Text.split (\Char
char -> Char
char Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',')
            [Text] -> ([Text] -> SelectedColumns) -> SelectedColumns
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> [Text] -> SelectedColumns
SelectSpecific

instance ParamReader OrderByClause where
    readParameter :: ByteString -> Either ByteString OrderByClause
readParameter ByteString
byteString = case Char -> ByteString -> [ByteString]
ByteString.split Char
',' ByteString
byteString of
            [ByteString
orderByColumn, ByteString
order] -> do
                OrderByDirection
orderByDirection <- ByteString -> Either ByteString OrderByDirection
forall a a.
(Eq a, Semigroup a, IsString a, IsString a,
 ConvertibleStrings a a) =>
a -> Either a OrderByDirection
parseOrder ByteString
order
                OrderByClause -> Either ByteString OrderByClause
forall (f :: * -> *) a. Applicative f => a -> f a
pure OrderByClause :: ByteString -> OrderByDirection -> OrderByClause
OrderByClause { ByteString
$sel:orderByColumn:OrderByClause :: ByteString
orderByColumn :: ByteString
orderByColumn, OrderByDirection
$sel:orderByDirection:OrderByClause :: OrderByDirection
orderByDirection :: OrderByDirection
orderByDirection }
            [ByteString
orderByColumn] -> OrderByClause -> Either ByteString OrderByClause
forall (f :: * -> *) a. Applicative f => a -> f a
pure OrderByClause :: ByteString -> OrderByDirection -> OrderByClause
OrderByClause { ByteString
orderByColumn :: ByteString
$sel:orderByColumn:OrderByClause :: ByteString
orderByColumn, $sel:orderByDirection:OrderByClause :: OrderByDirection
orderByDirection = OrderByDirection
Asc }
        where
            parseOrder :: a -> Either a OrderByDirection
parseOrder a
"asc" = OrderByDirection -> Either a OrderByDirection
forall a b. b -> Either a b
Right OrderByDirection
Asc
            parseOrder a
"desc" = OrderByDirection -> Either a OrderByDirection
forall a b. b -> Either a b
Right OrderByDirection
Desc
            parseOrder a
otherwise = a -> Either a OrderByDirection
forall a b. a -> Either a b
Left (a
"Invalid order " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a -> a
forall a b. ConvertibleStrings a b => a -> b
cs a
otherwise)

instance ToJSON PG.SqlError where
    toJSON :: SqlError -> Value
toJSON PG.SqlError { ByteString
sqlState :: SqlError -> ByteString
sqlState :: ByteString
sqlState, ByteString
sqlErrorMsg :: SqlError -> ByteString
sqlErrorMsg :: ByteString
sqlErrorMsg, ByteString
sqlErrorDetail :: SqlError -> ByteString
sqlErrorDetail :: ByteString
sqlErrorDetail, ByteString
sqlErrorHint :: SqlError -> ByteString
sqlErrorHint :: ByteString
sqlErrorHint } = [Pair] -> Value
object
                [ Text
"state" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ((ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
sqlState) :: Text)
                , Text
"errorMsg" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ((ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
sqlErrorMsg) :: Text)
                , Text
"errorDetail" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ((ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
sqlErrorDetail) :: Text)
                , Text
"errorHint" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ((ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
sqlErrorHint) :: Text)
                ]
        where
            fieldValueToJSON :: DynamicValue -> Value
fieldValueToJSON (IntValue Int
value) = Int -> Value
forall a. ToJSON a => a -> Value
toJSON Int
value
            fieldValueToJSON (TextValue Text
value) = Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
value
            fieldValueToJSON (BoolValue Bool
value) = Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
value
            fieldValueToJSON (UUIDValue UUID
value) = UUID -> Value
forall a. ToJSON a => a -> Value
toJSON UUID
value
            fieldValueToJSON (DateTimeValue UTCTime
value) = UTCTime -> Value
forall a. ToJSON a => a -> Value
toJSON UTCTime
value

instance ToJSON EnhancedSqlError where
    toJSON :: EnhancedSqlError -> Value
toJSON EnhancedSqlError { SqlError
$sel:sqlError:EnhancedSqlError :: EnhancedSqlError -> SqlError
sqlError :: SqlError
sqlError } = SqlError -> Value
forall a. ToJSON a => a -> Value
toJSON SqlError
sqlError

renderErrorJson :: (?context :: ControllerContext) => Data.Aeson.ToJSON json => json -> IO ()
renderErrorJson :: json -> IO ()
renderErrorJson json
json = Status -> json -> IO ()
forall json.
(?context::ControllerContext, ToJSON json) =>
Status -> json -> IO ()
renderJsonWithStatusCode Status
status400 json
json
{-# INLINABLE renderErrorJson #-}

aesonValueToPostgresValue :: Value -> PG.Action
aesonValueToPostgresValue :: Value -> Action
aesonValueToPostgresValue (String Text
text) = Text -> Action
forall a. ToField a => a -> Action
PG.toField Text
text
aesonValueToPostgresValue (Bool Bool
value) = Bool -> Action
forall a. ToField a => a -> Action
PG.toField Bool
value
aesonValueToPostgresValue (Number Scientific
value) = case Scientific -> Either Double Integer
forall r i. (RealFloat r, Integral i) => Scientific -> Either r i
Scientific.floatingOrInteger Scientific
value of -- Hacky, we should make this function "Schema.sql"-aware in the future
    Left (Double
floating :: Double) -> Double -> Action
forall a. ToField a => a -> Action
PG.toField Double
floating
    Right (Integer
integer :: Integer) -> Integer -> Action
forall a. ToField a => a -> Action
PG.toField Integer
integer
aesonValueToPostgresValue Value
Data.Aeson.Null = Null -> Action
forall a. ToField a => a -> Action
PG.toField Null
PG.Null
aesonValueToPostgresValue object :: Value
object@(Object Object
values) =
    let
        tryDecodeAsPoint :: Maybe Point
        tryDecodeAsPoint :: Maybe Point
tryDecodeAsPoint = do
                Value
xValue <- Text -> Object -> Maybe Value
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
"x" Object
values
                Value
yValue <- Text -> Object -> Maybe Value
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
"y" Object
values
                Double
x <- case Value
xValue of
                        Number Scientific
number -> Double -> Maybe Double
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scientific -> Double
forall a. RealFloat a => Scientific -> a
Scientific.toRealFloat Scientific
number)
                        Value
otherwise -> Maybe Double
forall a. Maybe a
Nothing
                Double
y <- case Value
yValue of
                        Number Scientific
number -> Double -> Maybe Double
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scientific -> Double
forall a. RealFloat a => Scientific -> a
Scientific.toRealFloat Scientific
number)
                        Value
otherwise -> Maybe Double
forall a. Maybe a
Nothing
                Point -> Maybe Point
forall (f :: * -> *) a. Applicative f => a -> f a
pure Point :: Double -> Double -> Point
Point { Double
$sel:x:Point :: Double
x :: Double
x, Double
$sel:y:Point :: Double
y :: Double
y }
    in
        -- This is really hacky and is mostly duck typing. We should refactor this in the future to
        -- become more type aware by passing the DDL of the table to 'aesonValueToPostgresValue'.
        if Object -> Int
forall k v. HashMap k v -> Int
HashMap.size Object
values Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
            then Action -> Maybe Action -> Action
forall a. a -> Maybe a -> a
fromMaybe (Value -> Action
forall a. ToField a => a -> Action
PG.toField (Value -> Action) -> Value -> Action
forall a b. (a -> b) -> a -> b
$ Value -> Value
forall a. ToJSON a => a -> Value
toJSON Value
object) (Point -> Action
forall a. ToField a => a -> Action
PG.toField (Point -> Action) -> Maybe Point -> Maybe Action
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Point
tryDecodeAsPoint)
            else Value -> Action
forall a. ToField a => a -> Action
PG.toField (Value -> Value
forall a. ToJSON a => a -> Value
toJSON Value
object)


instance ToJSON GraphQLResult where
    toJSON :: GraphQLResult -> Value
toJSON GraphQLResult { Int
$sel:requestId:GraphQLResult :: GraphQLResult -> Int
requestId :: Int
requestId, UndecodedJSON
$sel:graphQLResult:GraphQLResult :: GraphQLResult -> UndecodedJSON
graphQLResult :: UndecodedJSON
graphQLResult } = [Pair] -> Value
object [ Text
"tag" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"GraphQLResult" :: Text), Text
"requestId" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Int
requestId, Text
"graphQLResult" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"" :: Text) ]
    toEncoding :: GraphQLResult -> Encoding
toEncoding GraphQLResult { Int
requestId :: Int
$sel:requestId:GraphQLResult :: GraphQLResult -> Int
requestId, UndecodedJSON
graphQLResult :: UndecodedJSON
$sel:graphQLResult:GraphQLResult :: GraphQLResult -> UndecodedJSON
graphQLResult } = [Encoding] -> Encoding
forall a. [Encoding' a] -> Encoding' a
Aeson.econcat
        [ Builder -> Encoding
forall a. Builder -> Encoding' a
Aeson.unsafeToEncoding Builder
"{\"tag\":\"GraphQLResult\",\"requestId\":"
        , Int -> Encoding
Aeson.int Int
requestId
        , Builder -> Encoding
forall a. Builder -> Encoding' a
Aeson.unsafeToEncoding Builder
",\"graphQLResult\":"
        , UndecodedJSON -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding UndecodedJSON
graphQLResult
        , Builder -> Encoding
forall a. Builder -> Encoding' a
Aeson.unsafeToEncoding Builder
"}"
        ]
instance ToJSON UndecodedJSON where
    toJSON :: UndecodedJSON -> Value
toJSON (UndecodedJSON ByteString
_) = Text -> Value
forall a. Text -> a
error Text
"Not implemented"
    toEncoding :: UndecodedJSON -> Encoding
toEncoding (UndecodedJSON ByteString
json) = Builder -> Encoding
forall a. Builder -> Encoding' a
Aeson.unsafeToEncoding (ByteString -> Builder
ByteString.byteString ByteString
json)