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

@ -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
}