packages feed

RESTng-0.1: Example/Resource/Author.hs

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# OPTIONS_GHC -fallow-incoherent-instances #-}

module Resource.Author where

import Prelude hiding (div,span)
import Database.HDBC (toSql)
import Text.ParserCombinators.Parsec (parse)

import Text.CxML
import Text.YuiGrid

import RESTng.System

data Author = Author {
      author_id :: Integer,
      name :: String,
      age :: Integer
    }

authorProxy :: Proxy Author
authorProxy = undefined


instance Resource Author where
  resourceType _ = "Author"
  key = author_id
  setKey res k = res {author_id = k}

  userFields _ = ["name", "age"]


instance RelationalResource Author where

  userFieldsToSql res = [toSql $ name res, toSql $ age res]

  sqlUserFieldsParser (k, _) =
                   do
                     (name, age) <- sqlRecordParser
                     return (Author k name age)

instance CanCreateSchemaForResource Author where
   --userFieldsReps :: Proxy a -> [TypeRep]
     userFieldsReps _ = [stringTR, integerTR]
   --userFieldsDefaultValues :: Proxy a -> [Maybe String]   -- default value sql expression
     userFieldsDefaultValues _ = [Nothing, Nothing]

instance PersistableResource Author where
  persistableFunctions = persistableFromRelational

instance WebResource Author where
  userFieldValues res = [showField $ name res, showField $ age res]

  userFieldValuesParser (k, _) = 
                   do
                     name <- parseNotEmpty "name"
                     age <- parseField "age"
                     return (Author k name age)

{-
  showHtml (Author k name age) =
                                                           h3 /- [t name] 
                                                           +++ h4 /- [t $ "Age: " ++ show age]
-}
  showShortHtml = t . name


instance InGridResource Author where
  showLayout a b c = map (addCss ("padded", [("padding","3px")])) (showLayoutDefault a b c)
  listView = listInBoxesView