/
rm_flat.go
68 lines (55 loc) · 1.77 KB
/
rm_flat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package internal
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"strconv"
)
type rmFlatCommand struct {
db *botDB
}
func (cmd *rmFlatCommand) Exec(upd *tgbotapi.Update) (tgbotapi.MessageConfig, bool) {
if upd.Message.IsCommand() {
flats := cmd.db.getUserFlats(upd.Message.Chat.ID)
if len(flats) == 0 {
text := fmt.Sprintf("Нет добавленных квартир.")
msg := tgbotapi.NewMessage(upd.Message.Chat.ID, text)
return msg, false
}
text := fmt.Sprintf("Выберите квартиру для удаления.")
markup := tgbotapi.NewReplyKeyboard()
markup.OneTimeKeyboard = true
for _, flat := range flats {
row := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton(strconv.Itoa(flat)))
markup.Keyboard = append(markup.Keyboard, row)
}
msg := tgbotapi.NewMessage(upd.Message.Chat.ID, text)
msg.ReplyMarkup = markup
return msg, true
}
n, err := strconv.ParseInt(upd.Message.Text, 10, 32)
flat := int(n)
if err != nil || flat < minFlat || flat > maxFlat {
text := fmt.Sprintf("Неверный номер.")
msg := tgbotapi.NewMessage(upd.Message.Chat.ID, text)
return msg, false
}
cmd.db.removeUserFlat(upd.Message.Chat.ID, flat)
text := fmt.Sprintf("Квартира %d удалена.", flat)
msg := tgbotapi.NewMessage(upd.Message.Chat.ID, text)
return msg, false
}
type rmFlatCommandCreator struct {
db *botDB
}
func (cc *rmFlatCommandCreator) BotCommand() tgbotapi.BotCommand {
return tgbotapi.BotCommand{
Command: "rm_flat",
Description: "убрать квартиру из списка",
}
}
func (cc *rmFlatCommandCreator) Create() Command {
return &rmFlatCommand{db: cc.db}
}
func newRmFlatCommandCreator(db *botDB) CommandCreator {
return &rmFlatCommandCreator{db: db}
}