2026-01-14 12:42:00 +01:00
|
|
|
package checkvist
|
|
|
|
|
|
2026-01-14 13:40:37 +01:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-14 12:42:00 +01:00
|
|
|
// notes.go contains the NoteService for CRUD operations on notes (comments) attached to tasks.
|
2026-01-14 13:40:37 +01:00
|
|
|
|
|
|
|
|
// NoteService provides operations on notes (comments) attached to a specific task.
|
|
|
|
|
type NoteService struct {
|
|
|
|
|
client *Client
|
|
|
|
|
checklistID int
|
|
|
|
|
taskID int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notes returns a NoteService for performing note operations on the specified task.
|
|
|
|
|
func (c *Client) Notes(checklistID, taskID int) *NoteService {
|
|
|
|
|
return &NoteService{
|
|
|
|
|
client: c,
|
|
|
|
|
checklistID: checklistID,
|
|
|
|
|
taskID: taskID,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List returns all notes (comments) attached to the task.
|
|
|
|
|
func (s *NoteService) List(ctx context.Context) ([]Note, error) {
|
|
|
|
|
path := fmt.Sprintf("/checklists/%d/tasks/%d/comments.json", s.checklistID, s.taskID)
|
|
|
|
|
|
|
|
|
|
var notes []Note
|
|
|
|
|
if err := s.client.doGet(ctx, path, ¬es); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return notes, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 18:18:31 +01:00
|
|
|
// noteCommentWrapper wraps the comment field for the nested JSON format
|
|
|
|
|
// expected by the Checkvist API: {"comment": {"comment": "text"}}
|
|
|
|
|
type noteCommentWrapper struct {
|
|
|
|
|
Comment string `json:"comment"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 13:40:37 +01:00
|
|
|
// createNoteRequest is the request body for creating a note.
|
|
|
|
|
type createNoteRequest struct {
|
2026-01-14 18:18:31 +01:00
|
|
|
Comment noteCommentWrapper `json:"comment"`
|
2026-01-14 13:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create creates a new note (comment) on the task.
|
|
|
|
|
func (s *NoteService) Create(ctx context.Context, comment string) (*Note, error) {
|
|
|
|
|
path := fmt.Sprintf("/checklists/%d/tasks/%d/comments.json", s.checklistID, s.taskID)
|
2026-01-14 18:18:31 +01:00
|
|
|
body := createNoteRequest{Comment: noteCommentWrapper{Comment: comment}}
|
2026-01-14 13:40:37 +01:00
|
|
|
|
|
|
|
|
var note Note
|
|
|
|
|
if err := s.client.doPost(ctx, path, body, ¬e); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ¬e, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// updateNoteRequest is the request body for updating a note.
|
|
|
|
|
type updateNoteRequest struct {
|
2026-01-14 18:18:31 +01:00
|
|
|
Comment noteCommentWrapper `json:"comment"`
|
2026-01-14 13:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update updates an existing note's comment text.
|
|
|
|
|
func (s *NoteService) Update(ctx context.Context, noteID int, comment string) (*Note, error) {
|
|
|
|
|
path := fmt.Sprintf("/checklists/%d/tasks/%d/comments/%d.json", s.checklistID, s.taskID, noteID)
|
2026-01-14 18:18:31 +01:00
|
|
|
body := updateNoteRequest{Comment: noteCommentWrapper{Comment: comment}}
|
2026-01-14 13:40:37 +01:00
|
|
|
|
|
|
|
|
var note Note
|
|
|
|
|
if err := s.client.doPut(ctx, path, body, ¬e); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ¬e, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete permanently deletes a note.
|
|
|
|
|
func (s *NoteService) Delete(ctx context.Context, noteID int) error {
|
|
|
|
|
path := fmt.Sprintf("/checklists/%d/tasks/%d/comments/%d.json", s.checklistID, s.taskID, noteID)
|
|
|
|
|
return s.client.doDelete(ctx, path)
|
|
|
|
|
}
|