Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Created February 8, 2018 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenXL/3ea2032b398b651e5aeb25bb5b85d82e to your computer and use it in GitHub Desktop.
Save StevenXL/3ea2032b398b651e5aeb25bb5b85d82e to your computer and use it in GitHub Desktop.
ByteString
{-# 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