summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-11 21:55:55 +0200
committerJulien Dessaux2023-07-11 21:56:35 +0200
commit7bd1c116c26d0c2147aa787b04f6e7de85d44133 (patch)
tree8d831b852eeb37a2d8e4c09062c0ed7c9b441d19 /haskell/src/SpaceTraders/Database
parent[haskell] Refactored JSON parsing code (diff)
downloadspacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.tar.gz
spacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.tar.bz2
spacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.zip
[haskell] Implemented pagination and systems list api call
Diffstat (limited to 'haskell/src/SpaceTraders/Database')
-rw-r--r--haskell/src/SpaceTraders/Database/Systems.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/haskell/src/SpaceTraders/Database/Systems.hs b/haskell/src/SpaceTraders/Database/Systems.hs
new file mode 100644
index 0000000..5312fd0
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/Systems.hs
@@ -0,0 +1,23 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module SpaceTraders.Database.Systems
+ ( addSystems
+ , getSystems
+ ) where
+
+import Data.Aeson
+import Data.Maybe
+import qualified Database.SQLite.Simple as S
+
+import Data.Text.Encoding (encodeUtf8Builder)
+import Data.ByteString.Builder(toLazyByteString)
+
+import SpaceTraders.Model.System
+
+addSystems :: S.Connection -> [System] -> IO ()
+addSystems conn systems = S.withTransaction conn $ S.executeMany conn "INSERT INTO systems(data) VALUES (json(?));" $ S.Only <$> map encode systems
+
+getSystems :: S.Connection -> IO [System]
+getSystems conn = do
+ ret <- S.query_ conn "SELECT data from systems;"
+ return . catMaybes $ map (decode . toLazyByteString . encodeUtf8Builder . head) ret