From 7bd1c116c26d0c2147aa787b04f6e7de85d44133 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 11 Jul 2023 21:55:55 +0200 Subject: [haskell] Implemented pagination and systems list api call --- haskell/src/SpaceTraders/Database/Systems.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 haskell/src/SpaceTraders/Database/Systems.hs (limited to 'haskell/src/SpaceTraders/Database/Systems.hs') 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 -- cgit v1.2.3