Skip to content

Commit

Permalink
Add CreateTimeEntry()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent 0b97509 commit b2c7fb4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
26 changes: 26 additions & 0 deletions mite/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mite

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -13,6 +14,7 @@ const layout = "2006-01-02"
type MiteApi interface {
TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error)
TimeEntry(id string) (*TimeEntry, error)
CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error)
Projects() ([]*Project, error)
Services() ([]*Service, error)
}
Expand Down Expand Up @@ -52,3 +54,27 @@ func (a *miteApi) getParametrized(resource string, values url.Values, result int

return a.get(u.String(), result)
}

func (a *miteApi) post(resource string, body interface{}, result interface{}) error {
b, err := json.Marshal(body)
if err != nil {
return err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", a.url, resource), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/json")

res, err := a.client.Do(req)
if err != nil {
return err
}

defer func() { _ = res.Body.Close() }()

return json.NewDecoder(res.Body).Decode(result)
}
51 changes: 51 additions & 0 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mite

import (
"fmt"
"math"
"net/url"
"time"
)
Expand All @@ -15,12 +16,52 @@ type TimeEntry struct {
ServiceName string
}

type TimeEntryCommand struct {
Date *time.Time
Duration *time.Duration
Note string
ProjectId string
ServiceId string
}

func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
r := &timeEntryRequest{}
if c.Date != nil {
r.TimeEntry.Date = c.Date.Format(layout)
}
if c.Duration != nil {
r.TimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS
}
if c.Note != "" {
r.TimeEntry.Note = c.Note
}
if c.ProjectId != "" {
r.TimeEntry.ProjectId = c.ProjectId
}
if c.ServiceId != "" {
r.TimeEntry.ServiceId = c.ServiceId
}

return r
}

// TODO: rename to TimeEntryQuery
type TimeEntryParameters struct {
From *time.Time
To *time.Time
Direction string
}

type timeEntryRequest struct {
TimeEntry struct {
Date string `json:"date_at"`
Minutes int `json:"minutes"`
Note string `json:"note"`
ProjectId string `json:"project_id"`
ServiceId string `json:"service_id"`
} `json:"time_entry"`
}

type timeEntryResponse struct {
TimeEntry struct {
Id int `json:"id"`
Expand Down Expand Up @@ -85,3 +126,13 @@ func (a *miteApi) TimeEntry(id string) (*TimeEntry, error) {

return ter.ToTimeEntry(), nil
}

func (a *miteApi) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) {
ter := timeEntryResponse{}
err := a.post("/time_entries.json", command.toRequest(), &ter)
if err != nil {
return nil, err
}

return ter.ToTimeEntry(), nil
}

0 comments on commit b2c7fb4

Please sign in to comment.