mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 20:43:41 +00:00
37 lines
1.3 KiB
Haskell
37 lines
1.3 KiB
Haskell
module Handler.Notifications where
|
|
|
|
import Startlude
|
|
|
|
import Data.UUID
|
|
import Database.Persist
|
|
import Yesod.Core.Handler
|
|
import Yesod.Core.Types ( JSONResponse(..) )
|
|
import Yesod.Persist.Core
|
|
|
|
import Foundation
|
|
import qualified Lib.Notifications as Notification
|
|
import Model
|
|
|
|
getNotificationsR :: Handler (JSONResponse [Entity Notification])
|
|
getNotificationsR = runDB $ do
|
|
page <- lookupGetParam "page" `orDefaultTo` 1
|
|
pageSize <- lookupGetParam "perPage" `orDefaultTo` 20
|
|
evs <- selectList [] [Desc NotificationCreatedAt, LimitTo pageSize, OffsetBy ((page - 1) * pageSize)]
|
|
let toArchive = fmap entityKey $ filter ((== Nothing) . notificationArchivedAt . entityVal) evs
|
|
void $ Notification.archive toArchive
|
|
pure $ JSONResponse evs
|
|
where
|
|
orDefaultTo :: (Monad m, Read a) => m (Maybe Text) -> a -> m a
|
|
orDefaultTo m a = do
|
|
m' <- m
|
|
case m' >>= readMaybe . toS of
|
|
Nothing -> pure a
|
|
Just x -> pure x
|
|
|
|
deleteNotificationsR :: Handler ()
|
|
deleteNotificationsR = do
|
|
runDB $ deleteWhere ([] :: [Filter Notification])
|
|
|
|
deleteNotificationR :: UUID -> Handler ()
|
|
deleteNotificationR notifId = runDB $ delete (coerce @_ @(Key Notification) notifId)
|