Update tasks in database

This commit is contained in:
Joscha 2018-03-26 15:35:25 +00:00
parent d4b5d0b7c2
commit 9d8d6f6d80
2 changed files with 36 additions and 16 deletions

View file

@ -52,8 +52,9 @@ instance DB.FromField BoolFormula where
data TaskRow = TaskRow data TaskRow = TaskRow
{ rowID :: Integer { rowID :: Integer
, rowDeadline :: Maybe Day , rowDeadline :: Maybe Day
, rowBoolFormula :: Maybe BoolFormula -- deadline formula , rowDuration :: Integer -- If there is no deadline, the duration is irrelevant
, rowIntFormula :: Maybe IntFormula -- info formula (e. g. age for birthdays) , rowBoolFormula :: Maybe BoolFormula -- Deadline formula
, rowIntFormula :: Maybe IntFormula -- Info formula (e. g. age for birthdays)
, rowDescription :: T.Text , rowDescription :: T.Text
, rowDetails :: T.Text , rowDetails :: T.Text
, rowRepetitionsTotal :: Integer , rowRepetitionsTotal :: Integer
@ -64,6 +65,7 @@ instance DB.ToRow TaskRow where
toRow TaskRow{..} = DB.toRow toRow TaskRow{..} = DB.toRow
( rowID ( rowID
, rowDeadline , rowDeadline
, rowDuration
, rowBoolFormula , rowBoolFormula
, rowIntFormula , rowIntFormula
, rowDescription , rowDescription
@ -74,15 +76,16 @@ instance DB.ToRow TaskRow where
instance DB.FromRow TaskRow where instance DB.FromRow TaskRow where
fromRow = do fromRow = do
(a,b,c,d,e,f,g,h) <- DB.fromRow (a,b,c,d,e,f,g,h,i) <- DB.fromRow
let rowID = a let rowID = a
rowDeadline = b rowDeadline = b
rowBoolFormula = c rowDuration = c
rowIntFormula = d rowBoolFormula = d
rowDescription = e rowIntFormula = e
rowDetails = f rowDescription = f
rowRepetitionsTotal = g rowDetails = g
rowRepetitionsDone = h rowRepetitionsTotal = h
rowRepetitionsDone = i
return TaskRow{..} return TaskRow{..}
-- TODO: Maybe put this in separate module and/or make less specific? -- TODO: Maybe put this in separate module and/or make less specific?
@ -102,6 +105,7 @@ initializeNewDB c = do
"CREATE TABLE IF NOT EXISTS tasks (\ "CREATE TABLE IF NOT EXISTS tasks (\
\ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\ \ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
\ deadline TEXT,\ \ deadline TEXT,\
\ duration INTEGER NOT NULL,\
\ boolFormula TEXT,\ \ boolFormula TEXT,\
\ intFormula TEXT,\ \ intFormula TEXT,\
\ description TEXT NOT NULL,\ \ description TEXT NOT NULL,\
@ -116,15 +120,22 @@ initializeNewDB c = do
fillVersionTable = fillVersionTable =
"INSERT INTO version (version_number) VALUES (?)" "INSERT INTO version (version_number) VALUES (?)"
updateTask :: TaskRow -> Maybe TaskRow updateTask :: Day -> TaskRow -> Maybe (Day, Integer)
updateTask t = undefined updateTask day t = do
expr <- boolFormulaExpr <$> rowBoolFormula t
nextDeadline <- TM.findNext expr day (fromIntegral $ rowDuration t)
case rowDeadline t of
Nothing -> return (nextDeadline, rowID t)
Just prevDeadline ->
if prevDeadline == nextDeadline
then Nothing
else return (nextDeadline, rowID t)
updateTasks :: DB.Connection -> IO () updateTasks :: DB.Connection -> Day -> IO ()
updateTasks c = DB.withTransaction c $ do updateTasks c day = DB.withTransaction c $ do
tasks <- DB.query_ c selectTasksToUpdate tasks <- DB.query_ c selectTasksToUpdate
let toUpdate = catMaybes $ map updateTask tasks let updated = mapMaybe (updateTask day) tasks
params = map (\t -> (rowDeadline t, rowID t)) toUpdate DB.executeMany c updateTaskRow updated
DB.executeMany c updateTaskRow params
where where
selectTasksToUpdate = selectTasksToUpdate =
"SELECT * FROM tasks\ "SELECT * FROM tasks\

View file

@ -4,6 +4,7 @@ module TaskMachine.DateExpr
( BoolExpr ( BoolExpr
, parseBoolExpr , parseBoolExpr
, evalBoolExpr , evalBoolExpr
, findNext
, IntExpr , IntExpr
, parseIntExpr , parseIntExpr
, evalIntExpr , evalIntExpr
@ -11,6 +12,8 @@ module TaskMachine.DateExpr
import Control.Applicative import Control.Applicative
import Control.Monad import Control.Monad
import Data.List
import Data.Maybe
import Data.Void import Data.Void
import Data.Time.Calendar import Data.Time.Calendar
@ -69,6 +72,12 @@ parseBoolExpr = parseMaybe boolExpr
parseIntExpr :: String -> Maybe IntExpr parseIntExpr :: String -> Maybe IntExpr
parseIntExpr = parseMaybe intExpr parseIntExpr = parseMaybe intExpr
findNext :: BoolExpr -> Day -> Int -> Maybe Day
findNext expr start duration =
let possibleDays = take duration $ iterate (addDays 1) start
checkDay = fromMaybe False . evalBoolExpr expr
in find checkDay possibleDays
{- {-
- Evaluating expressions - Evaluating expressions
-} -}