Skip to content

Commit

Permalink
chore: add /lcd_delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 30, 2024
1 parent 834b715 commit f91f052
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ var (
ErrWrongInvocation = errors.New("wrong invocation")
ErrChainNotFound = fmt.Errorf("chain not found")
ErrChainNotBound = fmt.Errorf("chain not bound to this chat")
ErrLCDNotFound = fmt.Errorf("chain LCD host not found")
)
11 changes: 11 additions & 0 deletions pkg/database/lcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ func (d *Database) InsertLCDHost(chain *types.Chain, host string) error {

return nil
}

func (d *Database) DeleteLCDHost(chain *types.Chain, host string) (bool, error) {
result, err := d.client.Exec("DELETE FROM lcd WHERE chain = $1 AND host = $2", chain.Name, host)
if err != nil {
d.logger.Error().Err(err).Msg("Could not delete LCD host")
return false, err
}

rowsAffected, _ := result.RowsAffected()
return rowsAffected > 0, nil
}
51 changes: 51 additions & 0 deletions pkg/interacter/telegram/lcd_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package telegram

import (
"fmt"
"html"
"main/pkg/constants"
"main/pkg/types"
"strings"

tele "gopkg.in/telebot.v3"
)

func (interacter *Interacter) GetLCDDeleteCommand() Command {
return Command{
Name: "lcd_delete",
Execute: interacter.HandleDeleteLCD,
}
}

func (interacter *Interacter) HandleDeleteLCD(c tele.Context, chainBinds []string) (string, error) {
args := strings.SplitN(c.Text(), " ", 3)
if len(args) < 3 {
return html.EscapeString(fmt.Sprintf("Usage: %s <chain name> <host>", args[0])), constants.ErrWrongInvocation
}

chainName, host := args[1], args[2]
chain, err := interacter.Database.GetChainByName(chainName)
if err != nil {
return "Error finding chain!", err
}

allLCDs, err := interacter.Database.GetLCDHosts(chain)
if err != nil {
return "Error finding LCD hosts!", err
}

if len(allLCDs) <= 1 {
return "Cannot remove the only chain LCD!", constants.ErrWrongInvocation
}

deleted, insertErr := interacter.Database.DeleteLCDHost(chain, host)
if insertErr != nil {
return "Error deleting LCD host!", insertErr
}

if !deleted {
return "Chain LCD host was not found!", constants.ErrLCDNotFound
}

return interacter.TemplateManager.Render("lcd_delete", types.ChainWithLCD{Chain: *chain, LCDEndpoint: host})
}
1 change: 1 addition & 0 deletions pkg/interacter/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (interacter *Interacter) Init() {
interacter.AddCommand("/denom_add", bot, interacter.GetDenomAddCommand())
interacter.AddCommand("/denom_delete", bot, interacter.GetDenomDeleteCommand())
interacter.AddCommand("/lcd_add", bot, interacter.GetLCDAddCommand())
interacter.AddCommand("/lcd_delete", bot, interacter.GetLCDDeleteCommand())

interacter.TelegramBot = bot
}
Expand Down
3 changes: 3 additions & 0 deletions templates/telegram/lcd_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Successfully deleted LCD host!
<strong>Chain name:</strong> <code>{{ .Chain.Name }}</code>
<strong>LCD endpoint:</strong> <code>{{ .LCDEndpoint }}</code>

0 comments on commit f91f052

Please sign in to comment.