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

This commit is contained in:
Владимир Фёдоров 2023-03-14 01:15:11 +07:00
parent 922e770405
commit 01e132628c
2 changed files with 84 additions and 2 deletions

32
main.go
View File

@ -20,6 +20,10 @@ var (
"Отжимания", "Отжимания",
"Пресс", "Пресс",
} }
caloriesMap = map[string]int{
"чай": 79,
"яблоко": 100,
}
) )
const ( const (
@ -198,7 +202,7 @@ func run() {
_, _ = bot.Send(msg) _, _ = bot.Send(msg)
continue continue
case db.UserStateEat: case db.UserStateEat:
count, err := strconv.Atoi(text) count, err := calcCalories(text)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
@ -211,7 +215,7 @@ func run() {
_, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой")) _, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой"))
continue continue
} }
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %s, записал.", text))) _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Калории, фу, %d, записал.", count)))
if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil { if err := dataBase.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err) log.Println(err)
} }
@ -246,6 +250,30 @@ func run() {
} }
} }
func calcCalories(text string) (int, error) {
count, ok := caloriesMap[strings.ToLower(text)]
if ok {
return count, nil
}
arr := strings.Split(text, " ")
if len(arr) == 2 {
coun1, err := strconv.Atoi(arr[0])
if err != nil {
return 0, nil
}
count2, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return coun1 * count2 / 100, nil
}
count, err := strconv.Atoi(text)
if err != nil {
return 0, nil
}
return count, nil
}
func sendGoToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64) { func sendGoToChat(bot *tgbot.BotAPI, dataBase *db.DB, chatID int64) {
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, выбирай:") msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, выбирай:")
row := tgbot.NewKeyboardButtonRow() row := tgbot.NewKeyboardButtonRow()

54
main_test.go Normal file
View File

@ -0,0 +1,54 @@
package main
import "testing"
func TestCalcCalories(t *testing.T) {
t.Parallel()
tests := []struct {
name string
text string
want int
wantErr bool
}{
{
"empty text",
"",
0,
false,
},
{
"1 text",
"1",
1,
false,
},
{
"2 5 text",
"20 50",
10,
false,
},
{
"чай text",
"чай",
39,
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
count, err := calcCalories(tt.text)
if (err != nil) != tt.wantErr {
t.Errorf("error calc calories: %v", err)
return
}
if count != tt.want {
t.Errorf("error count: %v", err)
return
}
})
}
}