From dcd0a7a9b2612f383c1f627c72c27868c367058d Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 21 Jul 2023 00:01:02 +0200 Subject: [haskell] abstracted away common database access patterns --- haskell/src/SpaceTraders/Database/Agents.hs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'haskell/src/SpaceTraders/Database/Agents.hs') diff --git a/haskell/src/SpaceTraders/Database/Agents.hs b/haskell/src/SpaceTraders/Database/Agents.hs index 0557b1d..49ae11f 100644 --- a/haskell/src/SpaceTraders/Database/Agents.hs +++ b/haskell/src/SpaceTraders/Database/Agents.hs @@ -8,7 +8,6 @@ module SpaceTraders.Database.Agents import Control.Monad.Reader import Data.Aeson -import Data.Maybe import qualified Database.SQLite.Simple as S import SpaceTraders @@ -16,17 +15,10 @@ import SpaceTraders.Model.Agent import SpaceTraders.Utils addAgent :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => Agent -> m () -addAgent agent = do - env <- ask - liftIO $ S.execute (getConn env) "INSERT INTO agents(data) VALUES (json(?));" (S.Only (encode agent)) +addAgent agent = execute "INSERT INTO agents(data) VALUES (json(?));" (S.Only (encode agent)) getAgent :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => m Agent -getAgent = do - env <- ask - ret <- liftIO $ S.query_ (getConn env) "SELECT data FROM agents;" - return . head . catMaybes $ map (decodeText . head) ret +getAgent = one_ "SELECT data FROM agents"; setAgent :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => Agent -> m () -setAgent agent = do - env <- ask - liftIO $ S.execute (getConn env) "UPDATE agents SET data = json(?);" (S.Only (encode agent)) +setAgent agent = execute "UPDATE agents SET data = json(?);" (S.Only (encode agent)) -- cgit v1.2.3