Skip to content

Commit 73c7e89

Browse files
committed
edit activity with minutes - seconds is kinda dumb
1 parent b685a4e commit 73c7e89

2 files changed

Lines changed: 109 additions & 102 deletions

File tree

cmd/ls.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ var projectCmd = &cobra.Command{
1616
Run: func(cmd *cobra.Command, args []string) {
1717
projects, _ := data.GetProjects()
1818
t := table.New().
19-
Border(lipgloss.NormalBorder()).
20-
Headers("ID", "Name")
19+
Border(lipgloss.RoundedBorder()).
20+
Headers("ID", "Name").
21+
StyleFunc(func(row, col int) lipgloss.Style {
22+
return lipgloss.NewStyle().Padding(0, 1)
23+
})
2124
for _, project := range projects {
2225
t.Row(fmt.Sprintf("%d", project.Id), project.GetName())
2326
}
@@ -32,7 +35,10 @@ var taskCmd = &cobra.Command{
3235
projectId, err := cmd.Flags().GetInt("project")
3336

3437
t := table.New().
35-
Border(lipgloss.NormalBorder()).
38+
Border(lipgloss.RoundedBorder()).
39+
StyleFunc(func(row, col int) lipgloss.Style {
40+
return lipgloss.NewStyle().Padding(0, 1)
41+
}).
3642
Headers("ID", "Name")
3743
if projectId != 0 && err == nil {
3844
project, err := data.GetProject(projectId)
@@ -84,17 +90,18 @@ var activityCmd = &cobra.Command{
8490
}
8591
}
8692
t := table.New().
87-
Border(lipgloss.NormalBorder()).
93+
Border(lipgloss.RoundedBorder()).
8894
Headers("ID", "Date", "Time", "Description").
8995
StyleFunc(func(row int, col int) lipgloss.Style {
96+
style := lipgloss.NewStyle().Padding(0, 1)
9097
if (runningIndex < 1) {
91-
return lipgloss.Style{}
98+
return style
9299
}
93100

94101
if runningIndex == row {
95-
return lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Bold(true)
102+
return style.Foreground(lipgloss.Color("9")).Bold(true)
96103
}
97-
return lipgloss.Style{}
104+
return style
98105
}).
99106
Rows(rows...)
100107
fmt.Println(t)

data/activities.go

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,58 @@ func StartActivity(activityId int) error {
3737
config := config.Init()
3838
apiKey := config.GetString("api_key")
3939
if apiKey == "" {
40-
return fmt.Errorf("api_key not set")
40+
return fmt.Errorf("api_key not set")
4141
}
4242
domain := config.GetString("domain")
4343
if domain == "" {
44-
return fmt.Errorf("domain not set")
44+
return fmt.Errorf("domain not set")
4545
}
4646

4747
req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, activityId), nil)
4848
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
4949
client := &http.Client{}
5050
resp, err := client.Do(req)
5151
if err != nil || resp.StatusCode == 422 {
52-
return fmt.Errorf("Something went wrong")
52+
return fmt.Errorf("Something went wrong")
5353
} else if resp.StatusCode == 404 {
54-
return fmt.Errorf("Activity not found")
55-
}
54+
return fmt.Errorf("Activity not found")
55+
}
5656
defer resp.Body.Close()
57-
return nil
57+
return nil
5858
}
5959

6060
func StopActivity(activityId int) error {
6161
config := config.Init()
6262
apiKey := config.GetString("api_key")
6363
if apiKey == "" {
64-
return fmt.Errorf("api_key not set")
64+
return fmt.Errorf("api_key not set")
6565
}
6666
domain := config.GetString("domain")
6767
if domain == "" {
68-
return fmt.Errorf("domain not set")
68+
return fmt.Errorf("domain not set")
6969
}
7070

71-
req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/stop_timer",domain, activityId), nil)
71+
req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/stop_timer", domain, activityId), nil)
7272
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
7373
client := &http.Client{}
7474
resp, err := client.Do(req)
7575
if err != nil || resp.StatusCode == 422 {
76-
return fmt.Errorf("Error on response.\n[ERROR] - %s", err)
76+
return fmt.Errorf("Error on response.\n[ERROR] - %s", err)
7777
}
7878
defer resp.Body.Close()
79-
return nil
79+
return nil
8080
}
8181

8282
func CreateActivity(projectId int, taskId int, description string) error {
8383
config := config.Init()
8484
apiKey := config.GetString("api_key")
85-
domain := config.GetString("domain")
85+
domain := config.GetString("domain")
8686
if apiKey == "" {
87-
return fmt.Errorf("api_key not set")
87+
return fmt.Errorf("api_key not set")
88+
}
89+
if domain == "" {
90+
return fmt.Errorf("domain not set")
8891
}
89-
if domain == "" {
90-
return fmt.Errorf("domain not set")
91-
}
9292

9393
type ActivityBody struct {
9494
ProjectId int `json:"project_id"`
@@ -97,101 +97,101 @@ func CreateActivity(projectId int, taskId int, description string) error {
9797
Date string `json:"date"`
9898
}
9999

100-
body := ActivityBody{
101-
ProjectId: projectId,
102-
TaskId: taskId,
103-
Description: description,
104-
Date: time.Now().Format("2006-01-02"),
105-
}
106-
marshaledBody, err := json.Marshal(body)
107-
if err != nil {
108-
log.Fatal(err)
109-
}
100+
body := ActivityBody{
101+
ProjectId: projectId,
102+
TaskId: taskId,
103+
Description: description,
104+
Date: time.Now().Format("2006-01-02"),
105+
}
106+
marshaledBody, err := json.Marshal(body)
107+
if err != nil {
108+
log.Fatal(err)
109+
}
110110

111111
req, _ := http.NewRequest("POST", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities", domain), bytes.NewReader(marshaledBody))
112112
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
113-
req.Header.Add("Content-Type", "application/json")
114-
client := &http.Client{}
115-
resp, err := client.Do(req)
116-
if err != nil || resp.StatusCode == 422 {
117-
return err
118-
}
119-
defer resp.Body.Close()
120-
return nil
113+
req.Header.Add("Content-Type", "application/json")
114+
client := &http.Client{}
115+
resp, err := client.Do(req)
116+
if err != nil || resp.StatusCode == 422 {
117+
return err
118+
}
119+
defer resp.Body.Close()
120+
return nil
121121
}
122122

123-
func EditActivity(id int, seconds int, description string) error {
124-
config := config.Init()
125-
apiKey := config.GetString("api_key")
126-
domain := config.GetString("domain")
127-
if apiKey == "" {
128-
return fmt.Errorf("api_key not set")
129-
}
130-
if domain == "" {
131-
return fmt.Errorf("domain not set")
132-
}
133-
134-
type Body struct {
135-
Description string `json:"description,omitempty"`
136-
Seconds int `json:"seconds,omitempty"`
137-
}
138-
139-
body := Body{
140-
Description: description,
141-
Seconds: seconds,
142-
}
143-
marshaledBody, err := json.Marshal(body)
144-
145-
req, _ := http.NewRequest("PUT", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), bytes.NewReader(marshaledBody))
146-
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
147-
req.Header.Add("Content-Type", "application/json")
148-
client := &http.Client{}
149-
resp, err := client.Do(req)
150-
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
151-
return err
152-
}
153-
defer resp.Body.Close()
154-
return nil
123+
func EditActivity(id int, minutes int, description string) error {
124+
config := config.Init()
125+
apiKey := config.GetString("api_key")
126+
domain := config.GetString("domain")
127+
if apiKey == "" {
128+
return fmt.Errorf("api_key not set")
129+
}
130+
if domain == "" {
131+
return fmt.Errorf("domain not set")
132+
}
133+
134+
type Body struct {
135+
Description string `json:"description,omitempty"`
136+
Seconds int `json:"seconds,omitempty"`
137+
}
138+
139+
body := Body{
140+
Description: description,
141+
Seconds: minutes * 60,
142+
}
143+
marshaledBody, err := json.Marshal(body)
144+
145+
req, _ := http.NewRequest("PUT", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), bytes.NewReader(marshaledBody))
146+
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
147+
req.Header.Add("Content-Type", "application/json")
148+
client := &http.Client{}
149+
resp, err := client.Do(req)
150+
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
151+
return err
152+
}
153+
defer resp.Body.Close()
154+
return nil
155155
}
156156

157157
func DeleteActivity(id int) error {
158-
config := config.Init()
159-
apiKey := config.GetString("api_key")
160-
domain := config.GetString("domain")
161-
if apiKey == "" {
162-
return fmt.Errorf("api_key not set")
163-
}
164-
if domain == "" {
165-
return fmt.Errorf("domain not set")
166-
}
167-
168-
req, _ := http.NewRequest("DELETE", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), nil)
169-
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
170-
client := &http.Client{}
171-
resp, err := client.Do(req)
172-
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
173-
return err
174-
}
175-
defer resp.Body.Close()
176-
return nil
158+
config := config.Init()
159+
apiKey := config.GetString("api_key")
160+
domain := config.GetString("domain")
161+
if apiKey == "" {
162+
return fmt.Errorf("api_key not set")
163+
}
164+
if domain == "" {
165+
return fmt.Errorf("domain not set")
166+
}
167+
168+
req, _ := http.NewRequest("DELETE", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), nil)
169+
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
170+
client := &http.Client{}
171+
resp, err := client.Do(req)
172+
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
173+
return err
174+
}
175+
defer resp.Body.Close()
176+
return nil
177177
}
178178

179179
func GetActivities() ([]Activity, error) {
180180
config := config.Init()
181181

182182
apiKey := config.GetString("api_key")
183-
domain := config.GetString("domain")
183+
domain := config.GetString("domain")
184184
if apiKey == "" {
185-
return nil, fmt.Errorf("api_key not set")
185+
return nil, fmt.Errorf("api_key not set")
186+
}
187+
if domain == "" {
188+
return nil, fmt.Errorf("domain not set")
186189
}
187-
if domain == "" {
188-
return nil, fmt.Errorf("domain not set")
189-
}
190190

191-
userId, err := GetUserId()
192-
if err != nil {
193-
return nil, err
194-
}
191+
userId, err := GetUserId()
192+
if err != nil {
193+
return nil, err
194+
}
195195
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities?user_id=%d", domain, userId), nil)
196196
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
197197
client := &http.Client{}
@@ -205,7 +205,7 @@ func GetActivities() ([]Activity, error) {
205205

206206
body, err := io.ReadAll(resp.Body)
207207
if err != nil {
208-
return nil, err
208+
return nil, err
209209
}
210210
json.Unmarshal(body, &activities)
211211

0 commit comments

Comments
 (0)