module IHP.DataSync.REST.Routes where

import IHP.RouterPrelude
import IHP.DataSync.REST.Types

instance CanRoute ApiController where
    parseRoute' :: Parser ApiController
parseRoute' = do
        ByteString -> Parser ByteString
string ByteString
"/api/"

        let
            graphQLQueryAction :: Parser ApiController
graphQLQueryAction = do
                ByteString -> Parser ByteString
string ByteString
"graphql"
                Parser ByteString ()
forall t. Chunk t => Parser t ()
endOfInput
                (?context::RequestContext) => [StdMethod] -> Parser ByteString ()
[StdMethod] -> Parser ByteString ()
onlyAllowMethods [StdMethod
POST]
                ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure ApiController
GraphQLQueryAction

            createRecordAction :: Text -> Parser ApiController
createRecordAction Text
table = do
                Parser ByteString ()
forall t. Chunk t => Parser t ()
endOfInput
                (?context::RequestContext) => [StdMethod] -> Parser ByteString ()
[StdMethod] -> Parser ByteString ()
onlyAllowMethods [StdMethod
POST]
                ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure CreateRecordAction :: Text -> ApiController
CreateRecordAction { Text
$sel:table:CreateRecordAction :: Text
table :: Text
table }

            updateOrDeleteRecordAction :: Text -> Parser ApiController
updateOrDeleteRecordAction Text
table = do
                ByteString -> Parser ByteString
string ByteString
"/"
                UUID
id <- Parser UUID
parseUUID

                Parser ByteString ()
forall t. Chunk t => Parser t ()
endOfInput

                StdMethod
method <- Parser StdMethod
(?context::RequestContext) => Parser StdMethod
getMethod
                case StdMethod
method of
                    StdMethod
PATCH  -> ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure UpdateRecordAction :: Text -> UUID -> ApiController
UpdateRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table, UUID
$sel:id:CreateRecordAction :: UUID
id :: UUID
id }
                    StdMethod
GET    -> ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure ShowRecordAction :: Text -> UUID -> ApiController
ShowRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table, UUID
$sel:id:CreateRecordAction :: UUID
id :: UUID
id }
                    StdMethod
DELETE -> ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure DeleteRecordAction :: Text -> UUID -> ApiController
DeleteRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table, UUID
$sel:id:CreateRecordAction :: UUID
id :: UUID
id }

            listRecordsAction :: Text -> Parser ApiController
listRecordsAction Text
table = do
                Parser ByteString ()
forall t. Chunk t => Parser t ()
endOfInput
                StdMethod
method <- Parser StdMethod
(?context::RequestContext) => Parser StdMethod
getMethod
                case StdMethod
method of
                    StdMethod
GET -> ApiController -> Parser ApiController
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListRecordsAction :: Text -> ApiController
ListRecordsAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table }

            crud :: Parser ApiController
crud = do
                Text
table <- Parser Text
parseText
                (?context::RequestContext) => Text -> Parser ApiController
Text -> Parser ApiController
updateOrDeleteRecordAction Text
table Parser ApiController
-> Parser ApiController -> Parser ApiController
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (?context::RequestContext) => Text -> Parser ApiController
Text -> Parser ApiController
createRecordAction Text
table Parser ApiController
-> Parser ApiController -> Parser ApiController
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (?context::RequestContext) => Text -> Parser ApiController
Text -> Parser ApiController
listRecordsAction Text
table

        Parser ApiController
graphQLQueryAction Parser ApiController
-> Parser ApiController -> Parser ApiController
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ApiController
crud

instance HasPath ApiController where
    pathTo :: ApiController -> Text
pathTo CreateRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table } = Text
"/api/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
table
    pathTo UpdateRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table, UUID
id :: UUID
$sel:id:CreateRecordAction :: ApiController -> UUID
id } = Text
"/api/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
table Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> UUID -> Text
forall a. Show a => a -> Text
tshow UUID
id
    pathTo DeleteRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: ApiController -> Text
table, UUID
id :: UUID
$sel:id:CreateRecordAction :: ApiController -> UUID
id } = Text
"/api/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
table Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> UUID -> Text
forall a. Show a => a -> Text
tshow UUID
id
    pathTo ApiController
GraphQLQueryAction = Text
"/api/graphql"