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
This commit is contained in:
parent
3bb006e33d
commit
e5ee947fa4
2 changed files with 91 additions and 1 deletions
|
|
@ -1,3 +1,93 @@
|
|||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue