buuka/src/Data/Buuka.hs

55 lines
1.2 KiB
Haskell

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Buuka
( BuukaQ(..)
, BuukaU(..)
, BuukaEntry(..)
, URL(..)
, insert
)
where
import Data.Map
(Map)
import qualified Data.Map.Strict as M
import Database.Migrations
import Data.Aeson
import GHC.Generics
(Generic)
import Control.Monad.Reader
import Control.Monad.State
newtype URL = URL String
deriving stock (Show, Eq, Generic, Ord)
deriving newtype (ToJSON, FromJSON, FromJSONKey, ToJSONKey)
data BuukaEntry
= BuukaEntry { url :: URL
, title :: Maybe String
}
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)
instance SafeJSON BuukaEntry where
type Version BuukaEntry = 0
newtype Buuka = Buuka ( Map URL BuukaEntry )
deriving newtype (Semigroup, Monoid, FromJSON, ToJSON)
insert :: BuukaEntry -> Buuka -> Buuka
insert e (Buuka b) = Buuka (M.insert (url e) e b)
instance SafeJSON Buuka where
type Version Buuka = 0
newtype BuukaQ a = BuukaQ { runBuukaQ :: Reader Buuka a }
deriving newtype (Functor, Applicative, Monad, MonadReader Buuka)
-- Last write wins
newtype BuukaU a = BuukaU { runBuukaU :: State Buuka a }
deriving newtype (Functor, Applicative, Monad, MonadState Buuka)