diff options
author | Julien Dessaux | 2023-07-11 21:55:55 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-11 21:56:35 +0200 |
commit | 7bd1c116c26d0c2147aa787b04f6e7de85d44133 (patch) | |
tree | 8d831b852eeb37a2d8e4c09062c0ed7c9b441d19 /haskell/src/SpaceTraders/Database | |
parent | [haskell] Refactored JSON parsing code (diff) | |
download | spacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.tar.gz spacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.tar.bz2 spacetraders-7bd1c116c26d0c2147aa787b04f6e7de85d44133.zip |
[haskell] Implemented pagination and systems list api call
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/Database.hs | 7 | ||||
-rw-r--r-- | haskell/src/SpaceTraders/Database/Systems.hs | 23 |
2 files changed, 29 insertions, 1 deletions
diff --git a/haskell/src/SpaceTraders/Database.hs b/haskell/src/SpaceTraders/Database.hs index ac3aa7b..4fffb07 100644 --- a/haskell/src/SpaceTraders/Database.hs +++ b/haskell/src/SpaceTraders/Database.hs @@ -31,7 +31,12 @@ migrations = [ id INTEGER PRIMARY KEY, data TEXT NOT NULL );|], - [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|]] + [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|], + [r|CREATE TABLE systems ( + id INTEGER PRIMARY KEY, + data TEXT NOT NULL + );|], + [r|CREATE UNIQUE INDEX systems_data_symbol ON systems (json_extract(data, '$.symbol'));|]] close :: S.Connection -> IO () close conn = S.close conn 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 |