@@ -13,6 +13,7 @@ import (
|
||||
const (
|
||||
UserStateNone = UserState("")
|
||||
UserStateGo = UserState("Go")
|
||||
UserStateEat = UserState("Eat")
|
||||
)
|
||||
|
||||
type UserState string
|
||||
@@ -46,10 +47,31 @@ func NewWorkout(
|
||||
count int,
|
||||
username string,
|
||||
) *Workout {
|
||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||
return &Workout{
|
||||
Name: name,
|
||||
Count: count,
|
||||
Username: username,
|
||||
Name: name,
|
||||
Count: count,
|
||||
Username: username,
|
||||
CreatedAt: time.Now().In(loc),
|
||||
}
|
||||
}
|
||||
|
||||
type Calories struct {
|
||||
ChatID int64 `bson:"chat_id"`
|
||||
Count int `bson:"count"`
|
||||
CreatedAt time.Time `bson:"created_at"`
|
||||
Username string `bson:"username"`
|
||||
}
|
||||
|
||||
func NewCalories(
|
||||
count int,
|
||||
username string,
|
||||
) *Calories {
|
||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||
return &Calories{
|
||||
Count: count,
|
||||
Username: username,
|
||||
CreatedAt: time.Now().In(loc),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,8 +110,6 @@ func AddWorkout(chatID int64, workout *Workout) error {
|
||||
return err
|
||||
}
|
||||
workout.ChatID = chatID
|
||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||
workout.CreatedAt = time.Now().In(loc)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
@@ -107,6 +127,31 @@ func AddWorkout(chatID int64, workout *Workout) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func AddCalories(chatID int64, calories *Calories) error {
|
||||
if calories.Count <= 0 {
|
||||
return nil
|
||||
}
|
||||
if err := AddChat(chatID); err != nil {
|
||||
return err
|
||||
}
|
||||
calories.ChatID = chatID
|
||||
|
||||
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.CaloriesCollectionName)
|
||||
_, err = collection.InsertOne(
|
||||
ctx,
|
||||
calories,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) {
|
||||
if err := AddChat(chatID); err != nil {
|
||||
return nil, err
|
||||
@@ -160,6 +205,8 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||
t := time.Now().In(loc).Add(-24 * time.Hour)
|
||||
|
||||
res := map[string]int{}
|
||||
|
||||
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
|
||||
cursor, err := collection.Find(
|
||||
ctx,
|
||||
@@ -171,7 +218,6 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := map[string]int{}
|
||||
for cursor.Next(context.Background()) {
|
||||
var result Workout
|
||||
if err := cursor.Decode(&result); err != nil {
|
||||
@@ -182,6 +228,29 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||
if err := cursor.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
caloriesCollection := client.Database(cfg.DBName).Collection(cfg.CaloriesCollectionName)
|
||||
caloriesCursor, err := caloriesCollection.Find(
|
||||
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
|
||||
if err := cursor.Decode(&result); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
res["Калории"] += result.Count
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -195,7 +264,7 @@ func GetAllChats() ([]int64, error) {
|
||||
}
|
||||
|
||||
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
||||
cursor, err := collection.Find(ctx,bson.M{})
|
||||
cursor, err := collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user