diff options
Diffstat (limited to 'src/server/Model/Income.hs')
-rw-r--r-- | src/server/Model/Income.hs | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/server/Model/Income.hs b/src/server/Model/Income.hs index 62ab0ed..f389661 100644 --- a/src/server/Model/Income.hs +++ b/src/server/Model/Income.hs @@ -1,8 +1,9 @@ module Model.Income ( getJsonIncome , getIncomes - , createIncome - , deleteOwnIncome + , create + , editOwn + , deleteOwn ) where import Data.Time.Clock (getCurrentTime) @@ -23,13 +24,32 @@ getJsonIncome incomeEntity = getIncomes :: Persist [Entity Income] getIncomes = selectList [IncomeDeletedAt ==. Nothing] [] -createIncome :: UserId -> Day -> Int -> Persist IncomeId -createIncome userId date amount = do +create :: UserId -> Day -> Int -> Persist IncomeId +create userId date amount = do now <- liftIO getCurrentTime - insert (Income userId date amount now Nothing) + insert (Income userId date amount now Nothing Nothing) -deleteOwnIncome :: Entity User -> IncomeId -> Persist Bool -deleteOwnIncome user incomeId = do +editOwn :: UserId -> IncomeId -> Day -> Int -> Persist Bool +editOwn userId incomeId date amount = do + mbIncome <- get incomeId + case mbIncome of + Just income -> + if incomeUserId income == userId + then do + now <- liftIO getCurrentTime + update incomeId + [ IncomeEditedAt =. Just now + , IncomeDate =. date + , IncomeAmount =. amount + ] + return True + else + return False + Nothing -> + return False + +deleteOwn :: Entity User -> IncomeId -> Persist Bool +deleteOwn user incomeId = do mbIncome <- get incomeId case mbIncome of Just income -> |