module Model.Income ( getJsonIncome , getIncomes , addIncome , deleteOwnIncome ) where import Data.Time.Clock (getCurrentTime) import Data.Time.Calendar (Day) 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) (incomeDate income) (incomeAmount income) where income = entityVal incomeEntity getIncomes :: Persist [Entity Income] getIncomes = selectList [IncomeDeletedAt ==. Nothing] [] addIncome :: UserId -> Day -> Int -> Persist IncomeId addIncome userId day amount = do now <- liftIO getCurrentTime insert (Income userId day amount now 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