diff --git a/internal/modules/times/times.go b/internal/modules/times/times.go new file mode 100644 index 0000000..ac7f1ae --- /dev/null +++ b/internal/modules/times/times.go @@ -0,0 +1,16 @@ +package times + +import "time" + +func GetStartDay() time.Time { + loc, _ := time.LoadLocation("Asia/Novosibirsk") + t := time.Now().In(loc) + t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) + return t +} + +func GetStartWeek() time.Time { + t := GetStartDay() + t = t.Add(-6 * 24 * time.Hour) + return t +} diff --git a/internal/states/eat_bot_state/eat_bot_state.go b/internal/states/eat_bot_state/eat_bot_state.go index 994f739..519ccc2 100644 --- a/internal/states/eat_bot_state/eat_bot_state.go +++ b/internal/states/eat_bot_state/eat_bot_state.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "valera/internal/db" + "valera/internal/modules/times" "valera/internal/states" tgbot "github.com/go-telegram-bot-api/telegram-bot-api" @@ -38,7 +39,12 @@ func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error { log.Println(err) return nil } - _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("Калории, фу, %d, записал.", count))) + stat, err := s.dataBase.GetStatAfter(chatInfo.ChatID, times.GetStartDay()) + if err != nil { + log.Println(err) + return nil + } + _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("Калории, фу, %d, записал.\nКалорий сегодня: %v", count, stat["Калории"]))) return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone) } diff --git a/internal/states/stat_bot_state/stat_bot_state.go b/internal/states/stat_bot_state/stat_bot_state.go index b073082..0160f8a 100644 --- a/internal/states/stat_bot_state/stat_bot_state.go +++ b/internal/states/stat_bot_state/stat_bot_state.go @@ -7,6 +7,8 @@ import ( "valera/internal/db" "valera/internal/states" + "valera/internal/modules/times" + tgbot "github.com/go-telegram-bot-api/telegram-bot-api" "golang.org/x/exp/slices" ) @@ -34,11 +36,11 @@ func (s *statBotState) Do(text string, chatInfo *db.ChatInfo) error { } if text == "/stat" { - s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", s.getStartDay()) + s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", times.GetStartDay()) } if text == "/stat_week" { - s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", s.getStartWeek()) + s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", times.GetStartWeek()) } return nil @@ -55,15 +57,15 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req return } for _, chatID := range chats { - t := s.getStartDay() - if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", s.getStartDay()); err != nil { + t := times.GetStartDay() + if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", times.GetStartDay()); err != nil { w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) _, _ = fmt.Fprintf(w, `{"result":"error"}`) return } if t.Weekday() == time.Sunday { - if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", s.getStartWeek()); err != nil { + if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", times.GetStartWeek()); err != nil { w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) _, _ = fmt.Fprintf(w, `{"result":"error"}`) @@ -89,16 +91,3 @@ func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.T _, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText)) return nil } - -func (s *statBotState) getStartDay() time.Time { - loc, _ := time.LoadLocation("Asia/Novosibirsk") - t := time.Now().In(loc) - t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) - return t -} - -func (s *statBotState) getStartWeek() time.Time { - t := s.getStartDay() - t = t.Add(-6 * 24 * time.Hour) - return t -}