#if defined(__GLASGOW_HASKELL__) && (__GLASGOW_HASKELL__ >= 702)
#endif
module Data.IterIO.Http.Support.RestController (
RestController(..)
, routeRestController
) where
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import Data.IterIO.Http.Support.Action
import Data.IterIO.Http.Support.Routing
import Data.IterIO.Http.Support.Responses
import Data.Monoid
class Monad m => RestController t b m a where
restIndex :: a -> Action t b m ()
restIndex _ = respond404
restShow :: a -> L.ByteString -> Action t b m ()
restShow _ _ = respond404
restNew :: a -> Action t b m ()
restNew _ = respond404
restCreate :: a -> Action t b m ()
restCreate _ = respond404
restEdit :: a -> L.ByteString -> Action t b m ()
restEdit _ _ = respond404
restUpdate :: a -> L.ByteString -> Action t b m ()
restUpdate _ _ = respond404
restDestroy :: a -> L.ByteString -> Action t b m ()
restDestroy _ _ = respond404
runWithVar :: Monad m => S.ByteString -> (L.ByteString -> Action t b m ()) -> Action t b m ()
runWithVar varName controller = do
(Just var) <- param varName
controller $ paramValue var
routeRestController :: RestController t b m a => String -> a -> ActionRoute b m t
routeRestController prefix controller = routePattern prefix $ mconcat [
routeTop $ routeMethod "GET" $ routeAction $ restIndex controller
, routeTop $ routeMethod "POST" $ routeAction $ restCreate controller
, routeMethod "GET" $ routePattern "/new" $ routeAction $ restNew controller
, routeMethod "GET" $ routePattern "/:id/edit" $ routeAction $ runWithVar "id" $ restEdit controller
, routeMethod "GET" $ routePattern "/:id" $ routeAction $ runWithVar "id" $ restShow controller
, routeMethod "DELETE" $ routePattern "/:id" $ routeAction $ runWithVar "id" $ restDestroy controller
, routeMethod "PUT" $ routePattern "/:id" $ routeAction $ runWithVar "id" $ restUpdate controller
, routeMethod "POST" $ routePattern "/:id" $ routeAction $ runWithVar "id" $ restUpdate controller
]