diff --git a/db/db.go b/db/db.go index b6a926e..5daaef1 100644 --- a/db/db.go +++ b/db/db.go @@ -9,15 +9,30 @@ import ( "valera/config" ) +const ( + UserStateNone = UserState("") + UserStateGo = UserState("Go") +) + +type UserState string + type chatDTO struct { ChatID int64 `bson:"chat_id"` } -type chatInfoDTO struct { +type ChatInfoDTO struct { ChatID int64 `bson:"chat_id"` + Status string `bson:"status"` Workouts []Workout `bson:"workouts"` } +func (c *ChatInfoDTO) GetStatus() UserState { + if c.Status == "" { + return UserStateNone + } + return UserStateGo +} + type Workout struct { Name string `bson:"name"` Count int `bson:"count"` @@ -82,20 +97,37 @@ func AddWorkoutInUser(chatID int64, workout *Workout) error { return err } -func GetUsers(chatID int64) ([]string, error) { - // ctx, cancel := context.WithTimeout(context.Background(), time.Second) - // defer cancel() - // - // client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) - // if err != nil { - // return nil, err - // } - // - // collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) - // chatInfoDTO := &chatInfoDTO{} - // if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { - // return nil, err - // } - // return chatInfoDTO.Users, err - return nil, nil +func GetUserInfo(chatID int64) (*ChatInfoDTO, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) + if err != nil { + return nil, err + } + + collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) + chatInfoDTO := &ChatInfoDTO{} + if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { + return nil, err + } + return chatInfoDTO, err +} + +func SetStatusInUser(chatID int64, status UserState) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) + if err != nil { + return err + } + + collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) + _, err = collection.UpdateOne( + ctx, + bson.M{"chat_id": chatID}, + bson.M{"$set": bson.M{"status": status}}, + ) + return err } diff --git a/main.go b/main.go index 07c2bc2..dc44584 100644 --- a/main.go +++ b/main.go @@ -16,15 +16,8 @@ import ( const ( version = "v1.0.0" - userStateNone = userState("None") - userStateGo = userState("Go") ) -var ( - state = userStateNone -) - -type userState string type Opts struct { Token string `short:"t" long:"token" description:"Telegram api token"` @@ -90,7 +83,13 @@ func run() { chatID := update.Message.Chat.ID // username := update.Message.From.UserName - if state == userStateGo { + + userInfoDTO, err := db.GetUserInfo(chatID) + if err != nil { + log.Println(err) + continue + } + if userInfoDTO.GetStatus() == db.UserStateGo { count, err := strconv.Atoi(text) if err != nil { continue @@ -101,7 +100,7 @@ func run() { msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text)) msg.ReplyMarkup = tgbot.NewHideKeyboard(false) _, _ = bot.Send(msg) - state = userStateNone + db.SetStatusInUser(chatID, db.UserStateNone) continue } @@ -123,7 +122,7 @@ func run() { ), ) if _, err = bot.Send(msg); err == nil { - state = userStateGo + db.SetStatusInUser(chatID, db.UserStateGo) } } }