From 871b6c6dcd7bd1905a2f07bf08861b52173b1b32 Mon Sep 17 00:00:00 2001 From: FedorovVladimir Date: Sat, 18 Mar 2023 14:35:05 +0700 Subject: [PATCH] add calories --- calories/calories.go | 37 +++++++++++++++++++++++ main_test.go => calories/calories_test.go | 2 +- main.go | 36 +++------------------- 3 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 calories/calories.go rename main_test.go => calories/calories_test.go (97%) diff --git a/calories/calories.go b/calories/calories.go new file mode 100644 index 0000000..a91bd66 --- /dev/null +++ b/calories/calories.go @@ -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 +} diff --git a/main_test.go b/calories/calories_test.go similarity index 97% rename from main_test.go rename to calories/calories_test.go index 81f47e6..39573c0 100644 --- a/main_test.go +++ b/calories/calories_test.go @@ -1,4 +1,4 @@ -package main +package calories import "testing" diff --git a/main.go b/main.go index 9e952e7..b1d4f72 100644 --- a/main.go +++ b/main.go @@ -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))