Skip to content

Commit

Permalink
Implement archiving transactions
Browse files Browse the repository at this point in the history
Refine info messages
  • Loading branch information
LucaBernstein committed Oct 28, 2021
1 parent 501734e commit 937d28f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 14 additions & 1 deletion db/crud/bot_transaction.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package crud

import "log"

func (r *Repo) RecordTransaction(chatId int64, tx string) error {
_, err := r.db.Exec(`INSERT INTO "bot::transaction" ("tgChatId", "value")
_, err := r.db.Exec(`
INSERT INTO "bot::transaction" ("tgChatId", "value")
VALUES ($1, $2);`, chatId, tx)
return err
}

func (r *Repo) GetTransactions(chatId int64) (string, error) {
log.Printf("Getting transactions for %d", chatId)
rows, err := r.db.Query(`
SELECT "value" FROM "bot::transaction"
WHERE "archived" = FALSE AND "tgChatId" = $1
Expand All @@ -29,3 +33,12 @@ func (r *Repo) GetTransactions(chatId int64) (string, error) {
}
return allTransactionsMessage, nil
}

func (r *Repo) ArchiveTransactions(chatId int64) error {
log.Printf("Archiving transactions for %d", chatId)
_, err := r.db.Exec(`
UPDATE "bot::transaction"
SET "archived" = TRUE
WHERE "tgChatId" = $1`, chatId)
return err
}
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,23 @@ func commandClear(b *tb.Bot, m *tb.Message) {
}

func commandArchiveTransactions(b *tb.Bot, m *tb.Message) {
b.Send(m.Sender, "TODO: NOT IMPLEMENTED YET")
err := CRUD_REPO.ArchiveTransactions(m.Chat.ID)
if err != nil {
b.Send(m.Sender, "Something went wrong archiving your transactions: "+err.Error())
return
}
b.Send(m.Sender, "Archived all transactions. Your /list is empty again.")
}

func commandList(b *tb.Bot, m *tb.Message) {
tx, err := CRUD_REPO.GetTransactions(m.Chat.ID)
if err != nil {
b.Send(m.Sender, "Something went wrong retrieving the transactions list: "+err.Error())
b.Send(m.Sender, "Something went wrong retrieving your transactions: "+err.Error())
return
}
if tx == "" {
b.Send(m.Sender, "Your transaction list is already empty. Create some first. Check /simple or /help for commands.")
return
}
b.Send(m.Sender, tx)
}
Expand Down

0 comments on commit 937d28f

Please sign in to comment.