checkvist-api/checklists.go
Oliver Jakoubek e5ee947fa4 Implement Checklist operations
Add checklists.go with ChecklistService for CRUD operations:
- client.Checklists() returns ChecklistService
- List(ctx) - get all checklists (GET /checklists.json)
- ListWithOptions(ctx, opts) - with archived filter support
- Get(ctx, id) - get single checklist (GET /checklists/{id}.json)
- Create(ctx, name) - create new checklist (POST /checklists.json)
- Update(ctx, id, name) - update checklist name (PUT /checklists/{id}.json)
- Delete(ctx, id) - delete checklist (DELETE /checklists/{id}.json)

All methods support context for cancellation and timeouts.

Closes checkvist-api-c2k
2026-01-14 13:38:13 +01:00

93 lines
2.6 KiB
Go

package checkvist
import (
"context"
"fmt"
)
// checklists.go contains the ChecklistService for CRUD operations on checklists.
// ChecklistService provides operations on Checkvist checklists.
type ChecklistService struct {
client *Client
}
// Checklists returns a ChecklistService for performing checklist operations.
func (c *Client) Checklists() *ChecklistService {
return &ChecklistService{client: c}
}
// ListOptions configures the List operation.
type ListOptions struct {
// Archived filters to show only archived checklists when true.
Archived bool
}
// List returns all checklists accessible to the authenticated user.
func (s *ChecklistService) List(ctx context.Context) ([]Checklist, error) {
return s.ListWithOptions(ctx, ListOptions{})
}
// ListWithOptions returns checklists with the specified options.
func (s *ChecklistService) ListWithOptions(ctx context.Context, opts ListOptions) ([]Checklist, error) {
path := "/checklists.json"
if opts.Archived {
path += "?archived=true"
}
var checklists []Checklist
if err := s.client.doGet(ctx, path, &checklists); err != nil {
return nil, err
}
return checklists, nil
}
// Get returns a single checklist by ID.
func (s *ChecklistService) Get(ctx context.Context, id int) (*Checklist, error) {
path := fmt.Sprintf("/checklists/%d.json", id)
var checklist Checklist
if err := s.client.doGet(ctx, path, &checklist); err != nil {
return nil, err
}
return &checklist, nil
}
// createChecklistRequest is the request body for creating a checklist.
type createChecklistRequest struct {
Name string `json:"name"`
}
// Create creates a new checklist with the given name.
func (s *ChecklistService) Create(ctx context.Context, name string) (*Checklist, error) {
body := createChecklistRequest{Name: name}
var checklist Checklist
if err := s.client.doPost(ctx, "/checklists.json", body, &checklist); err != nil {
return nil, err
}
return &checklist, nil
}
// updateChecklistRequest is the request body for updating a checklist.
type updateChecklistRequest struct {
Name string `json:"name"`
}
// Update updates the name of an existing checklist.
func (s *ChecklistService) Update(ctx context.Context, id int, name string) (*Checklist, error) {
path := fmt.Sprintf("/checklists/%d.json", id)
body := updateChecklistRequest{Name: name}
var checklist Checklist
if err := s.client.doPut(ctx, path, body, &checklist); err != nil {
return nil, err
}
return &checklist, nil
}
// Delete permanently deletes a checklist by ID.
func (s *ChecklistService) Delete(ctx context.Context, id int) error {
path := fmt.Sprintf("/checklists/%d.json", id)
return s.client.doDelete(ctx, path)
}