+45
-7
@@ -16,6 +16,7 @@ type DB struct {
|
||||
chatsColection *mongo.Collection
|
||||
workoutsColection *mongo.Collection
|
||||
caloriesColection *mongo.Collection
|
||||
weightColection *mongo.Collection
|
||||
}
|
||||
|
||||
func NewDB(
|
||||
@@ -35,6 +36,7 @@ func NewDB(
|
||||
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),
|
||||
weightColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.WeightCollectionName),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -82,9 +84,6 @@ func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
|
||||
func (db *DB) AddCalories(chatID int64, calories *Calories) error {
|
||||
ctx := context.Background()
|
||||
|
||||
if calories.Count <= 0 {
|
||||
return nil
|
||||
}
|
||||
if err := db.AddChat(chatID); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,6 +96,24 @@ func (db *DB) AddCalories(chatID int64, calories *Calories) error {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (db *DB) GetChatInfo(chatID int64, username string) (*ChatInfo, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -135,14 +152,14 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]int, error) {
|
||||
func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
if err := db.AddChat(chatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := map[string]int{}
|
||||
res := map[string]float64{}
|
||||
|
||||
cursor, err := db.workoutsColection.Find(
|
||||
ctx,
|
||||
@@ -159,7 +176,7 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]int, error) {
|
||||
if err := cursor.Decode(&result); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
res[result.Name] += result.Count
|
||||
res[result.Name] += float64(result.Count)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -180,12 +197,33 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]int, error) {
|
||||
if err := caloriesCursor.Decode(&result); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
res["Калории"] += result.Count
|
||||
res["Калории"] += float64(result.Count)
|
||||
}
|
||||
if err := caloriesCursor.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user