diff options
author | Joris Guyonvarch | 2015-07-06 00:16:45 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2015-07-06 00:16:45 +0200 |
commit | 4ce9751c9e645916fdde71874c2cdadd252f32a0 (patch) | |
tree | 1014c58787231cbdc3ae2799f32127b40ab393ab /src/server/Model |
Setting up Scotty, Persistent, Clay, Blaze, Esqueleto, Elm
Diffstat (limited to 'src/server/Model')
-rw-r--r-- | src/server/Model/Database.hs | 44 | ||||
-rw-r--r-- | src/server/Model/Json/Payment.hs | 21 | ||||
-rw-r--r-- | src/server/Model/Payment.hs | 36 | ||||
-rw-r--r-- | src/server/Model/User.hs | 30 |
4 files changed, 131 insertions, 0 deletions
diff --git a/src/server/Model/Database.hs b/src/server/Model/Database.hs new file mode 100644 index 0000000..abf235d --- /dev/null +++ b/src/server/Model/Database.hs @@ -0,0 +1,44 @@ +{-# LANGUAGE EmptyDataDecls #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeFamilies #-} + +module Model.Database where + +import Control.Monad.Logger (NoLoggingT, runNoLoggingT) +import Control.Monad.Trans.Resource (runResourceT, ResourceT) + +import Data.Text +import Data.Time.Clock (UTCTime) + +import Database.Persist.Sqlite +import Database.Persist.TH + +share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| +User + creation UTCTime + email Text + name Text + EmailKey email + deriving Show +Payment + userId UserId + creation UTCTime + name Text + cost Int + deriving Show +|] + +type Persist a = SqlPersistT (ResourceT (NoLoggingT IO)) a + +runDb :: Persist a -> IO a +runDb = runNoLoggingT . runResourceT . withSqliteConn "database" . runSqlConn + +runMigrations :: IO () +runMigrations = runDb $ runMigration migrateAll diff --git a/src/server/Model/Json/Payment.hs b/src/server/Model/Json/Payment.hs new file mode 100644 index 0000000..de6beb9 --- /dev/null +++ b/src/server/Model/Json/Payment.hs @@ -0,0 +1,21 @@ +{-# LANGUAGE DeriveGeneric #-} + +module Model.Json.Payment + ( Payment(..) + ) where + +import GHC.Generics + +import Data.Time +import Data.Text (Text) +import Data.Aeson + +data Payment = Payment + { creation :: UTCTime + , name :: Text + , cost :: Int + , userName :: Text + } deriving (Show, Generic) + +instance FromJSON Payment +instance ToJSON Payment diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs new file mode 100644 index 0000000..b35e13c --- /dev/null +++ b/src/server/Model/Payment.hs @@ -0,0 +1,36 @@ +module Model.Payment + ( getPayments + , insertPayment + ) where + +import Data.Text (Text) +import Data.Time.Clock (getCurrentTime) + +import Control.Monad.IO.Class (liftIO) + +import Database.Persist +import Database.Esqueleto +import qualified Database.Esqueleto as E + +import Model.Database +import qualified Model.Json.Payment as P + +getPayments :: Persist [P.Payment] +getPayments = do + xs <- select $ + from $ \(payment `InnerJoin` user) -> do + on (payment ^. PaymentUserId E.==. user ^. UserId) + return (payment, user) + return (map getJsonPayment xs) + +getJsonPayment :: (Entity Payment, Entity User) -> P.Payment +getJsonPayment (paymentEntity, userEntity) = + let payment = entityVal paymentEntity + user = entityVal userEntity + in P.Payment (paymentCreation payment) (paymentName payment) (paymentCost payment) (userName user) + + +insertPayment :: UserId -> Text -> Int -> Persist PaymentId +insertPayment userId name cost = do + now <- liftIO getCurrentTime + insert $ Payment userId now name cost diff --git a/src/server/Model/User.hs b/src/server/Model/User.hs new file mode 100644 index 0000000..ddca0fb --- /dev/null +++ b/src/server/Model/User.hs @@ -0,0 +1,30 @@ +module Model.User + ( getUsers + , getUser + , insertUser + , deleteUser + ) where + +import Data.Text (Text) +import Data.Time.Clock (getCurrentTime) + +import Control.Monad.IO.Class (liftIO) + +import Database.Persist + +import Model.Database + +getUsers :: Persist [User] +getUsers = map entityVal <$> selectList [] [Desc UserCreation] + +getUser :: Text -> Persist (Maybe (Entity User)) +getUser email = selectFirst [UserEmail ==. email] [] + +insertUser :: Text -> Text -> Persist UserId +insertUser email name = do + now <- liftIO getCurrentTime + insert $ User now email name + +deleteUser :: Text -> Persist () +deleteUser email = + deleteWhere [UserEmail ==. email] |