api ready
This commit is contained in:
parent
67d88bd31b
commit
c4b57d7a29
20 changed files with 440 additions and 568 deletions
|
|
@ -1,25 +1,36 @@
|
|||
{-# LANGUAGE NoImplicitPrelude #-}
|
||||
{-# LANGUAGE ImportQualifiedPost #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE NoImplicitPrelude #-}
|
||||
|
||||
module Handler.Api where
|
||||
|
||||
import Import
|
||||
import Database.Persist.Sql (rawSql)
|
||||
import Data.Text qualified as Text
|
||||
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
|
||||
import qualified Data.Text as Text
|
||||
import Database.Persist.Sql (rawSql)
|
||||
import Import
|
||||
|
||||
getApiR :: Int -> Handler TypedContent
|
||||
getApiR time = do
|
||||
-- TODO: use only one runDB
|
||||
userId <- getUserId
|
||||
-- We get all groups no matter what, since else we can't know which groups have been deleted
|
||||
groups <- getGroups userId
|
||||
let utcTime = posixSecondsToUTCTime (fromIntegral time)
|
||||
let sqlLists = "select ?? from todolist join group_user on todolist.group_id = group_user.group_id and group_user.user = ? join \"group\" on todolist.group_id = \"group\".id and \"group\".last_modified > ?;"
|
||||
lists <- runDB $ rawSql sqlLists [toPersistValue userId, toPersistValue utcTime]
|
||||
let a = lists :: [Entity Todolist]
|
||||
let sqlItems = "select ?? from todolist join group_user on todolist.group_id = group_user.group_id and group_user.user = ? join \"group\" on todolist.group_id = \"group\".id and \"group\".last_modified > ? join todolist_item on todolist_item.todolist_id = todolist.id;"
|
||||
items <- runDB $ rawSql sqlItems [toPersistValue userId, toPersistValue utcTime]
|
||||
let t = unlines $ map groupToCSV groups <> map todolistToCSV lists <> map todolistItemToCSV items
|
||||
-- TODO: use only one runDB (or use joins ?)
|
||||
userId <- getUserId
|
||||
let utcTime = posixSecondsToUTCTime (fromIntegral time)
|
||||
-- condition : parent user or group changed
|
||||
let sqlUpdatedGroups = "select ?? from \"group\" join group_user gu on \"group\".id = gu.group_id where gu.user = ? where \"group\".last_modified > ? or user.last_modified > ?;"
|
||||
-- condition : parent group or list changed
|
||||
let sqlUpdatedLists = "select ?? from todolist join group_user on todolist.group_id = group_user.group_id and group_user.user = ? join \"group\" as g on g.id = todolist.id where list.last_modified > ? or g.last_modified > ?;"
|
||||
-- condition : parent list changed
|
||||
let sqlUpdatedItems = "select ?? from todolist where todolist.last_modified > ? join group_user on todolist.group_id = group_user.group_id and group_user.user = ? join todolist_item on todolist_item.todolist_id = todolist.id;"
|
||||
runDB $ do
|
||||
user <- selectList [UserId ==. userId, UserLastModified >. utcTime] []
|
||||
groups <- rawSql sqlUpdatedGroups [toPersistValue userId, toPersistValue utcTime, toPersistValue utcTime]
|
||||
lists <- rawSql sqlUpdatedLists [toPersistValue userId, toPersistValue utcTime, toPersistValue utcTime]
|
||||
items <- rawSql sqlUpdatedItems [toPersistValue utcTime, toPersistValue userId]
|
||||
let t =
|
||||
unlines
|
||||
$ map userToCSV user
|
||||
<> map groupToCSV groups
|
||||
<> map todolistToCSV lists
|
||||
<> map todolistItemToCSV items
|
||||
return $ TypedContent typePlain $ toContent t
|
||||
|
||||
todolistItemToCSV :: Entity TodolistItem -> Text
|
||||
|
|
@ -28,9 +39,11 @@ todolistToCSV :: Entity Todolist -> Text
|
|||
todolistToCSV list = "l," <> fieldToText list
|
||||
groupToCSV :: Entity Group -> Text
|
||||
groupToCSV group = "g," <> fieldToText group
|
||||
userToCSV :: Entity User -> Text
|
||||
userToCSV user = "u," <> fieldToText user
|
||||
|
||||
-- TODO: error management ? (maybe use Either Text Text and then propagate left to handler and send error ?)
|
||||
fieldToText :: PersistEntity record => Entity record -> Text
|
||||
fieldToText :: (PersistEntity record) => Entity record -> Text
|
||||
fieldToText field = Text.intercalate "," (map persistValueToText $ entityValues field)
|
||||
|
||||
persistValueToText :: PersistValue -> Text
|
||||
|
|
@ -38,15 +51,15 @@ persistValueToText (PersistText s) = s
|
|||
persistValueToText (PersistInt64 i) = Text.pack $ show i
|
||||
persistValueToText (PersistUTCTime d) = Text.pack $ show $ floor (utcTimeToPOSIXSeconds d)
|
||||
persistValueToText (PersistBool b) = if b then "T" else "F"
|
||||
persistValueToText _ = error "Wrong input type"
|
||||
persistValueToText _ = error "Wrong input type"
|
||||
|
||||
getText :: Text
|
||||
getText = do
|
||||
-- GET EVERY GROUP THAT HAS BEEN MODIFIED SINCE TIMESTAMP FROM USER
|
||||
-- GET EVERY GROUP THAT HAS BEEN MODIFIED SINCE TIMESTAMP FROM USER
|
||||
|
||||
-- GET EVERY TODOLIST THAT HAS BEEN MODIFIED SINCE TIMESTAMP
|
||||
-- GET EVERY ITEM FROM THESE TODOLISTS
|
||||
-- ENCODE ALL OF THEM IN THE TEXTFILE
|
||||
-- SEND IT !
|
||||
-- DONE :)
|
||||
error "not done yet"
|
||||
-- GET EVERY TODOLIST THAT HAS BEEN MODIFIED SINCE TIMESTAMP
|
||||
-- GET EVERY ITEM FROM THESE TODOLISTS
|
||||
-- ENCODE ALL OF THEM IN THE TEXTFILE
|
||||
-- SEND IT !
|
||||
-- DONE :)
|
||||
error "not done yet"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue