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

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
+49 -17
View File
@@ -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
}