-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.go
89 lines (74 loc) · 2.03 KB
/
tasks.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
package alvtimeclient
import (
"encoding/json"
"net/url"
)
// Customer struct
type Customer struct {
ID int `json:"id"`
Name string `json:"name"`
InvoiceAddress string `json:"invoiceAddress"`
ContactPerson string `json:"contactPerson"`
ContactEmail string `json:"contactEmail"`
ContactPhone string `json:"contactPhone"`
}
// Project struct
type Project struct {
ID int `json:"id"`
Name string `json:"name"`
Customer Customer `json:"customer"`
}
// Task struct
type Task struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Favorite bool `json:"favorite"`
Locked bool `json:"locked"`
CompensationRate float32 `json:"compensationRate"`
Project Project `json:"project"`
}
// GetTasks fetches all the tasks available to the authenticated user
func (c *AlvtimeClient) GetTasks() (tasks []Task, err error) {
baseURL, err := url.Parse(c.domain)
if err != nil {
return nil, err
}
baseURL.Path += "/api/user/tasks"
req, err := c.newRequest("GET", baseURL, nil)
if err != nil {
return nil, err
}
byteArr, err := c.do(req)
if err != nil {
return nil, err
}
err = json.Unmarshal(byteArr, &tasks)
if err != nil {
return nil, err
}
return tasks, nil
}
// EditFavoriteTasks is used to edit favorite tasks by setting the Favorite
// attribute on and passing it in a slice. The edited tasks are returned as stored to confirm.
func (c *AlvtimeClient) EditFavoriteTasks(tasksToEdit []Task) (editedTasks []Task, err error) {
baseURL, err := url.Parse(c.domain)
if err != nil {
return nil, err
}
baseURL.Path += "/api/user/tasks"
req, err := c.newRequest("POST", baseURL, tasksToEdit)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
byteArr, err := c.do(req)
if err != nil {
return nil, err
}
err = json.Unmarshal(byteArr, &editedTasks)
if err != nil {
return nil, err
}
return editedTasks, nil
}