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/Payment.hs |
Setting up Scotty, Persistent, Clay, Blaze, Esqueleto, Elm
Diffstat (limited to 'src/server/Model/Payment.hs')
-rw-r--r-- | src/server/Model/Payment.hs | 36 |
1 files changed, 36 insertions, 0 deletions
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 |