Created
February 8, 2018 02:29
-
-
Save StevenXL/3ea2032b398b651e5aeb25bb5b85d82e to your computer and use it in GitHub Desktop.
ByteString
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) | |
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 = 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