forked from Invoiced/invoiced-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
150 lines (107 loc) · 2.97 KB
/
task.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package invdapi
import (
"errors"
"strconv"
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
)
type Task struct {
*Connection
*invdendpoint.Task
}
type Tasks []*Task
func (c *Connection) NewTask() *Task {
task := new(invdendpoint.Task)
return &Task{c, task}
}
func (c *Task) Create(task *Task) (*Task, error) {
endpoint := invdendpoint.TaskEndpoint
taskResp := new(Task)
// safe prune file data for creation
invdTaskDataToCreate, err := SafeTaskForCreation(task.Task)
if err != nil {
return nil, err
}
apiErr := c.create(endpoint, invdTaskDataToCreate, taskResp)
if apiErr != nil {
return nil, apiErr
}
taskResp.Connection = c.Connection
return taskResp, nil
}
func (c *Task) Save() error {
endpoint := invdendpoint.TaskEndpoint + "/" + strconv.FormatInt(c.Id, 10)
taskResp := new(Task)
taskDataToUpdate, err := SafeTaskForUpdating(c.Task)
if err != nil {
return err
}
apiErr := c.update(endpoint, taskDataToUpdate, taskResp)
if apiErr != nil {
return apiErr
}
c.Task = taskResp.Task
return nil
}
func (c *Task) Delete() error {
endpoint := invdendpoint.TaskEndpoint + "/" + strconv.FormatInt(c.Id, 10)
err := c.delete(endpoint)
if err != nil {
return err
}
return nil
}
func (c *Task) Retrieve(id int64) (*Task, error) {
endpoint := invdendpoint.TaskEndpoint + "/" + strconv.FormatInt(id, 10)
taskEndpoint := new(invdendpoint.Task)
task := &Task{c.Connection, taskEndpoint}
_, err := c.retrieveDataFromAPI(endpoint, task)
if err != nil {
return nil, err
}
return task, nil
}
func (c *Task) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Tasks, error) {
endpoint := invdendpoint.TaskEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
tasks := make(Tasks, 0)
NEXT:
tmpTasks := make(Tasks, 0)
endpointTmp, apiErr := c.retrieveDataFromAPI(endpoint, &tmpTasks)
if apiErr != nil {
return nil, apiErr
}
tasks = append(tasks, tmpTasks...)
if endpointTmp != "" {
goto NEXT
}
for _, task := range tasks {
task.Connection = c.Connection
}
return tasks, nil
}
// SafeCustomerForCreation prunes customer data for just fields that can be used for creation of a customer
func SafeTaskForCreation(task *invdendpoint.Task) (*invdendpoint.Task, error) {
if task == nil {
return nil, errors.New("task is nil")
}
taskData := new(invdendpoint.Task)
taskData.Name = task.Name
taskData.Action = task.Action
taskData.CustomerId = task.CustomerId
taskData.UserId = task.UserId
taskData.DueDate = task.DueDate
return taskData, nil
}
// SafeCustomerForCreation prunes customer data for just fields that can be used for creation of a customer
func SafeTaskForUpdating(task *invdendpoint.Task) (*invdendpoint.Task, error) {
if task == nil {
return nil, errors.New("task is nil")
}
taskData := new(invdendpoint.Task)
taskData.Name = task.Name
taskData.Action = task.Action
taskData.CustomerId = task.CustomerId
taskData.UserId = task.UserId
taskData.DueDate = task.DueDate
return taskData, nil
}