forked from Invoiced/invoiced-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.go
106 lines (71 loc) · 2.25 KB
/
notification.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
package invdapi
import (
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
"strconv"
)
type Notification struct {
*Connection
*invdendpoint.Notification
}
type Notifications []*Notification
func (c *Connection) NewNotification() *Notification {
notification := new(invdendpoint.Notification)
return &Notification{c, notification}
}
func (c *Notification) Create(notificationRequest *invdendpoint.NotificationRequest) (*Notification, error) {
endpoint := invdendpoint.NotificationEndpoint
notificationResp := new(Notification)
apiErr := c.create(endpoint, notificationRequest, notificationResp)
if apiErr != nil {
return nil, apiErr
}
notificationResp.Connection = c.Connection
return notificationResp, nil
}
func (c *Notification) Save(notificationRequest *invdendpoint.NotificationRequest, id int64) error {
endpoint := invdendpoint.NotificationEndpoint + "/" + strconv.FormatInt(id, 10)
notifResp := new(Notification)
apiErr := c.update(endpoint, notificationRequest, notifResp)
if apiErr != nil {
return apiErr
}
notifResp.Connection = c.Connection
return nil
}
func (c *Notification) Delete(id int64) error {
endpoint := invdendpoint.NotificationEndpoint + "/" + strconv.FormatInt(id, 10)
err := c.delete(endpoint)
if err != nil {
return err
}
return nil
}
func (c *Notification) Retrieve(id int64) (*Notification, error) {
endpoint := invdendpoint.NotificationEndpoint + "/" + strconv.FormatInt(id, 10)
notifResp := new(Notification)
_, err := c.retrieveDataFromAPI(endpoint, notifResp)
if err != nil {
return nil, err
}
notifResp.Connection = c.Connection
return notifResp, nil
}
func (c *Notification) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Notifications, error) {
endpoint := invdendpoint.NotificationEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
notifications := make(Notifications, 0)
NEXT:
tmpNotifications := make(Notifications, 0)
endpoint, apiErr := c.retrieveDataFromAPI(endpoint, &tmpNotifications)
if apiErr != nil {
return nil, apiErr
}
notifications = append(notifications, tmpNotifications...)
if endpoint != "" {
goto NEXT
}
for _, notification := range notifications {
notification.Connection = c.Connection
}
return notifications, nil
}