2023-03-11 10:17:30 +00:00
|
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-03-11 11:30:12 +00:00
|
|
|
|
"log"
|
2023-03-11 10:17:30 +00:00
|
|
|
|
"time"
|
2023-04-05 18:18:25 +00:00
|
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2023-04-07 08:01:17 +00:00
|
|
|
|
|
|
|
|
|
"valera/internal/config"
|
2023-03-11 10:17:30 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
type DB struct {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
chatsColection *mongo.Collection
|
|
|
|
|
workoutsColection *mongo.Collection
|
|
|
|
|
caloriesColection *mongo.Collection
|
2023-04-09 08:48:31 +00:00
|
|
|
|
weightColection *mongo.Collection
|
2023-03-11 10:17:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func NewDB(
|
2023-04-07 08:01:17 +00:00
|
|
|
|
cfg *config.AppConfig,
|
2023-03-14 19:09:08 +00:00
|
|
|
|
) (*DB, error) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
opt := options.Client().
|
2023-04-07 08:01:17 +00:00
|
|
|
|
ApplyURI(cfg.MongoConfig.URL).
|
2023-03-14 19:09:08 +00:00
|
|
|
|
SetMinPoolSize(10)
|
|
|
|
|
|
|
|
|
|
client, err := mongo.Connect(ctx, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2023-03-12 06:53:50 +00:00
|
|
|
|
}
|
2023-03-14 19:09:08 +00:00
|
|
|
|
|
|
|
|
|
return &DB{
|
2023-04-07 08:01:17 +00:00
|
|
|
|
chatsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.ChatsCollectionName),
|
|
|
|
|
workoutsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.WorkoutsCollectionName),
|
|
|
|
|
caloriesColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.CaloriesCollectionName),
|
2023-04-09 08:48:31 +00:00
|
|
|
|
weightColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.WeightCollectionName),
|
2023-03-14 19:09:08 +00:00
|
|
|
|
}, nil
|
2023-03-12 06:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func (db *DB) AddChat(chatID int64) error {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
2023-03-11 10:17:30 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
result := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID})
|
2023-03-11 10:17:30 +00:00
|
|
|
|
if result.Err() == mongo.ErrNoDocuments {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
if _, err := db.chatsColection.InsertOne(ctx, &Chat{ChatID: chatID}); err != nil {
|
2023-03-11 10:17:30 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 07:47:38 +00:00
|
|
|
|
if result.Err() != nil {
|
|
|
|
|
return result.Err()
|
|
|
|
|
}
|
|
|
|
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
|
|
|
|
t := time.Now().In(loc)
|
|
|
|
|
_, err := db.chatsColection.UpdateOne(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"chat_id": chatID},
|
|
|
|
|
bson.M{"$set": bson.M{"last_time": t}},
|
|
|
|
|
)
|
|
|
|
|
return err
|
2023-03-11 10:17:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2023-03-11 12:38:01 +00:00
|
|
|
|
if workout.Count <= 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-12 19:38:48 +00:00
|
|
|
|
if err := db.AddChat(chatID); err != nil {
|
2023-03-11 10:17:30 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
2023-03-11 11:30:12 +00:00
|
|
|
|
workout.ChatID = chatID
|
2023-03-11 10:17:30 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
_, err := db.workoutsColection.InsertOne(
|
2023-03-11 10:17:30 +00:00
|
|
|
|
ctx,
|
2023-03-11 11:30:12 +00:00
|
|
|
|
workout,
|
2023-03-11 10:17:30 +00:00
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func (db *DB) AddCalories(chatID int64, calories *Calories) error {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
if err := db.AddChat(chatID); err != nil {
|
2023-03-12 06:53:50 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
calories.ChatID = chatID
|
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
_, err := db.caloriesColection.InsertOne(
|
2023-03-12 06:53:50 +00:00
|
|
|
|
ctx,
|
|
|
|
|
calories,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 08:48:31 +00:00
|
|
|
|
func (db *DB) AddWeight(chatID int64, weight *Weight) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
if weight.Weight <= 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if err := db.AddChat(chatID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
weight.ChatID = chatID
|
|
|
|
|
|
|
|
|
|
_, err := db.weightColection.InsertOne(
|
|
|
|
|
ctx,
|
|
|
|
|
weight,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 07:47:38 +00:00
|
|
|
|
func (db *DB) GetChatInfo(chatID int64, username string) (*ChatInfo, error) {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
2023-03-11 10:30:38 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
if err := db.AddChat(chatID); err != nil {
|
2023-03-11 10:30:38 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 07:47:38 +00:00
|
|
|
|
chatInfoDTO := &ChatInfo{Username: username}
|
2023-03-14 19:09:08 +00:00
|
|
|
|
if err := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
|
2023-03-11 10:30:38 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-03-14 19:09:08 +00:00
|
|
|
|
return chatInfoDTO, nil
|
2023-03-11 10:30:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
2023-03-11 10:30:38 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
_, err := db.chatsColection.UpdateOne(
|
2023-03-11 10:30:38 +00:00
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"chat_id": chatID},
|
|
|
|
|
bson.M{"$set": bson.M{"status": status}},
|
|
|
|
|
)
|
|
|
|
|
return err
|
2023-03-11 10:17:30 +00:00
|
|
|
|
}
|
2023-03-11 11:30:12 +00:00
|
|
|
|
|
2023-04-05 18:18:25 +00:00
|
|
|
|
func (db *DB) SetPause(chatID int64, duration time.Duration) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
|
|
|
|
t := time.Now().In(loc).Add(+duration)
|
|
|
|
|
_, err := db.chatsColection.UpdateOne(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"chat_id": chatID},
|
|
|
|
|
bson.M{"$set": bson.M{"pause_until": t}},
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 08:48:31 +00:00
|
|
|
|
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error) {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
2023-03-11 11:30:12 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
if err := db.AddChat(chatID); err != nil {
|
2023-03-11 11:30:12 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 08:48:31 +00:00
|
|
|
|
res := map[string]float64{}
|
2023-03-12 06:53:50 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
cursor, err := db.workoutsColection.Find(
|
2023-03-11 12:00:50 +00:00
|
|
|
|
ctx,
|
|
|
|
|
bson.M{
|
|
|
|
|
"chat_id": chatID,
|
|
|
|
|
"created_at": bson.M{"$gt": t},
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-03-11 11:30:12 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for cursor.Next(context.Background()) {
|
|
|
|
|
var result Workout
|
|
|
|
|
if err := cursor.Decode(&result); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2023-04-09 08:48:31 +00:00
|
|
|
|
res[result.Name] += float64(result.Count)
|
2023-03-11 11:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2023-03-12 06:53:50 +00:00
|
|
|
|
|
2023-03-14 19:09:08 +00:00
|
|
|
|
caloriesCursor, err := db.caloriesColection.Find(
|
2023-03-12 06:53:50 +00:00
|
|
|
|
ctx,
|
|
|
|
|
bson.M{
|
|
|
|
|
"chat_id": chatID,
|
|
|
|
|
"created_at": bson.M{"$gt": t},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for caloriesCursor.Next(context.Background()) {
|
|
|
|
|
var result Calories
|
2023-03-12 07:06:42 +00:00
|
|
|
|
if err := caloriesCursor.Decode(&result); err != nil {
|
2023-03-12 06:53:50 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2023-04-09 08:48:31 +00:00
|
|
|
|
res["Калории"] += float64(result.Count)
|
2023-03-12 06:53:50 +00:00
|
|
|
|
}
|
2023-03-12 07:06:42 +00:00
|
|
|
|
if err := caloriesCursor.Err(); err != nil {
|
2023-03-12 06:53:50 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 08:48:31 +00:00
|
|
|
|
weightCursor, err := db.weightColection.Find(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{
|
|
|
|
|
"chat_id": chatID,
|
|
|
|
|
"created_at": bson.M{"$gt": t},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for weightCursor.Next(context.Background()) {
|
|
|
|
|
var result Weight
|
|
|
|
|
if err := weightCursor.Decode(&result); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
res["Вес кг"] = result.Weight
|
|
|
|
|
}
|
|
|
|
|
if err := weightCursor.Err(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-11 11:30:12 +00:00
|
|
|
|
return res, err
|
|
|
|
|
}
|
2023-03-11 12:38:01 +00:00
|
|
|
|
|
2023-03-12 19:38:48 +00:00
|
|
|
|
func (db *DB) GetAllChats() ([]int64, error) {
|
2023-03-14 19:09:08 +00:00
|
|
|
|
ctx := context.Background()
|
2023-03-11 12:38:01 +00:00
|
|
|
|
|
2023-04-05 18:18:25 +00:00
|
|
|
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
|
|
|
|
t := time.Now().In(loc)
|
|
|
|
|
cursor, err := db.chatsColection.Find(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{
|
|
|
|
|
"$or": bson.A{
|
|
|
|
|
bson.M{
|
|
|
|
|
"pause_until": bson.M{"$lt": t},
|
|
|
|
|
},
|
|
|
|
|
bson.M{
|
|
|
|
|
"pause_until": bson.M{"$exists": false},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-03-11 12:38:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var res []int64
|
|
|
|
|
for cursor.Next(context.Background()) {
|
2023-03-12 19:38:48 +00:00
|
|
|
|
var result ChatInfo
|
2023-03-11 12:38:01 +00:00
|
|
|
|
if err := cursor.Decode(&result); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, result.ChatID)
|
|
|
|
|
}
|
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|