forked from Invoiced/invoiced-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.go
109 lines (75 loc) · 1.96 KB
/
note.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
108
109
package invdapi
import (
"errors"
"strconv"
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
)
type Note struct {
*Connection
*invdendpoint.Note
}
type Notes []*Note
func (c *Connection) NewNote() *Note {
note := new(invdendpoint.Note)
return &Note{c, note}
}
func (c *Note) Create(createNoteRequest invdendpoint.CreateNoteRequest) (*Note, error) {
endpoint := invdendpoint.NoteEndpoint
noteResp := new(Note)
apiErr := c.create(endpoint, createNoteRequest, noteResp)
if apiErr != nil {
return nil, apiErr
}
noteResp.Connection = c.Connection
return noteResp, nil
}
func (c *Note) Save() error {
endpoint := invdendpoint.NoteEndpoint + "/" + strconv.FormatInt(c.Id, 10)
noteResp := new(Note)
noteDataToUpdate, err := SafeNoteForUpdating(c.Note)
if err != nil {
return err
}
apiErr := c.update(endpoint, noteDataToUpdate, noteResp)
if apiErr != nil {
return apiErr
}
c.Note = noteResp.Note
return nil
}
func (c *Note) Delete() error {
endpoint := invdendpoint.NoteEndpoint + "/" + strconv.FormatInt(c.Id, 10)
err := c.delete(endpoint)
if err != nil {
return err
}
return nil
}
func (c *Note) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Notes, error) {
endpoint := invdendpoint.NoteEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
notes := make(Notes, 0)
NEXT:
tmpNotes := make(Notes, 0)
endpointTmp, apiErr := c.retrieveDataFromAPI(endpoint, &tmpNotes)
if apiErr != nil {
return nil, apiErr
}
notes = append(notes, tmpNotes...)
if endpointTmp != "" {
goto NEXT
}
for _, note := range notes {
note.Connection = c.Connection
}
return notes, nil
}
// SafeCustomerForCreation prunes note data for just fields that can be used for creation of a note
func SafeNoteForUpdating(note *invdendpoint.Note) (*invdendpoint.Note, error) {
if note == nil {
return nil, errors.New("task is nil")
}
noteData := new(invdendpoint.Note)
noteData.Notes = note.Notes
return noteData, nil
}