forked from talbright/go-desk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_service.go
103 lines (94 loc) · 2.49 KB
/
note_service.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
package service
import (
"encoding/json"
. "github.com/talbright/go-desk/resource"
"net/http"
"net/url"
)
type NoteService struct {
client *Client
}
func NewNoteService(httpClient *Client) *NoteService {
s := &NoteService{client: httpClient}
return s
}
func (s *NoteService) Get(caseId string, noteId string) (*Note, *http.Response, error) {
restful := Restful{}
note := NewNote()
notePath := NewIdentityResourcePath(noteId, NewNote())
path := NewIdentityResourcePath(caseId, NewCase()).AppendPath(notePath)
resp, err := restful.
Get(path.Path()).
Json(note).
Client(s.client).
Do()
return note, resp, err
}
func (s *NoteService) Create(caseId string, note *Note) (*Note, *http.Response, error) {
restful := Restful{}
createdNote := NewNote()
path := NewIdentityResourcePath(caseId, NewCase()).SetNested(createdNote)
resp, err := restful.
Post(path.Path()).
Body(note).
Json(createdNote).
Client(s.client).
Do()
return createdNote, resp, err
}
func (s *NoteService) Update(caseId string, note *Note) (*Note, *http.Response, error) {
restful := Restful{}
updatedNote := NewNote()
notePath := NewResourcePath(note).SetMember()
path := NewIdentityResourcePath(caseId, NewCase()).AppendPath(notePath)
resp, err := restful.
Patch(path.Path()).
Body(note).
Json(updatedNote).
Client(s.client).
Do()
return updatedNote, resp, err
}
func (s *NoteService) List(caseId string, params *url.Values) (*Page, *http.Response, error) {
restful := Restful{}
page := new(Page)
path := NewIdentityResourcePath(caseId, NewCase()).SetNested(NewNote())
resp, err := restful.
Get(path.Path()).
Json(page).
Params(params).
Client(s.client).
Do()
if err != nil {
return nil, resp, err
}
err = s.unravelPage(page)
if err != nil {
return nil, nil, err
}
return page, resp, err
}
func (s *NoteService) Delete(caseId string, noteId string) (*http.Response, error) {
restful := Restful{}
notePath := NewIdentityResourcePath(noteId, NewNote()).SetMember()
path := NewIdentityResourcePath(caseId, NewCase()).AppendPath(notePath)
resp, err := restful.
Delete(path.Path()).
Client(s.client).
Do()
return resp, err
}
func (s *NoteService) unravelPage(page *Page) error {
notes := new([]Case)
err := json.Unmarshal(*page.Embedded.RawEntries, ¬es)
if err != nil {
return err
}
page.Embedded.Entries = make([]interface{}, len(*notes))
for i, v := range *notes {
v.InitializeResource(v)
page.Embedded.Entries[i] = interface{}(v)
}
page.Embedded.RawEntries = nil
return err
}