Return the time entry ID when creating

This commit is contained in:
Justin Hawkins 2025-06-20 17:08:15 +02:00
parent e1df2dea3b
commit 6116e89aac

View File

@ -52,7 +52,9 @@ func (p Project) String() string {
return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName) return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName)
} }
func (p Project) GetID() int { return p.ID } func (p Project) GetID() int { return p.ID }
func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")" } func (p Project) GetName() string {
return p.Name + " (" + p.CustomerName + ")"
}
func (a APIClient) GetProjects() (Projects, error) { func (a APIClient) GetProjects() (Projects, error) {
// GET /projects.json // GET /projects.json
@ -238,7 +240,9 @@ type requestAddTimeEntry struct {
} `json:"time_entry"` } `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 // POST /time_entries.json
// { // {
// "time_entry": { // "time_entry": {
@ -254,8 +258,10 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
req.RequestTimeEntryHolder.ProjectID = projectId req.RequestTimeEntryHolder.ProjectID = projectId
req.RequestTimeEntryHolder.ServiceID = serviceId req.RequestTimeEntryHolder.ServiceID = serviceId
err := a.post("/time_entries.json", req) res := apiPostTimeEntry{}
return err
err := a.post("/time_entries.json", req, &res)
return res.TimeEntry.ID, err
} }
type apiTimeTrackerEntry struct { type apiTimeTrackerEntry struct {
@ -398,9 +404,9 @@ func (a APIClient) patch(path string, data any) error {
return nil 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 { if err != nil {
return err return err
} }
@ -421,6 +427,11 @@ func (a APIClient) post(path string, data any) error {
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode) return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
} }
err = json.NewDecoder(resp.Body).Decode(&dataOut)
if err != nil {
return err
}
return nil return nil
} }