From 937d28f3e7fdda0ec5d52966e8a60c1ebb2bb056 Mon Sep 17 00:00:00 2001 From: Luca Bernstein Date: Thu, 28 Oct 2021 21:01:47 +0200 Subject: [PATCH] Implement archiving transactions Refine info messages --- db/crud/bot_transaction.go | 15 ++++++++++++++- main.go | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/db/crud/bot_transaction.go b/db/crud/bot_transaction.go index 3aa5de8..b6ad1a7 100644 --- a/db/crud/bot_transaction.go +++ b/db/crud/bot_transaction.go @@ -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 @@ -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 +} diff --git a/main.go b/main.go index ece7f11..3780614 100644 --- a/main.go +++ b/main.go @@ -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) }