@@ -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
6060func 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
8282func 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
157157func 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
179179func 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