module Model.Income ( getJsonIncome , getFirstIncome , getIncomes , setIncome ) where import Data.Time.Clock (getCurrentTime) import Control.Monad.IO.Class (liftIO) import Database.Persist import Model.Database import qualified Model.Json.Income as Json getJsonIncome :: Entity Income -> Json.Income getJsonIncome incomeEntity = Json.Income (entityKey incomeEntity) (incomeUserId income) (incomeCreation income) (incomeAmount income) where income = entityVal incomeEntity getIncomes :: Persist [Entity Income] getIncomes = selectList [] [] getFirstIncome :: UserId -> Persist (Maybe Income) getFirstIncome userId = fmap entityVal <$> selectFirst [IncomeUserId ==. userId] [Asc IncomeCreation] setIncome :: UserId -> Int -> Persist IncomeId setIncome userId amount = do now <- liftIO getCurrentTime insert (Income userId now amount)