module IHP.DataSync.REST.Routes where

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

instance CanRoute ApiController where
    parseRoute' :: (?context::RequestContext) => Parser ApiController
parseRoute' = do
        ByteString -> Parser ByteString
string ByteString
"/api/"

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

            createRecordAction :: Text -> Parser ApiController
createRecordAction Text
table = do
                forall t. Chunk t => Parser t ()
endOfInput
                (?context::RequestContext) => [StdMethod] -> Parser ()
onlyAllowMethods [StdMethod
POST]
                forall (f :: * -> *) a. Applicative f => a -> f a
pure 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

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

                StdMethod
method <- (?context::RequestContext) => Parser StdMethod
getMethod
                case StdMethod
method of
                    StdMethod
PATCH  -> forall (f :: * -> *) a. Applicative f => a -> f a
pure UpdateRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table, UUID
$sel:id:CreateRecordAction :: UUID
id :: UUID
id }
                    StdMethod
GET    -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ShowRecordAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table, UUID
$sel:id:CreateRecordAction :: UUID
id :: UUID
id }
                    StdMethod
DELETE -> forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
                forall t. Chunk t => Parser t ()
endOfInput
                StdMethod
method <- (?context::RequestContext) => Parser StdMethod
getMethod
                case StdMethod
method of
                    StdMethod
GET -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ListRecordsAction { Text
table :: Text
$sel:table:CreateRecordAction :: Text
table }

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

        Parser ApiController
graphQLQueryAction 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/" 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/" forall a. Semigroup a => a -> a -> a
<> Text
table forall a. Semigroup a => a -> a -> a
<> Text
"/" forall a. Semigroup a => a -> a -> a
<> 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/" forall a. Semigroup a => a -> a -> a
<> Text
table forall a. Semigroup a => a -> a -> a
<> Text
"/" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> Text
tshow UUID
id
    pathTo ApiController
GraphQLQueryAction = Text
"/api/graphql"