add calories
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-03-12 13:53:50 +07:00
parent daa52ecbde
commit f1f3fb5db4
4 changed files with 107 additions and 9 deletions

View File

@ -8,4 +8,5 @@ const (
Ping = Command("/ping")
Go = Command("/go")
Stat = Command("/stat")
Eat = Command("/eat")
)

View File

@ -5,6 +5,7 @@ type Config struct {
DBName string
ChatsCollectionName string
WorkoutsCollectionName string
CaloriesCollectionName string
}
func NewConfig() *Config {
@ -13,5 +14,6 @@ func NewConfig() *Config {
DBName: "valera",
ChatsCollectionName: "chats",
WorkoutsCollectionName: "workouts",
CaloriesCollectionName: "calories",
}
}

View File

@ -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
}

30
main.go
View File

@ -133,7 +133,8 @@ func run() {
log.Println(err)
continue
}
if userInfoDTO.GetStatus() == db.UserStateGo {
switch userInfoDTO.GetStatus() {
case db.UserStateGo:
count, err := strconv.Atoi(text)
if err != nil {
log.Println(err)
@ -154,6 +155,25 @@ func run() {
log.Println(err)
}
continue
case db.UserStateEat:
count, err := strconv.Atoi(text)
if err != nil {
log.Println(err)
continue
}
if err := db.AddCalories(chatID, db.NewCalories(count, username)); err != nil {
log.Println(err)
continue
}
if count <= 0 {
_, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой"))
continue
}
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %s, записал.", text)))
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
continue
}
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
@ -164,7 +184,7 @@ func run() {
log.Println(err)
}
case commands.Help:
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n2) Показывать статистику"))
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n2) Показывать статистику\n3) Считать калории"))
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go:
@ -174,6 +194,12 @@ func run() {
fmt.Println(err)
continue
}
case commands.Eat:
if _, err := bot.Send(tgbot.NewMessage(chatID, "Вижу ты поел, отпишись сколько калорий было")); err == nil {
if err := db.SetStatusToChat(chatID, db.UserStateEat); err != nil {
log.Println(err)
}
}
}
}
}