add last_time
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-04-09 14:47:38 +07:00
parent d1f2c359a2
commit ada1a97999
12 changed files with 30 additions and 19 deletions

View File

@ -19,7 +19,7 @@ import (
) )
const ( const (
version = "v1.7.2" version = "v1.7.3"
) )
func main() { func main() {
@ -71,14 +71,14 @@ func main() {
text := update.Message.Text text := update.Message.Text
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
username := update.Message.From.UserName username := update.Message.From.UserName
userInfoDTO, err := dataBase.GetChatInfo(chatID) userInfoDTO, err := dataBase.GetChatInfo(chatID, username)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
} }
for _, s := range botStates { for _, s := range botStates {
if err := s.Do(text, userInfoDTO, username); err != nil { if err := s.Do(text, userInfoDTO); err != nil {
log.Println(err) log.Println(err)
} }
} }

View File

@ -16,6 +16,7 @@ type Chat struct {
type ChatInfo struct { type ChatInfo struct {
ChatID int64 `bson:"chat_id"` ChatID int64 `bson:"chat_id"`
Status string `bson:"status"` Status string `bson:"status"`
Username string `bson:"username"`
} }
func (c *ChatInfo) GetStatus() UserState { func (c *ChatInfo) GetStatus() UserState {

View File

@ -48,8 +48,18 @@ func (db *DB) AddChat(chatID int64) error {
} }
return nil return nil
} }
if result.Err() != nil {
return result.Err() 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 { func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
ctx := context.Background() ctx := context.Background()
@ -87,14 +97,14 @@ func (db *DB) AddCalories(chatID int64, calories *Calories) error {
return err return err
} }
func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) { func (db *DB) GetChatInfo(chatID int64, username string) (*ChatInfo, error) {
ctx := context.Background() ctx := context.Background()
if err := db.AddChat(chatID); err != nil { if err := db.AddChat(chatID); err != nil {
return nil, err return nil, err
} }
chatInfoDTO := &ChatInfo{} chatInfoDTO := &ChatInfo{Username: username}
if err := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { if err := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
return nil, err return nil, err
} }

View File

@ -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) { if !slices.Contains(names, text) {
return nil return nil
} }

View File

@ -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) { if chatInfo.Status == string(db.UserStateEat) {
count, err := calcCalories(text) count, err := calcCalories(text)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil 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) log.Println(err)
return nil return nil
} }

View File

@ -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 chatInfo.Status == string(db.UserStateGo) {
if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserState(text)); err != nil { if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserState(text)); err != nil {
log.Println(err) log.Println(err)
@ -57,7 +57,7 @@ func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) err
log.Println(err) log.Println(err)
continue 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) log.Println(err)
continue continue
} }

View File

@ -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) { if !slices.Contains(names, text) {
return nil return nil
} }

View File

@ -6,6 +6,6 @@ import (
) )
type BotState interface { 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)) GetHandler() (string, func(http.ResponseWriter, *http.Request))
} }

View File

@ -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) { if chatInfo.Status == string(db.UserStatePause) {
duration, err := getDuration(text) duration, err := getDuration(text)
if err != nil { if err != nil {

View File

@ -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) { if !slices.Contains(names, text) {
return nil return nil
} }

View File

@ -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) { if !slices.Contains(names, text) {
return nil return nil
} }

View File

@ -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) { if !slices.Contains(names, text) {
return nil return nil
} }