summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database/Systems.hs
blob: 7c43caf5ecb19093e4c53addac107df83286027f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-# LANGUAGE OverloadedStrings #-}

module SpaceTraders.Database.Systems
  ( addSystems
  , getSystems
  ) where

import           Control.Monad.Reader
import           Data.Aeson
import qualified Database.SQLite.Simple    as S

import           SpaceTraders
import           SpaceTraders.Model.System
import           SpaceTraders.Utils

addSystems :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => [System] -> m ()
addSystems systems = do
  env <- ask
  let conn = getConn env
  liftIO $ S.withTransaction conn $ S.executeMany conn "INSERT INTO systems(data) VALUES (json(?));" $ S.Only <$> map encode systems

getSystems :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => m [System]
getSystems = query_ "SELECT data FROM systems;"