-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeEntries.go
140 lines (101 loc) · 3.2 KB
/
timeEntries.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
package matata
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type TimeEntriesOptions struct {
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
}
// TODO: Omit emtys are not ideal here
// TODO: Maybe rename to general name, since it's used for creation and update... maybe use general time entry?
type TimeEntryOptions struct {
Date string `json:"date,omitempty"`
StartTime string `json:"start_time,omitempty"`
EndTime string `json:"end_time,omitempty"`
TaskID int `json:"task_id,omitempty"`
ProjectID int `json:"project_id,omitempty"`
Note string `json:"note,omitempty"`
}
type TimeEntry struct {
ID int `json:"id"`
Date string `json:"date"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Duration string `json:"duration"`
DurationInSeconds float64 `json:"duration_in_seconds"`
Note string `json:"note"`
User User `json:"user"`
Task Task `json:"task"`
Project Project `json:"project"`
}
type TimeEntryList []TimeEntry
func (c *Client) GetTimeEntries(ctx context.Context, options *TimeEntriesOptions) (*TimeEntryList, error) {
if options.StartDate == "" {
options.StartDate = time.Now().AddDate(0, 0, -7).Format("2006-01-02")
}
if options.EndDate == "" {
options.EndDate = time.Now().Format("2006-01-02")
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/time_entries?start_date=%s&end_date=%s", c.BaseURL, options.StartDate, options.EndDate), nil)
if err != nil {
return nil, err
}
res := TimeEntryList{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) GetTimeEntry(ctx context.Context, id int) (*TimeEntry, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/time_entries/%d", c.BaseURL, id), nil)
if err != nil {
return nil, err
}
res := TimeEntry{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) CreateTimeEntry(ctx context.Context, options *TimeEntryOptions) (*TimeEntry, error) {
body, _ := json.Marshal(options)
fmt.Println(string(body))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/time_entries", c.BaseURL), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
res := TimeEntry{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) UpdateTimeEntry(ctx context.Context, id int, options *TimeEntryOptions) (*TimeEntry, error) {
body, _ := json.Marshal(options)
fmt.Println(string(body))
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/time_entries/%d", c.BaseURL, id), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
res := TimeEntry{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) DeleteTimeEntry(ctx context.Context, id int) (*TimeEntry, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/time_entries/%d", c.BaseURL, id), nil)
if err != nil {
return nil, err
}
res := TimeEntry{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}