module Model.Income ( getJsonIncome , getIncomes , addIncome , deleteOwnIncome ) where import Data.Time.Clock (UTCTime, 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 [IncomeDeletedAt ==. Nothing] [] addIncome :: UserId -> UTCTime -> Int -> Persist IncomeId addIncome userId creation amount = do insert (Income userId creation amount Nothing) deleteOwnIncome :: Entity User -> IncomeId -> Persist Bool deleteOwnIncome user incomeId = do mbIncome <- get incomeId case mbIncome of Just income -> if incomeUserId income == entityKey user then do now <- liftIO getCurrentTime update incomeId [IncomeDeletedAt =. Just now] return True else return False Nothing -> return False