api: cleanup

This commit is contained in:
Simon Michael 2016-01-17 11:07:26 -08:00
parent ab1d47cfe2
commit cabcd80ad5

View File

@ -82,7 +82,7 @@ hledgerApiApp j = Servant.serve hledgerApi hledgerApiServer
-- add Reader to the server monad so handlers can see the journal -- add Reader to the server monad so handlers can see the journal
hledgerApiServer :: Servant.Server HledgerApi hledgerApiServer :: Servant.Server HledgerApi
hledgerApiServer = Servant.enter readerToEither hledgerServerT hledgerApiServer = Servant.enter readerToEither hledgerAPIServer
where where
readerToEither :: Reader Journal :~> EitherT ServantErr IO readerToEither :: Reader Journal :~> EitherT ServantErr IO
readerToEither = Nat $ \r -> return (runReader r j) readerToEither = Nat $ \r -> return (runReader r j)
@ -94,11 +94,10 @@ type HledgerApi =
:<|> "commodities" :> Get '[JSON] [Commodity] :<|> "commodities" :> Get '[JSON] [Commodity]
:<|> "accounts" :> Get '[JSON] [Account] :<|> "accounts" :> Get '[JSON] [Account]
:<|> "reports" :> :<|> "reports" :>
-- "accounttransactions" :> QueryParam "acct" AccountName :> Get '[JSON] AccountTransactionsReport
"accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
hledgerServerT :: ServerT HledgerApi (Reader Journal) hledgerAPIServer :: ServerT HledgerApi (Reader Journal)
hledgerServerT = hledgerAPIServer =
accountnamesH accountnamesH
:<|> transactionsH :<|> transactionsH
:<|> pricesH :<|> pricesH
@ -123,8 +122,12 @@ hledgerServerT =
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs thisacctq = Acct $ accountNameToAccountRegex a -- includes subs
return $ accountTransactionsReport ropts j q thisacctq return $ accountTransactionsReport ropts j q thisacctq
instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions -- avoid https://github.com/bos/aeson/issues/290 -- brief toJSON definitions included to avoid https://github.com/bos/aeson/issues/290
-- use toEncoding = genericToEncoding defaultOptions instead ?
instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions
instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
instance ToJSON Decimal where
toJSON = toJSON . show
instance ToJSON Amount where toJSON = genericToJSON defaultOptions instance ToJSON Amount where toJSON = genericToJSON defaultOptions
instance ToJSON AmountStyle where toJSON = genericToJSON defaultOptions instance ToJSON AmountStyle where toJSON = genericToJSON defaultOptions
instance ToJSON Side where toJSON = genericToJSON defaultOptions instance ToJSON Side where toJSON = genericToJSON defaultOptions
@ -132,8 +135,8 @@ instance ToJSON DigitGroupStyle where toJSON = genericToJSON defaultOptions
instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions
instance ToJSON Price where toJSON = genericToJSON defaultOptions instance ToJSON Price where toJSON = genericToJSON defaultOptions
instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions
instance ToJSON Posting instance ToJSON PostingType where toJSON = genericToJSON defaultOptions
where instance ToJSON Posting where
toJSON Posting{..} = toJSON Posting{..} =
object object
["pdate" .= toJSON pdate ["pdate" .= toJSON pdate
@ -147,16 +150,8 @@ instance ToJSON Posting
,"pbalanceassertion" .= toJSON pbalanceassertion ,"pbalanceassertion" .= toJSON pbalanceassertion
,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction) ,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction)
] ]
instance ToJSON PostingType where toJSON = genericToJSON defaultOptions
instance ToJSON Transaction where toJSON = genericToJSON defaultOptions instance ToJSON Transaction where toJSON = genericToJSON defaultOptions
instance ToJSON Decimal instance ToJSON Account where
where
-- toJSON (Decimal decimalPlaces decimalMantissa) =
-- object ["places" .= decimalPlaces, "mantissa" .= decimalMantissa]
-- toEncoding = genericToEncoding defaultOptions
toJSON d = toJSON $ show d
instance ToJSON Account
where
toJSON a = toJSON a =
object object
["aname" .= toJSON (aname a) ["aname" .= toJSON (aname a)
@ -167,6 +162,4 @@ instance ToJSON Account
,"aparentname" .= toJSON (maybe "" aname $ aparent a) ,"aparentname" .= toJSON (maybe "" aname $ aparent a)
,"asubs" .= toJSON (map toJSON $ asubs a) ,"asubs" .= toJSON (map toJSON $ asubs a)
] ]
instance ToJSON AccountTransactionsReport where toJSON = genericToJSON defaultOptions instance ToJSON AccountTransactionsReport where toJSON = genericToJSON defaultOptions