2023-03-11 10:17:30 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"time"
|
|
|
|
"valera/config"
|
|
|
|
)
|
|
|
|
|
2023-03-11 10:30:38 +00:00
|
|
|
const (
|
|
|
|
UserStateNone = UserState("")
|
|
|
|
UserStateGo = UserState("Go")
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserState string
|
|
|
|
|
2023-03-11 10:17:30 +00:00
|
|
|
type chatDTO struct {
|
|
|
|
ChatID int64 `bson:"chat_id"`
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:30:38 +00:00
|
|
|
type ChatInfoDTO struct {
|
2023-03-11 10:17:30 +00:00
|
|
|
ChatID int64 `bson:"chat_id"`
|
2023-03-11 10:30:38 +00:00
|
|
|
Status string `bson:"status"`
|
2023-03-11 10:17:30 +00:00
|
|
|
Workouts []Workout `bson:"workouts"`
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:30:38 +00:00
|
|
|
func (c *ChatInfoDTO) GetStatus() UserState {
|
|
|
|
if c.Status == "" {
|
|
|
|
return UserStateNone
|
|
|
|
}
|
|
|
|
return UserStateGo
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:17:30 +00:00
|
|
|
type Workout struct {
|
|
|
|
Name string `bson:"name"`
|
|
|
|
Count int `bson:"count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorkout(
|
|
|
|
name string,
|
|
|
|
count int,
|
|
|
|
) *Workout {
|
|
|
|
return &Workout{
|
|
|
|
Name: name,
|
|
|
|
Count: count,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg *config.Config
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cfg = config.NewConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddChat(chatID int64) 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)
|
|
|
|
|
|
|
|
result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
|
|
|
|
if result.Err() == mongo.ErrNoDocuments {
|
|
|
|
if _, err := collection.InsertOne(ctx, &chatDTO{ChatID: chatID}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return result.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddWorkoutInUser(chatID int64, workout *Workout) error {
|
|
|
|
if err := AddChat(chatID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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{"$push": bson.M{"workouts": workout}},
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:30:38 +00:00
|
|
|
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
|
2023-03-11 10:17:30 +00:00
|
|
|
}
|