Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# 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 }
Add Comment
Please, Sign In to add comment