func (h *Handlers) AddCategoryHandler() tele.HandlerFunc {
return func(tb tele.Context) error {
ctx := context.Background()
senderID := int(tb.Sender().ID)
if err := h.fsm.SetState(ctx, senderID, fsm.InputAddCategoryState); err != nil {
h.logger.Err(err).Msg("failed to set state")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
selector.Inline(selector.Row(BackBtn))
if err := tb.EditCaption("<b> Работа с категориями</b>\n\nℹ️ Введите название категории:", selector); err != nil {
h.logger.Err(err).Msg("failed to send message")
return nil
}
tb.Bot().Handle(tele.OnText, func(tbText tele.Context) error {
currentState, err := h.fsm.GetState(ctx, senderID)
if err != nil {
h.logger.Err(err).Msg("failed to get state")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
if currentState != fsm.InputAddCategoryState {
return nil
}
categoryName := strings.TrimSpace(tbText.Message().Text)
if err = tbText.Delete(); err != nil {
h.logger.Err(err).Msg("failed to delete message")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
if err = h.categoriesService.CategoryWebService.Save(ctx, categoryName); err != nil {
if err = h.fsm.SetState(ctx, senderID, fsm.StartState); err != nil {
h.logger.Err(err).Msg("failed to set state")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
h.logger.Err(err).Msg("failed to save new category")
return tb.EditCaption(messages.InternalErrorStr, selector)
}
if err = h.fsm.SetState(ctx, senderID, fsm.StartState); err != nil {
h.logger.Err(err).Msg("failed to set state")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
selector.Inline(selector.Row(BackAdminPanelBtn))
return tb.EditCaption(fmt.Sprintf("✅ Новая категория с именем <b>%s</b> успешно добавлена!", categoryName), selector)
})
tb.Bot().Handle(&BackBtn, func(tb tele.Context) error {
if err := h.fsm.SetState(ctx, senderID, fsm.StartState); err != nil {
h.logger.Err(err).Msg("failed to set state")
return tb.Respond(&tele.CallbackResponse{
CallbackID: tb.Callback().ID,
Text: messages.InternalErrorStr,
ShowAlert: false,
URL: "",
})
}
return h.categoryPanel(tb)
})
return nil
}
}