built database

This commit is contained in:
Jonathan Lamothe 2019-08-16 11:49:04 -04:00
parent 505d0d8e96
commit 0be1a64119
2 changed files with 60 additions and 0 deletions

View File

@ -3,11 +3,15 @@
module Mtlstats.Types (
-- * Types
ProgState (..),
Database (..),
Player (..),
PlayerStats (..),
Goalie (..),
GoalieStats (..),
-- * Lenses
-- ** Database Lenses
dbPlayers,
dbGoalies,
-- ** Player Lenses
pNumber,
pName,
@ -32,6 +36,7 @@ module Mtlstats.Types (
gsLosses,
gsTies,
-- * Constructors
newDatabase,
newPlayer,
newPlayerStats,
newGoalie,
@ -58,6 +63,28 @@ import Lens.Micro.TH (makeLenses)
-- | Represents the program state
data ProgState = ProgState
-- | Represents the database
data Database = Database
{ _dbPlayers :: [Player]
-- ^ The list of players
, _dbGoalies :: [Goalie]
-- ^ The lidt of goalies
} deriving (Eq, Show)
instance FromJSON Database where
parseJSON = withObject "Database" $ \v -> Database
<$> v .: "players"
<*> v .: "goalies"
instance ToJSON Database where
toJSON (Database ps gs) = object
[ "players" .= ps
, "goalies" .= gs
]
toEncoding (Database ps gs) = pairs $
"players" .= ps <>
"goalies" .= gs
-- | Represents a (non-goalie) player
data Player = Player
{ _pNumber :: Int
@ -201,11 +228,19 @@ instance ToJSON GoalieStats where
"losses" .= l <>
"ties" .= t
makeLenses ''Database
makeLenses ''Player
makeLenses ''PlayerStats
makeLenses ''Goalie
makeLenses ''GoalieStats
-- | Constructor for a 'Database'
newDatabase :: Database
newDatabase = Database
{ _dbPlayers = []
, _dbGoalies = []
}
-- | Constructor for a 'Player'
newPlayer
:: Int

View File

@ -14,6 +14,7 @@ spec = describe "Mtlstats.Types" $ do
pPointsSpec
playerSpec
goalieSpec
databaseSpec
pPointsSpec :: Spec
pPointsSpec = describe "pPoints" $ mapM_
@ -55,6 +56,17 @@ goalieSpec = describe "Goalie" $ do
it "should encode" $
decode (encode goalie) `shouldBe` Just goalie
databaseSpec :: Spec
databaseSpec = describe "Database" $ do
describe "decode" $
it "should decode" $
decode dbJSON `shouldBe` Just db
describe "encode" $
it "should encode" $
decode (encode db) `shouldBe` Just db
player :: Player
player = newPlayer 1 "Joe" "centre"
& pYtd . psGoals .~ 2
@ -81,6 +93,11 @@ goalie = newGoalie 1 "Joe"
& gLifetime . gsLosses .~ 14
& gLifetime . gsTies .~ 15
db :: Database
db = newDatabase
& dbPlayers .~ [player]
& dbGoalies .~ [goalie]
playerJSON :: ByteString
playerJSON = [r|
{ "number": 1
@ -121,3 +138,11 @@ goalieJSON = [r|
, "ties": 15
}
}|]
dbJSON :: ByteString
dbJSON = [r|
{ "players":
[ |] <> playerJSON <> [r| ]
, "goalies":
[ |] <> goalieJSON <> [r| ]
}|]