summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database/Tokens.hs
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-18 00:02:33 +0200
committerJulien Dessaux2023-07-18 00:02:33 +0200
commit4af96da5670566c111fa5c7dac9572eaac021a4b (patch)
tree74ae00fb63e087437bc3a62152d4b7fecf8c039e /haskell/src/SpaceTraders/Database/Tokens.hs
parent[haskell] Trigger a program restart when the api client encounters a server r... (diff)
downloadspacetraders-4af96da5670566c111fa5c7dac9572eaac021a4b.tar.gz
spacetraders-4af96da5670566c111fa5c7dac9572eaac021a4b.tar.bz2
spacetraders-4af96da5670566c111fa5c7dac9572eaac021a4b.zip
[haskell] Refactored everything with a ReaderT pattern
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/Database/Tokens.hs22
1 files changed, 14 insertions, 8 deletions
diff --git a/haskell/src/SpaceTraders/Database/Tokens.hs b/haskell/src/SpaceTraders/Database/Tokens.hs
index b907609..e99e49e 100644
--- a/haskell/src/SpaceTraders/Database/Tokens.hs
+++ b/haskell/src/SpaceTraders/Database/Tokens.hs
@@ -2,17 +2,23 @@
{-# LANGUAGE QuasiQuotes #-}
module SpaceTraders.Database.Tokens
- ( getToken
- , setToken
+ ( addToken
+ , getToken
) where
+import Control.Monad.Reader
import qualified Database.SQLite.Simple as S
import qualified Data.Text as T
-getToken :: S.Connection -> IO (T.Text)
-getToken conn = do
- [[token]] <- S.query_ conn "SELECT data FROM tokens;"
- return token
+import SpaceTraders
+
+addToken :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => T.Text -> m ()
+addToken value = do
+ env <- ask
+ liftIO $ S.execute (getConn env) "INSERT INTO tokens(data) VALUES (?);" (S.Only value)
-setToken :: S.Connection -> T.Text -> IO ()
-setToken conn value = S.execute conn "INSERT INTO tokens(data) VALUES (?);" (S.Only value)
+getToken :: (HasDatabaseConn env, MonadFail m, MonadIO m, MonadReader env m) => m T.Text
+getToken = do
+ env <- ask
+ [[token]] <- liftIO $ S.query_ (getConn env) "SELECT data FROM tokens;"
+ return token