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

This commit is contained in:
2023-04-09 15:48:31 +07:00
parent ada1a97999
commit a35a5c8efc
9 changed files with 171 additions and 20 deletions
+5 -4
View File
@@ -1,10 +1,11 @@
package db
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
UserStatePause = UserState("Pause")
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
UserStatePause = UserState("Pause")
UserStateWeight = UserState("Weight")
)
type UserState string
+45 -7
View File
@@ -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
}
+22
View File
@@ -0,0 +1,22 @@
package db
import "time"
type Weight struct {
ChatID int64 `bson:"chat_id"`
Weight float64 `bson:"weight"`
CreatedAt time.Time `bson:"created_at"`
Username string `bson:"username"`
}
func NewWeight(
weight float64,
username string,
) *Weight {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
return &Weight{
Weight: weight,
Username: username,
CreatedAt: time.Now().In(loc),
}
}