From baadb5ae549c713e369a66c32a9f5ca71ab56cd8 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 19 Aug 2019 09:31:24 -0400 Subject: [PATCH] store number of games in database --- src/Mtlstats/Types.hs | 19 +++++++++++++------ test/TypesSpec.hs | 2 ++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 773f6b8..c0d89e1 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -12,6 +12,7 @@ module Mtlstats.Types ( -- ** Database Lenses dbPlayers, dbGoalies, + dbGames, -- ** Player Lenses pNumber, pName, @@ -69,21 +70,26 @@ data Database = Database -- ^ The list of players , _dbGoalies :: [Goalie] -- ^ The lidt of goalies + , _dbGames :: Int + -- ^ The number of games recorded } deriving (Eq, Show) instance FromJSON Database where parseJSON = withObject "Database" $ \v -> Database <$> v .: "players" <*> v .: "goalies" + <*> v .: "games" instance ToJSON Database where - toJSON (Database ps gs) = object - [ "players" .= ps - , "goalies" .= gs + toJSON (Database players goalies games) = object + [ "players" .= players + , "goalies" .= goalies + , "games" .= games ] - toEncoding (Database ps gs) = pairs $ - "players" .= ps <> - "goalies" .= gs + toEncoding (Database players goalies games) = pairs $ + "players" .= players <> + "goalies" .= goalies <> + "games" .= games -- | Represents a (non-goalie) player data Player = Player @@ -239,6 +245,7 @@ newDatabase :: Database newDatabase = Database { _dbPlayers = [] , _dbGoalies = [] + , _dbGames = 0 } -- | Constructor for a 'Player' diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index f435d9f..c3c10a8 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -97,6 +97,7 @@ db :: Database db = newDatabase & dbPlayers .~ [player] & dbGoalies .~ [goalie] + & dbGames .~ 1 playerJSON :: ByteString playerJSON = [r| @@ -145,4 +146,5 @@ dbJSON = [r| [ |] <> playerJSON <> [r| ] , "goalies": [ |] <> goalieJSON <> [r| ] + , "games": 1 }|]