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

This commit is contained in:
Владимир Фёдоров 2023-03-18 14:35:05 +07:00
parent c52a1f7f40
commit 871b6c6dcd
3 changed files with 43 additions and 32 deletions

37
calories/calories.go Normal file
View File

@ -0,0 +1,37 @@
package calories
import (
"strconv"
"strings"
)
var (
caloriesMap = map[string]int{
"чай": 79,
"яблоко": 100,
}
)
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
}

View File

@ -1,4 +1,4 @@
package main
package calories
import "testing"

36
main.go
View File

@ -3,16 +3,18 @@ package main
import (
"errors"
"fmt"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"valera/calories"
"valera/commands"
"valera/db"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
)
var (
@ -21,10 +23,6 @@ var (
"Пресс",
"Подтягивания",
}
caloriesMap = map[string]int{
"чай": 79,
"яблоко": 100,
}
)
const (
@ -206,7 +204,7 @@ func run() {
_, _ = bot.Send(msg)
continue
case db.UserStateEat:
count, err := calcCalories(text)
count, err := calories.CalcCalories(text)
if err != nil {
log.Println(err)
continue
@ -254,30 +252,6 @@ 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, "Давай немного разомнемся, выбирай:")
rows := make([][]tgbot.KeyboardButton, 0, len(workoutTypes))