souplesse/lib/Store.hs

74 lines
1.9 KiB
Haskell
Raw Normal View History

2024-11-04 23:37:48 +00:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
2024-11-05 23:32:01 +00:00
module Store (save, fetch, migrateAll) where
2024-11-04 23:37:48 +00:00
2024-11-05 23:32:01 +00:00
import Control.Monad.IO.Class (MonadIO, liftIO)
2024-11-04 23:37:48 +00:00
import Control.Monad.Trans.Reader (ReaderT)
import Data.Time.Clock (UTCTime)
import Database.Persist
2024-11-05 23:32:01 +00:00
import Database.Persist.Class
2024-11-04 23:37:48 +00:00
import Database.Persist.Postgresql
( ConnectionString,
SqlBackend,
createPostgresqlPool,
pgConnStr,
pgPoolSize,
runMigration,
runSqlPool,
)
import Database.Persist.TH
2024-11-05 23:32:01 +00:00
import Track (Point (..), Pos (..))
2024-11-04 23:37:48 +00:00
import Track as T
connString :: ConnectionString
connString = "host=127.0.0.1 port=5432 user=souplesse dbname=souplesse password=secret"
share
[mkPersist sqlSettings, mkMigrate "migrateAll"]
[persistLowerCase|
Trkpt
lat Double
lon Double
ele Double Maybe
time UTCTime
cadence Int Maybe
power Int Maybe
heartRate Int Maybe
|]
fromPoint :: Point -> Trkpt
fromPoint p =
let Pos lat lon ele = T.pos p
in Trkpt lat lon ele (T.time p) (T.cadence p) (T.power p) (T.heartRate p)
2024-11-05 23:32:01 +00:00
toPoint :: Entity Trkpt -> Point
toPoint entity =
let tkp = (\(Entity _ tkp) -> tkp) entity
pos = Pos (trkptLat tkp) (trkptLon tkp) (trkptEle tkp)
in Point
pos
(trkptTime tkp)
2024-11-06 19:13:47 +00:00
(trkptCadence tkp)
(trkptPower tkp)
(trkptHeartRate tkp)
2024-11-05 23:32:01 +00:00
2024-11-04 23:37:48 +00:00
save p = do insert $ fromPoint p
2024-11-05 23:32:01 +00:00
fetch :: (MonadIO m) => ReaderT SqlBackend m [Point]
fetch = do
trkpts <- selectList [TrkptLat <=. 360] [] -- Asc TrkptTime]
return $ map toPoint trkpts