From ada1a97999b5ac85354bdd0753ee058c05a40013 Mon Sep 17 00:00:00 2001 From: FedorovVladimir Date: Sun, 9 Apr 2023 14:47:38 +0700 Subject: [PATCH] add last_time --- cmd/valera/main.go | 6 +++--- internal/db/chat.go | 5 +++-- internal/db/db.go | 16 +++++++++++++--- .../states/clear_bot_state/clear_bot_state.go | 2 +- internal/states/eat_bot_state/eat_bot_state.go | 4 ++-- internal/states/go_bot_state/go_bot_state.go | 4 ++-- internal/states/help_bot_state/help_bot_state.go | 2 +- internal/states/interface.go | 2 +- .../states/pause_bot_state/pause_bot_state.go | 2 +- internal/states/ping_bot_state/ping_bot_state.go | 2 +- .../states/start_bot_state/start_bot_state.go | 2 +- internal/states/stat_bot_state/stat_bot_state.go | 2 +- 12 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cmd/valera/main.go b/cmd/valera/main.go index 4139192..38af6e7 100644 --- a/cmd/valera/main.go +++ b/cmd/valera/main.go @@ -19,7 +19,7 @@ import ( ) const ( - version = "v1.7.2" + version = "v1.7.3" ) func main() { @@ -71,14 +71,14 @@ func main() { text := update.Message.Text chatID := update.Message.Chat.ID username := update.Message.From.UserName - userInfoDTO, err := dataBase.GetChatInfo(chatID) + userInfoDTO, err := dataBase.GetChatInfo(chatID, username) if err != nil { log.Println(err) continue } for _, s := range botStates { - if err := s.Do(text, userInfoDTO, username); err != nil { + if err := s.Do(text, userInfoDTO); err != nil { log.Println(err) } } diff --git a/internal/db/chat.go b/internal/db/chat.go index 17deff1..775ae58 100644 --- a/internal/db/chat.go +++ b/internal/db/chat.go @@ -14,8 +14,9 @@ type Chat struct { } type ChatInfo struct { - ChatID int64 `bson:"chat_id"` - Status string `bson:"status"` + ChatID int64 `bson:"chat_id"` + Status string `bson:"status"` + Username string `bson:"username"` } func (c *ChatInfo) GetStatus() UserState { diff --git a/internal/db/db.go b/internal/db/db.go index 676a634..0e85558 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -48,7 +48,17 @@ func (db *DB) AddChat(chatID int64) error { } return nil } - return result.Err() + if result.Err() != nil { + return result.Err() + } + loc, _ := time.LoadLocation("Asia/Novosibirsk") + t := time.Now().In(loc) + _, err := db.chatsColection.UpdateOne( + ctx, + bson.M{"chat_id": chatID}, + bson.M{"$set": bson.M{"last_time": t}}, + ) + return err } func (db *DB) AddWorkout(chatID int64, workout *Workout) error { @@ -87,14 +97,14 @@ func (db *DB) AddCalories(chatID int64, calories *Calories) error { return err } -func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) { +func (db *DB) GetChatInfo(chatID int64, username string) (*ChatInfo, error) { ctx := context.Background() if err := db.AddChat(chatID); err != nil { return nil, err } - chatInfoDTO := &ChatInfo{} + chatInfoDTO := &ChatInfo{Username: username} if err := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { return nil, err } diff --git a/internal/states/clear_bot_state/clear_bot_state.go b/internal/states/clear_bot_state/clear_bot_state.go index e80aba9..ae150c8 100644 --- a/internal/states/clear_bot_state/clear_bot_state.go +++ b/internal/states/clear_bot_state/clear_bot_state.go @@ -26,7 +26,7 @@ func NewClearBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *clearBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *clearBotState) Do(text string, chatInfo *db.ChatInfo) error { if !slices.Contains(names, text) { return nil } diff --git a/internal/states/eat_bot_state/eat_bot_state.go b/internal/states/eat_bot_state/eat_bot_state.go index b89b48d..63e7d60 100644 --- a/internal/states/eat_bot_state/eat_bot_state.go +++ b/internal/states/eat_bot_state/eat_bot_state.go @@ -27,14 +27,14 @@ func NewEatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error { if chatInfo.Status == string(db.UserStateEat) { count, err := calcCalories(text) if err != nil { log.Println(err) return nil } - if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, username)); err != nil { + if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, chatInfo.Username)); err != nil { log.Println(err) return nil } diff --git a/internal/states/go_bot_state/go_bot_state.go b/internal/states/go_bot_state/go_bot_state.go index f45cb06..f0dbf6c 100644 --- a/internal/states/go_bot_state/go_bot_state.go +++ b/internal/states/go_bot_state/go_bot_state.go @@ -38,7 +38,7 @@ func NewGoBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *goBotState) Do(text string, chatInfo *db.ChatInfo) error { if chatInfo.Status == string(db.UserStateGo) { if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserState(text)); err != nil { log.Println(err) @@ -57,7 +57,7 @@ func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) err log.Println(err) continue } - if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, username)); err != nil { + if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, chatInfo.Username)); err != nil { log.Println(err) continue } diff --git a/internal/states/help_bot_state/help_bot_state.go b/internal/states/help_bot_state/help_bot_state.go index 54cdea8..e7bb958 100644 --- a/internal/states/help_bot_state/help_bot_state.go +++ b/internal/states/help_bot_state/help_bot_state.go @@ -25,7 +25,7 @@ func NewHelpBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *helpBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *helpBotState) Do(text string, chatInfo *db.ChatInfo) error { if !slices.Contains(names, text) { return nil } diff --git a/internal/states/interface.go b/internal/states/interface.go index e822684..39b1480 100644 --- a/internal/states/interface.go +++ b/internal/states/interface.go @@ -6,6 +6,6 @@ import ( ) type BotState interface { - Do(text string, chatInfo *db.ChatInfo, username string) error + Do(text string, chatInfo *db.ChatInfo) error GetHandler() (string, func(http.ResponseWriter, *http.Request)) } diff --git a/internal/states/pause_bot_state/pause_bot_state.go b/internal/states/pause_bot_state/pause_bot_state.go index 5f76ec4..702be79 100644 --- a/internal/states/pause_bot_state/pause_bot_state.go +++ b/internal/states/pause_bot_state/pause_bot_state.go @@ -27,7 +27,7 @@ func NewPauseBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo) error { if chatInfo.Status == string(db.UserStatePause) { duration, err := getDuration(text) if err != nil { diff --git a/internal/states/ping_bot_state/ping_bot_state.go b/internal/states/ping_bot_state/ping_bot_state.go index d960459..3f49699 100644 --- a/internal/states/ping_bot_state/ping_bot_state.go +++ b/internal/states/ping_bot_state/ping_bot_state.go @@ -25,7 +25,7 @@ func NewPingBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo) error { if !slices.Contains(names, text) { return nil } diff --git a/internal/states/start_bot_state/start_bot_state.go b/internal/states/start_bot_state/start_bot_state.go index 3030b7e..6ccadb9 100644 --- a/internal/states/start_bot_state/start_bot_state.go +++ b/internal/states/start_bot_state/start_bot_state.go @@ -28,7 +28,7 @@ func NewStartBotState(bot *tgbot.BotAPI, dataBase *db.DB, version string) states } } -func (s *startBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *startBotState) Do(text string, chatInfo *db.ChatInfo) error { if !slices.Contains(names, text) { return nil } diff --git a/internal/states/stat_bot_state/stat_bot_state.go b/internal/states/stat_bot_state/stat_bot_state.go index c119af6..89f99b5 100644 --- a/internal/states/stat_bot_state/stat_bot_state.go +++ b/internal/states/stat_bot_state/stat_bot_state.go @@ -28,7 +28,7 @@ func NewStatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState { } } -func (s *statBotState) Do(text string, chatInfo *db.ChatInfo, username string) error { +func (s *statBotState) Do(text string, chatInfo *db.ChatInfo) error { if !slices.Contains(names, text) { return nil }