Skip to content

Commit

Permalink
Fix first issues with templates (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaBernstein authored Apr 11, 2022
1 parent cd0cc77 commit 8ee3c95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 10 additions & 5 deletions bot/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,20 @@ func (bc *BotController) templatesHandleRemove(m *tb.Message, params ...string)
return
}
name := params[0]
err := bc.Repo.RmTemplate(m.Chat.ID, string(name))
wasRemoved, err := bc.Repo.RmTemplate(m.Chat.ID, string(name))
if err != nil {
_, err := bc.Bot.Send(m.Sender, "Something went wrong while deleting your template.")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
return
}
if !wasRemoved {
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("There was no template called '%s' to remove. Please check '/t list'.", name))
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
return
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Successfully removed your template '%s'.", name))
if err != nil {
Expand Down Expand Up @@ -190,10 +198,7 @@ func (bc *BotController) templatesUse(m *tb.Message, params ...string) error {
tx, err := bc.State.TemplateTx(m, res[0].Template, bc.Repo.UserGetCurrency(m), date)
if err != nil {
bc.Logf(ERROR, m, "Creating tx from template failed: %s", err.Error())
_, err := bc.Bot.Send(m.Sender, "Something went wrong while creating a transaction from your template")
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
return fmt.Errorf("something went wrong creating a transaction from your template: %s", err.Error())
}
_, err = bc.Bot.Send(m.Sender, fmt.Sprintf("Creating a new transaction from your template '%s'.", name))
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions db/crud/bot_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func (r *Repo) AddTemplate(chatId int64, name, template string) error {
return err
}

func (r *Repo) RmTemplate(chatId int64, name string) error {
_, err := r.db.Exec(fmt.Sprintf(`DELETE FROM "%s" WHERE "tgChatId" = $1 AND "name" = $2;`, DB_TABLE_TEMPLATES),
func (r *Repo) RmTemplate(chatId int64, name string) (bool, error) {
res, err := r.db.Exec(fmt.Sprintf(`DELETE FROM "%s" WHERE "tgChatId" = $1 AND "name" = $2;`, DB_TABLE_TEMPLATES),
chatId, name)
return err
rows, _ := res.RowsAffected()
return rows > 0, err
}

0 comments on commit 8ee3c95

Please sign in to comment.