aboutsummaryrefslogtreecommitdiff
path: root/src/server/Model/Job.hs
diff options
context:
space:
mode:
authorJoris2015-09-06 23:25:44 +0200
committerJoris2015-09-06 23:25:44 +0200
commit53afb9c96904ab226ccee754419569da16c59871 (patch)
tree18c2497275d857f99399595d40a4ccc5a65e396a /src/server/Model/Job.hs
parent2e75a5ac41afd4d6458ad230bd26fd9e73c7bdb9 (diff)
downloadbudget-53afb9c96904ab226ccee754419569da16c59871.tar.gz
budget-53afb9c96904ab226ccee754419569da16c59871.tar.bz2
budget-53afb9c96904ab226ccee754419569da16c59871.zip
Setting up the monthly job
Diffstat (limited to 'src/server/Model/Job.hs')
-rw-r--r--src/server/Model/Job.hs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/server/Model/Job.hs b/src/server/Model/Job.hs
new file mode 100644
index 0000000..3d5df96
--- /dev/null
+++ b/src/server/Model/Job.hs
@@ -0,0 +1,27 @@
+module Model.Job
+ ( getLastExecution
+ , actualizeLastExecution
+ ) where
+
+import Control.Monad.IO.Class (liftIO)
+
+import Data.Time.Clock (UTCTime, getCurrentTime)
+import Data.Maybe (isJust)
+
+import Database.Persist
+
+import Model.Database
+import Model.JobKind
+
+getLastExecution :: JobKind -> Persist (Maybe UTCTime)
+getLastExecution kind = do
+ mbJob <- fmap entityVal <$> selectFirst [JobKind ==. kind] []
+ return (mbJob >>= jobLastExecution)
+
+actualizeLastExecution :: JobKind -> Persist ()
+actualizeLastExecution kind = do
+ now <- liftIO getCurrentTime
+ jobKindDefined <- isJust <$> selectFirst [JobKind ==. kind] []
+ if jobKindDefined
+ then updateWhere [JobKind ==. kind] [JobLastExecution =. Just now]
+ else insert (Job kind (Just now)) >> return ()