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

module SpaceTraders.Database.Ships
  ( getShips
  , setShip
  ) where

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

import           SpaceTraders
import           SpaceTraders.Model.Ship
import           SpaceTraders.Utils

getShips :: (HasDatabaseConn env, MonadIO m, MonadReader env m) => m [Ship]
getShips = query_ "SELECT data FROM ships;"

setShip :: (HasDatabaseConn env, MonadError e m, MonadIO m, MonadReader env m) => Ship -> m ()
setShip ship = updateShip `catchError` addShip
  where
    addShip _ = execute "INSERT INTO ships(data) VALUES (json(?));" (S.Only $ encode ship)
    updateShip = execute "UPDATE ships SET data = json(?) WHERE data->>'symbol' = ?;" (encode ship, symbol ship)