This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ledger_account.go
107 lines (86 loc) · 2.59 KB
/
ledger_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package moneybird
import "encoding/json"
// LedgerAccount represents a ledger account in Moneybird
type LedgerAccount struct {
ID string `json:"id,omitempty"`
AdministrationID string `json:"administration_id,omitempty"`
Name string `json:"name"`
AccountType string `json:"account_type"`
AccountID string `json:"account_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// LedgerAccountGateway encapsulates all /ledger_accounts related endpoints
type LedgerAccountGateway struct {
*Client
}
// LedgerAccount returns a new gateway instance
func (c *Client) LedgerAccount() *LedgerAccountGateway {
return &LedgerAccountGateway{c}
}
// List returns all ledger accounts in Moneybird
func (c *LedgerAccountGateway) List() ([]*LedgerAccount, error) {
var ledgerAccounts []*LedgerAccount
var err error
res, err := c.execute("GET", "ledger_accounts", nil)
if err != nil {
return nil, err
}
switch res.StatusCode {
case 200:
err = json.NewDecoder(res.Body).Decode(&ledgerAccounts)
return ledgerAccounts, err
}
return nil, res.error()
}
// Get returns the ledger account with the specified id, or nil
func (c *LedgerAccountGateway) Get(ID string) (*LedgerAccount, error) {
var err error
res, err := c.execute("GET", "ledger_accounts/"+ID, nil)
if err != nil {
return nil, err
}
// TODO: Check status code here.
switch res.StatusCode {
case 200:
return res.ledgerAccount()
}
return nil, err
}
// Create adds a ledger account to MoneyBird
func (c *LedgerAccountGateway) Create(ledgerAccount *LedgerAccount) (*LedgerAccount, error) {
res, err := c.execute("POST", "ledger_accounts", &envelope{LedgerAccount: ledgerAccount})
if err != nil {
return ledgerAccount, err
}
switch res.StatusCode {
case 201:
return res.ledgerAccount()
}
return nil, err
}
// Update updates an existing ledger account in Moneybird
func (c *LedgerAccountGateway) Update(ledgerAccount *LedgerAccount) (*LedgerAccount, error) {
res, err := c.execute("PATCH", "ledger_accounts/"+ledgerAccount.ID, &envelope{LedgerAccount: ledgerAccount})
if err != nil {
return ledgerAccount, err
}
switch res.StatusCode {
case 200:
return res.ledgerAccount()
}
return nil, err
}
// Delete the given ledger account
func (c *LedgerAccountGateway) Delete(ledgerAccount *LedgerAccount) error {
res, err := c.execute("DELETE", "ledger_accounts/"+ledgerAccount.ID, nil)
if err != nil {
return err
}
switch res.StatusCode {
case 204:
return nil
}
return err
}