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

This commit is contained in:
Владимир Фёдоров 2023-03-11 17:30:38 +07:00
parent b6b017c9bb
commit 57755d28d2
2 changed files with 58 additions and 27 deletions

View File

@ -9,15 +9,30 @@ import (
"valera/config" "valera/config"
) )
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
)
type UserState string
type chatDTO struct { type chatDTO struct {
ChatID int64 `bson:"chat_id"` ChatID int64 `bson:"chat_id"`
} }
type chatInfoDTO struct { type ChatInfoDTO struct {
ChatID int64 `bson:"chat_id"` ChatID int64 `bson:"chat_id"`
Status string `bson:"status"`
Workouts []Workout `bson:"workouts"` Workouts []Workout `bson:"workouts"`
} }
func (c *ChatInfoDTO) GetStatus() UserState {
if c.Status == "" {
return UserStateNone
}
return UserStateGo
}
type Workout struct { type Workout struct {
Name string `bson:"name"` Name string `bson:"name"`
Count int `bson:"count"` Count int `bson:"count"`
@ -82,20 +97,37 @@ func AddWorkoutInUser(chatID int64, workout *Workout) error {
return err return err
} }
func GetUsers(chatID int64) ([]string, error) { func GetUserInfo(chatID int64) (*ChatInfoDTO, error) {
// ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
// defer cancel() defer cancel()
//
// client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
// if err != nil { if err != nil {
// return nil, err return nil, err
// } }
//
// collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
// chatInfoDTO := &chatInfoDTO{} chatInfoDTO := &ChatInfoDTO{}
// if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil { if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
// return nil, err return nil, err
// } }
// return chatInfoDTO.Users, err return chatInfoDTO, err
return nil, nil }
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
} }

19
main.go
View File

@ -16,15 +16,8 @@ import (
const ( const (
version = "v1.0.0" version = "v1.0.0"
userStateNone = userState("None")
userStateGo = userState("Go")
) )
var (
state = userStateNone
)
type userState string
type Opts struct { type Opts struct {
Token string `short:"t" long:"token" description:"Telegram api token"` Token string `short:"t" long:"token" description:"Telegram api token"`
@ -90,7 +83,13 @@ func run() {
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
// username := update.Message.From.UserName // 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) count, err := strconv.Atoi(text)
if err != nil { if err != nil {
continue continue
@ -101,7 +100,7 @@ func run() {
msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text)) msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text))
msg.ReplyMarkup = tgbot.NewHideKeyboard(false) msg.ReplyMarkup = tgbot.NewHideKeyboard(false)
_, _ = bot.Send(msg) _, _ = bot.Send(msg)
state = userStateNone db.SetStatusInUser(chatID, db.UserStateNone)
continue continue
} }
@ -123,7 +122,7 @@ func run() {
), ),
) )
if _, err = bot.Send(msg); err == nil { if _, err = bot.Send(msg); err == nil {
state = userStateGo db.SetStatusInUser(chatID, db.UserStateGo)
} }
} }
} }