diff --git a/README.md b/README.md index 5f0d52b..9b24e50 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ You can use the bot [`@LB_Bean_Bot`](https://t.me/LB_Bean_Bot) ([https://t.me/LB * `/t myTemplate`: Use the template created before. For all variables used, the value to use will be asked. It is possible to call a template with only a subset of its name, as long as it's uniquely identifiable, e.g. `/t myTempl` * `/cancel`: Cancel either the current transaction recording questionnaire or the creation of a new template. * `/comment` or `/c`: Add arbitrary text to the transaction list (e.g. for follow-ups). Example: `/c Checking account balance needs to be asserted`. (Note that no comment prefix (`;`) is added automatically, so that by default the entered comment string causes a syntax error in a beancount file to ease follow-up and so that comments don't drown in long transaction lists) -* `/list`: Show a list of all currently recorded transactions (for easy copy-and-paste into your beancount file). The parameter `/list dated` adds a comment prior to each transaction in the list with the date and time the transaction has been added. `/list archived` shows all archived transactions. The parameters can also be used in conjunction, i.e. `/list archived dated`. +* `/list`: Show a list of all currently recorded transactions (for easy copy-and-paste into your beancount file). The parameter `/list dated` adds a comment prior to each transaction in the list with the date and time the transaction has been added. `/list archived` shows all archived transactions. The parameters can also be used in conjunction, i.e. `/list archived dated`. When using the REST API, you can get a plain text list by adding `?format=text` to the URL. * `/list [archived] numbered`: Shows the transactions list with preceded number identifier. * `/list [archived] rm `: Remove a single transaction from the list * `/archiveAll`: Mark all currently opened transactions as archived. They can be revisited using `/list archived`. diff --git a/api/transactions/list.go b/api/transactions/list.go index a25667b..66a9c20 100644 --- a/api/transactions/list.go +++ b/api/transactions/list.go @@ -37,6 +37,8 @@ func (r *Router) List(c *gin.Context) { }) return } + format := c.Query("format") + transactions := []Transaction{} for _, t := range tx { transactions = append(transactions, Transaction{ @@ -46,6 +48,16 @@ func (r *Router) List(c *gin.Context) { IsArchived: isArchived, }) } + + if format == "text" { + var textResponse string + for _, t := range transactions { + textResponse += t.Booking + "\n" + } + c.String(http.StatusOK, textResponse) + return + } + c.JSON(http.StatusOK, transactions) } diff --git a/api/transactions/list_test.go b/api/transactions/list_test.go index d1aeb57..612c0f8 100644 --- a/api/transactions/list_test.go +++ b/api/transactions/list_test.go @@ -58,6 +58,18 @@ func TestList(t *testing.T) { assert.Contains(t, w.Body.String(), `"booking":"my tx"`) } +func TestListTextFormat(t *testing.T) { + r, w, token, _, _ := mockBcApiUser(t) + + + req, _ := http.NewRequest("GET", "/list?format=text", nil) + req.Header.Add("Authorization", "Bearer "+token) + r.ServeHTTP(w, req) + + assert.Equal(t, 200, w.Code) + assert.Equal(t, "my tx\n", w.Body.String()) +} + func TestListDeleteSingle(t *testing.T) { r, w, token, repo, msg := mockBcApiUser(t)