Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatting query argument to list API #251

Merged
merged 3 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <number>`: Remove a single transaction from the list
* `/archiveAll`: Mark all currently opened transactions as archived. They can be revisited using `/list archived`.
Expand Down
12 changes: 12 additions & 0 deletions api/transactions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions api/transactions/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading