Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. {-# LANGUAGE NoImplicitPrelude #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. module Handler.ExpenseFile where
  4.  
  5. import Import
  6. import Handler.Expense (expenseFileForm)
  7. import Data.Csv (decodeByName)
  8. import qualified Data.ByteString as B
  9.  
  10. postExpenseFileR :: Handler Html
  11. postExpenseFileR = do
  12. ((result, _), _) <- runFormPost expenseFileForm
  13. case result of
  14. FormSuccess fileInfo -> handleFormSuccess fileInfo
  15. _ -> handleFormUnsuccess
  16.  
  17. -- what we need to do here is use fileSource :: FileInfo -> Source m ByteString
  18. handleFormSuccess :: FileInfo -> Handler Html
  19. handleFormSuccess fileInfo = do
  20. expenseShells <- runConduit $ fileSource fileInfo .| toExpenseShell .| sinkList
  21. redirect ExpenseR
  22.  
  23. handleFormUnsuccess :: Handler Html
  24. handleFormUnsuccess = do
  25. setMessage "Invalid File Form Submitted"
  26. redirect ExpenseR
  27.  
  28. -- Conduit Components
  29. -- Source m ByteString ~ ConduitM () ByteString m ()
  30. -- ConduitM ByteString downstream m ()
  31. -- toExpenseShell :: ConduitM i (Either String (Vector ExpenseShell)) m ()
  32. toExpenseShell :: ConduitM B.ByteString (Either String (Vector ExpenseShell)) m ()
  33. toExpenseShell = mapC $ \bs -> fmap snd (decodeByName bs)
  34.  
  35. -- probaly goes in another module
  36. data ExpenseShell = ExpenseShell { esAmount :: !Int, esItem :: !Text, esVendor :: !Text }
Add Comment
Please, Sign In to add comment