add calories
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
daa52ecbde
commit
f1f3fb5db4
|
@ -8,4 +8,5 @@ const (
|
||||||
Ping = Command("/ping")
|
Ping = Command("/ping")
|
||||||
Go = Command("/go")
|
Go = Command("/go")
|
||||||
Stat = Command("/stat")
|
Stat = Command("/stat")
|
||||||
|
Eat = Command("/eat")
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,6 +5,7 @@ type Config struct {
|
||||||
DBName string
|
DBName string
|
||||||
ChatsCollectionName string
|
ChatsCollectionName string
|
||||||
WorkoutsCollectionName string
|
WorkoutsCollectionName string
|
||||||
|
CaloriesCollectionName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
|
@ -13,5 +14,6 @@ func NewConfig() *Config {
|
||||||
DBName: "valera",
|
DBName: "valera",
|
||||||
ChatsCollectionName: "chats",
|
ChatsCollectionName: "chats",
|
||||||
WorkoutsCollectionName: "workouts",
|
WorkoutsCollectionName: "workouts",
|
||||||
|
CaloriesCollectionName: "calories",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
83
db/db.go
83
db/db.go
|
@ -13,6 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
UserStateNone = UserState("")
|
UserStateNone = UserState("")
|
||||||
UserStateGo = UserState("Go")
|
UserStateGo = UserState("Go")
|
||||||
|
UserStateEat = UserState("Eat")
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserState string
|
type UserState string
|
||||||
|
@ -46,10 +47,31 @@ func NewWorkout(
|
||||||
count int,
|
count int,
|
||||||
username string,
|
username string,
|
||||||
) *Workout {
|
) *Workout {
|
||||||
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||||
return &Workout{
|
return &Workout{
|
||||||
Name: name,
|
Name: name,
|
||||||
Count: count,
|
Count: count,
|
||||||
Username: username,
|
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
|
return err
|
||||||
}
|
}
|
||||||
workout.ChatID = chatID
|
workout.ChatID = chatID
|
||||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
|
||||||
workout.CreatedAt = time.Now().In(loc)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -107,6 +127,31 @@ func AddWorkout(chatID int64, workout *Workout) error {
|
||||||
return err
|
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) {
|
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) {
|
||||||
if err := AddChat(chatID); err != nil {
|
if err := AddChat(chatID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -160,6 +205,8 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||||
t := time.Now().In(loc).Add(-24 * time.Hour)
|
t := time.Now().In(loc).Add(-24 * time.Hour)
|
||||||
|
|
||||||
|
res := map[string]int{}
|
||||||
|
|
||||||
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
|
collection := client.Database(cfg.DBName).Collection(cfg.WorkoutsCollectionName)
|
||||||
cursor, err := collection.Find(
|
cursor, err := collection.Find(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -171,7 +218,6 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
res := map[string]int{}
|
|
||||||
for cursor.Next(context.Background()) {
|
for cursor.Next(context.Background()) {
|
||||||
var result Workout
|
var result Workout
|
||||||
if err := cursor.Decode(&result); err != nil {
|
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 {
|
if err := cursor.Err(); err != nil {
|
||||||
log.Fatal(err)
|
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
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,7 +264,7 @@ func GetAllChats() ([]int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
30
main.go
30
main.go
|
@ -133,7 +133,8 @@ func run() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if userInfoDTO.GetStatus() == db.UserStateGo {
|
switch userInfoDTO.GetStatus() {
|
||||||
|
case db.UserStateGo:
|
||||||
count, err := strconv.Atoi(text)
|
count, err := strconv.Atoi(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -154,6 +155,25 @@ func run() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
continue
|
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))
|
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
|
||||||
|
@ -164,7 +184,7 @@ func run() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
case commands.Help:
|
case commands.Help:
|
||||||
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n2) Показывать статистику"))
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Предлагать размяться\n2) Показывать статистику\n3) Считать калории"))
|
||||||
case commands.Ping:
|
case commands.Ping:
|
||||||
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
||||||
case commands.Go:
|
case commands.Go:
|
||||||
|
@ -174,6 +194,12 @@ func run() {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
continue
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue