From 24f6c8eb90555b81a96b142fec8057b05d334035 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 20 Jul 2023 00:24:31 +0200 Subject: [haskell] Finalized the agent initialization, refresh and reset --- haskell/src/SpaceTraders/Database/Agents.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (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 b34ca04..0557b1d 100644 --- a/haskell/src/SpaceTraders/Database/Agents.hs +++ b/haskell/src/SpaceTraders/Database/Agents.hs @@ -2,16 +2,31 @@ module SpaceTraders.Database.Agents ( addAgent + , getAgent + , setAgent ) where import Control.Monad.Reader import Data.Aeson +import Data.Maybe import qualified Database.SQLite.Simple as S import SpaceTraders 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)) + +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 + +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)) -- cgit v1.2.3