Advertisement
Guest User

Untitled

a guest
May 31st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE GADTs, TypeFamilies, TemplateHaskell, QuasiQuotes, FlexibleInstances, DeriveGeneric, StandaloneDeriving, CPP #-}
  2.  
  3.  
  4. module Repository
  5.  
  6.     (Person, Account, validateUser, insertEntity)
  7.  
  8. where
  9.  
  10. import Database.Groundhog.Core
  11. import Database.Groundhog.Generic
  12. import Database.Groundhog.Postgresql
  13. import Database.Groundhog.TH
  14. import Database.Groundhog (select)
  15. import Control.Monad.Reader
  16. import Data.Pool
  17. import GHC.Generics
  18. import Data.UUID (UUID, fromText, toText)
  19. import Data.UUID.V4
  20. import Data.Time.Calendar
  21. import Crypto.Hash.MD5
  22. import Data.ByteString (ByteString)
  23. import Data.ByteString.Char8 (pack, unpack)
  24. import Data.ByteString.Base64 (encode)
  25. import Data.Aeson (FromJSON, ToJSON, parseJSON, toJSON, toEncoding, withText, Object)
  26. import Web.PathPieces (PathPiece, fromPathPiece, toPathPiece)
  27. import Data.Maybe (fromMaybe)
  28. import Control.Lens
  29.  
  30. instance PathPiece UUID where
  31.     fromPathPiece = fromText
  32.     toPathPiece = toText
  33.  
  34. #if MIN_VERSION_aeson(1,1,1)
  35.  
  36. #else
  37.  
  38. instance FromJSON UUID where
  39.     parseJSON = withText "UUID" $
  40.         maybe (fail "Invalid UUID") pure . fromText
  41.  
  42. instance ToJSON UUID where
  43.     toJSON = toJSON . toText
  44.  
  45. #endif
  46.  
  47. instance PrimitivePersistField UUID where
  48.   toPrimitivePersistValue uuid = toPrimitivePersistValue $ show uuid
  49.   fromPrimitivePersistValue a = uuid where
  50.     uuid = read $ fromPrimitivePersistValue a
  51.  
  52. instance PersistField UUID where
  53.   persistName _ = "UUID"
  54.   toPersistValues = primToPersistValue
  55.   fromPersistValues = primFromPersistValue
  56.   dbType _ _ = DbTypePrimitive (DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
  57.  
  58. -- These two instances of superclasses are useful but not necessary. They are like Functor and Applicative instances when you implement a Monad.
  59. instance SinglePersistField UUID where
  60.   toSinglePersistValue = primToSinglePersistValue
  61.   fromSinglePersistValue = primFromSinglePersistValue
  62.  
  63. instance PurePersistField UUID where
  64.   toPurePersistValues = primToPurePersistValues
  65.   fromPurePersistValues = primFromPurePersistValues
  66.  
  67.  
  68. data Person = Person {
  69.     personId :: UUID,
  70.     firstName :: String,
  71.     lastName  :: String,
  72.     phoneNumber :: String,
  73.     mobileNumber :: String,
  74.     mobileNumber2 :: String,
  75.     birthDate :: Day,
  76. --     image :: ByteString,
  77.     nationalCode :: String,
  78.     postalCode :: String,
  79.     address :: String
  80.     } deriving (Show, Generic)
  81.  
  82.  
  83. data Account  = Account {
  84.     accountId :: UUID,
  85.     cityId :: Maybe Int,
  86.     userTypeId :: Maybe Int,
  87.     userName :: String,
  88.     password :: String,
  89.     email :: Maybe String,
  90.     pushToken :: Maybe String,
  91.     active :: Maybe Bool,
  92.     registerDate :: Maybe Day,
  93.     verificationCode :: Maybe String,
  94.     verified :: Bool,
  95.     referrerId :: UUID,
  96.     person :: DefaultKey Person
  97.     } deriving Generic
  98.  
  99. deriving instance Show Account
  100.  
  101.  
  102. data Vehicle = Vehicle {
  103.     vehicleId :: UUID,
  104.     vehicleCapacity :: Int,
  105.     vehicleModel :: String,
  106.     plateNumber :: String,
  107.     imei :: String,
  108.     rate :: Double,
  109.     imei2 :: String,
  110.     workHours :: String,
  111. --     bankAccountInfo :: Object,
  112.     appVersion :: Int,
  113.     account :: DefaultKey Account
  114.  
  115. }
  116.  
  117.  
  118. deriving instance Show Vehicle
  119.  
  120. instance FromJSON Person
  121. instance ToJSON Person
  122.  
  123.  
  124. mkPersist defaultCodegenConfig {namingStyle=persistentNamingStyle {mkDbEntityName = toUnderscore, mkDbFieldName = \_ _ _ dbName _-> toUnderscore dbName}} [groundhog|
  125. definitions:
  126.   - entity: Person
  127.     autoKey: null
  128.  
  129.     constructors:
  130.       - name: Person
  131.         uniques:
  132.           - name: PersonIdPK
  133.             type: primary
  134.             fields: [personId]
  135.         fields:
  136.           - name: firstName
  137.             type: varchar(100)
  138.           - name: lastName
  139.             type: varchar(100)
  140.           - name: phoneNumber
  141.             type: varchar(15)
  142.           - name: mobileNumber
  143.             type: varchar(15)
  144.           - name: mobileNumber2
  145.             type: varchar(15)
  146.           - name: birthDate
  147.             type: date
  148. #          - name: image
  149. #            type: bytea
  150.           - name: nationalCode
  151.             type: varchar(10)
  152.           - name: postalCode
  153.             type: varchar(10)
  154.           - name: address
  155.             type: varchar(250)
  156.  
  157.  
  158.   - entity: Account
  159.     autoKey: null
  160.  
  161.     constructors:
  162.       - name: Account
  163.         uniques:
  164.           - name: AccountIdPK
  165.             type: primary
  166.             fields: [accountId]
  167.         fields:
  168.           - name: cityId
  169.             type: int
  170.           - name: userTypeId
  171.             type: int
  172.           - name: userName
  173.             type: varchar(50)
  174.           - name: password
  175.             type: varchar(50)
  176.           - name: email
  177.             type: varchar(50)
  178.           - name: pushToken
  179.             type: varchar(50)
  180.           - name: active
  181.             type: boolean
  182.           - name: registerDate
  183.             type: date
  184.           - name: verificationCode
  185.             type: varchar(10)
  186.           - name: verified
  187.             type: boolean
  188.           - name: referrerId
  189.             type: uuid
  190.  
  191.  
  192.  
  193.   - entity: Vehicle
  194.     autoKey: null
  195.  
  196.     constructors:
  197.       - name: Vehicle
  198.         uniques:
  199.             - name: VehicleIdPk
  200.               type: primary
  201.               fields: [vehicleId]
  202.  
  203.         fields:
  204.             - name: vehicleCapacity
  205.               type: smallint
  206.             - name: vehicleModel
  207.               type: varchar(100)
  208.             - name: plateNumber
  209.               type: varchar(20)
  210.             - name: imei
  211.               type: varchar(20)
  212.             - name: rate
  213.               type: decimal
  214.             - name: imei2
  215.               type: varchar(20)
  216.             - name: workHours
  217.               type: varchar(100)
  218. #            - name: bankAccountInfo
  219. #              type: json
  220.             - name: appVersion
  221.               type: int
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. |]
  229.  
  230. validateUser :: Pool Postgresql -> String -> String -> IO Bool
  231.  
  232. validateUser pool username password = do
  233.     x <- flip runDbConn pool $ select (AccountUserName ==. username &&. AccountPassword ==. unpack (encode $ hash (pack password)))
  234.     return $ null x
  235.  
  236.  
  237.  
  238. insertEntity ::  (PersistEntity val) => Pool Postgresql -> val -> IO (AutoKey val)
  239.  
  240. insertEntity pool e = flip runDbConn pool $ insert e
  241.  
  242.  
  243.  
  244. addVehicle :: Pool Postgresql -> Vehicle -> IO (AutoKey Vehicle)
  245.  
  246. addVehicle pool v = do
  247.     pid <- nextRandom
  248.     aid <- nextRandom
  249.  
  250.     let v = v {account = (account v) {accountId = aid} }
  251.     let v = v & account.accountId .~ aid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement