add new all command

This commit is contained in:
2022-04-25 03:07:52 +07:00
parent 42f73f84fd
commit 3e0bfc433b
2 changed files with 29 additions and 9 deletions
+23 -1
View File
@@ -10,7 +10,8 @@ import (
)
type chatDTO struct {
ChatID int64 `bson:"chat_id"`
ChatID int64 `bson:"chat_id"`
Users []string `bson:"users"`
}
var cfg *config.Config
@@ -41,6 +42,10 @@ func AddChat(chatID int64) error {
}
func AddUserInChat(chatID int64, username string) error {
if username == "" {
return nil
}
if err := AddChat(chatID); err != nil {
return err
}
@@ -61,3 +66,20 @@ func AddUserInChat(chatID int64, username string) 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)
chatDTO := &chatDTO{}
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatDTO); err != nil {
return nil, err
}
return chatDTO.Users, err
}