-
-
Save StevenXL/058572b7fb70bdb9a93b5d3251274e71 to your computer and use it in GitHub Desktop.
a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE NoImplicitPrelude #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Handler.ExpenseFile where | |
import Import | |
import Handler.Expense (expenseFileForm) | |
import Data.Csv (decodeByName) | |
import qualified Data.ByteString as B | |
postExpenseFileR :: Handler Html | |
postExpenseFileR = do | |
((result, _), _) <- runFormPost expenseFileForm | |
case result of | |
FormSuccess fileInfo -> handleFormSuccess fileInfo | |
_ -> handleFormUnsuccess | |
-- what we need to do here is use fileSource :: FileInfo -> Source m ByteString | |
handleFormSuccess :: FileInfo -> Handler Html | |
handleFormSuccess fileInfo = do | |
expenseShells <- runConduit $ fileSource fileInfo .| toExpenseShell .| sinkList | |
redirect ExpenseR | |
handleFormUnsuccess :: Handler Html | |
handleFormUnsuccess = do | |
setMessage "Invalid File Form Submitted" | |
redirect ExpenseR | |
-- Conduit Components | |
-- Source m ByteString ~ ConduitM () ByteString m () | |
-- ConduitM ByteString downstream m () | |
-- toExpenseShell :: ConduitM i (Either String (Vector ExpenseShell)) m () | |
toExpenseShell :: ConduitM B.ByteString (Either String (Vector ExpenseShell)) m () | |
toExpenseShell = mapC $ \bs -> fmap snd (decodeByName bs) | |
-- probaly goes in another module | |
data ExpenseShell = ExpenseShell { esAmount :: !Int, esItem :: !Text, esVendor :: !Text } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment