From 6116e89aaceb275439e19a24e6ea1f1057f57412 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Fri, 20 Jun 2025 17:08:15 +0200 Subject: [PATCH] Return the time entry ID when creating --- mite/mite.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mite/mite.go b/mite/mite.go index 921f9ca..3d4a142 100644 --- a/mite/mite.go +++ b/mite/mite.go @@ -51,8 +51,10 @@ type Project struct { func (p Project) String() string { return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName) } -func (p Project) GetID() int { return p.ID } -func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")" } +func (p Project) GetID() int { return p.ID } +func (p Project) GetName() string { + return p.Name + " (" + p.CustomerName + ")" +} func (a APIClient) GetProjects() (Projects, error) { // GET /projects.json @@ -238,7 +240,9 @@ type requestAddTimeEntry struct { } `json:"time_entry"` } -func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) error { +type apiPostTimeEntry timeEntryHolder + +func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) (int, error) { // POST /time_entries.json // { // "time_entry": { @@ -254,8 +258,10 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI req.RequestTimeEntryHolder.ProjectID = projectId req.RequestTimeEntryHolder.ServiceID = serviceId - err := a.post("/time_entries.json", req) - return err + res := apiPostTimeEntry{} + + err := a.post("/time_entries.json", req, &res) + return res.TimeEntry.ID, err } type apiTimeTrackerEntry struct { @@ -398,9 +404,9 @@ func (a APIClient) patch(path string, data any) error { return nil } -func (a APIClient) post(path string, data any) error { +func (a APIClient) post(path string, dataIn any, dataOut any) error { - b, err := json.Marshal(data) + b, err := json.Marshal(dataIn) if err != nil { return err } @@ -421,6 +427,11 @@ func (a APIClient) post(path string, data any) error { return fmt.Errorf("expected 2XX, got %d", resp.StatusCode) } + err = json.NewDecoder(resp.Body).Decode(&dataOut) + if err != nil { + return err + } + return nil }