From 2487771058fa2b1f984c5f27257956454f941f79 Mon Sep 17 00:00:00 2001 From: kemphack Date: Wed, 28 Jun 2017 13:50:30 +0300 Subject: [PATCH] counting notes --- lib/Bot.hs | 11 +++++++++-- lib/BotCommands.hs | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/Bot.hs b/lib/Bot.hs index b34f32b..6860a2b 100644 --- a/lib/Bot.hs +++ b/lib/Bot.hs @@ -12,7 +12,8 @@ import Web.Telegram.API.Bot (Chat (..), GetUpdatesRequest (..), Message (..), Response (..), TelegramClient, Update (..), User (..), getUpdatesM, getUpdatesRequest) -import BotCommands (BotCmd (..), addNote, readCommand, showNew, showOld) +import BotCommands (BotCmd (..), addNote, countNotes, readCommand, sendMessage, + showNew, showOld) import Const (updateIdFile) import Tools (putLog, saveOffset, tshow) @@ -44,7 +45,13 @@ handleMessage update = showOld (fromIntegral chat_id) user_id WrongCommand wrongCmd -> putLog $ cmdErr wrongCmd - Nothing -> addNote user_id text + Nothing -> do + addNote user_id text + notes <- countNotes user_id + sendMessage + (fromIntegral chat_id) + ("Добавлено. Всего " <> (tshow notes) <> + " заметок.") saveOffset updateIdFile update_id Just msg -> putLog $ "unhandled " <> tshow msg diff --git a/lib/BotCommands.hs b/lib/BotCommands.hs index a4b6ecd..a25b84e 100644 --- a/lib/BotCommands.hs +++ b/lib/BotCommands.hs @@ -4,7 +4,9 @@ module BotCommands ( BotCmd (..) , addNote + , countNotes , readCommand + , sendMessage , showNew , showOld ) where @@ -24,6 +26,16 @@ import DB (EntityField (NoteId, NoteOwner), Note (..), User (..), runDB) data BotCmd = ShowNew | ShowOld | WrongCommand Text +countNotes :: Int + -> TelegramClient (Int) +countNotes userId = do + mUid <- runDB $ getKeyByValue DB.User{userTelegramId = fromIntegral userId} + case mUid of + Just uid -> do + notes <- runDB $ selectValList [NoteOwner ==. uid] [] + pure $ length notes + Nothing -> pure 0 + sendMessage :: Integer -> Text -> TelegramClient ()