1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | module Repos.Users ( register , byUsername , bySession , bySession' ) where import Database.SQLite.Simple (Only(..), Connection) import Database.SQLite.Simple.QQ import qualified Database.SQLite.Simple as SQL import Models import Data.Text import Utils import Repos.Utils register :: (CanDB m) => User -> m DbR register user = execute [sql| insert into users(id, username, hash_pass, created_at) values (?, ?, ?, ?) |] user byUsername :: (CanDB m) => Text -> m (Maybe UserAuth) byUsername username = toMaybe <$> query [sql| select id, hash_pass from users where username = (?) |] (Only username) bySessionQuery :: SQL.Query bySessionQuery = [sql| select u.id, u.username, u.hash_pass, ifnull(sub.active, false), u.created_at from users u join sessions s on u.id = s.user_id left join subscriptions sub on u.id = sub.user_id where s.token = (?) |] bySession :: (CanDB m) => Text -> m (Maybe User) bySession token = do toMaybe <$> query bySessionQuery (Only token) bySession' :: Connection -> Text -> IO (Maybe User) bySession' conn token = do users :: [User] <- SQL.query conn bySessionQuery (Only token) return $ toMaybe users |