feat: edit and delete worklog entries
This commit is contained in:
@@ -124,12 +124,30 @@ func buildEntry(nodeID, summary, details, date string, minutes int, approximate,
|
||||
|
||||
// Update modifies an existing entry.
|
||||
func (s *Service) Update(id, summary, details string, minutes int, approximate, billable bool) error {
|
||||
return s.UpdateWithDate(id, summary, details, "", minutes, approximate, billable)
|
||||
}
|
||||
|
||||
// UpdateWithDate modifies an existing entry, including its work date when provided.
|
||||
func (s *Service) UpdateWithDate(id, summary, details, date string, minutes int, approximate, billable bool) error {
|
||||
if summary == "" {
|
||||
return fmt.Errorf("summary required")
|
||||
}
|
||||
t := time.Now().UTC().Format(time.RFC3339)
|
||||
res, err := s.db.Exec(
|
||||
`UPDATE worklog_entries SET summary=?, details=?, minutes=?,
|
||||
approximate=?, billable=?, updated_at=? WHERE id=?`,
|
||||
summary, details, &minutes, boolInt(approximate), boolInt(billable), t, id,
|
||||
)
|
||||
var res sql.Result
|
||||
var err error
|
||||
if date == "" {
|
||||
res, err = s.db.Exec(
|
||||
`UPDATE worklog_entries SET summary=?, details=?, minutes=?,
|
||||
approximate=?, billable=?, updated_at=? WHERE id=?`,
|
||||
summary, details, &minutes, boolInt(approximate), boolInt(billable), t, id,
|
||||
)
|
||||
} else {
|
||||
res, err = s.db.Exec(
|
||||
`UPDATE worklog_entries SET date=?, summary=?, details=?, minutes=?,
|
||||
approximate=?, billable=?, updated_at=? WHERE id=?`,
|
||||
date, summary, details, &minutes, boolInt(approximate), boolInt(billable), t, id,
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -179,7 +197,16 @@ func (s *Service) ListByNode(nodeID string) ([]Entry, error) {
|
||||
|
||||
// Delete removes an entry.
|
||||
func (s *Service) Delete(id string) error {
|
||||
res, err := s.db.Exec("DELETE FROM worklog_entries WHERE id=?", id)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if _, err := tx.Exec("DELETE FROM worklog_entry_events WHERE entry_id=?", id); err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := tx.Exec("DELETE FROM worklog_entries WHERE id=?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -187,7 +214,7 @@ func (s *Service) Delete(id string) error {
|
||||
if n == 0 {
|
||||
return fmt.Errorf("entry not found")
|
||||
}
|
||||
return nil
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// HasTodayEntries checks if any worklog entries exist for today.
|
||||
|
||||
@@ -76,12 +76,15 @@ func TestUpdate(t *testing.T) {
|
||||
svc := NewService(db)
|
||||
|
||||
e, _ := svc.Add("node-1", "Old text", "Old details", 60, false, false)
|
||||
err := svc.Update(e.ID, "New text", "New details", 90, true, true)
|
||||
err := svc.UpdateWithDate(e.ID, "New text", "New details", "2026-01-02", 90, true, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, _ := svc.Get(e.ID)
|
||||
if got.Date != "2026-01-02" {
|
||||
t.Errorf("date = %q", got.Date)
|
||||
}
|
||||
if got.Summary != "New text" {
|
||||
t.Errorf("summary = %q", got.Summary)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user