update calories
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
922e770405
commit
01e132628c
32
main.go
32
main.go
|
@ -20,6 +20,10 @@ var (
|
|||
"Отжимания",
|
||||
"Пресс",
|
||||
}
|
||||
caloriesMap = map[string]int{
|
||||
"чай": 79,
|
||||
"яблоко": 100,
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -198,7 +202,7 @@ func run() {
|
|||
_, _ = bot.Send(msg)
|
||||
continue
|
||||
case db.UserStateEat:
|
||||
count, err := strconv.Atoi(text)
|
||||
count, err := calcCalories(text)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
|
@ -211,7 +215,7 @@ func run() {
|
|||
_, _ = bot.Send(tgbot.NewMessage(chatID, "Все фигня, давай по новой"))
|
||||
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 {
|
||||
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) {
|
||||
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, выбирай:")
|
||||
row := tgbot.NewKeyboardButtonRow()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue