feat: add category write operations (create, update, remove, get-by-name)
Add CreateCategory, UpdateCategory, RemoveCategory, and GetCategoryByName methods to the client, with BoardScope wrappers for project-scoped operations. Includes comprehensive tests for all new methods. Closes kanboard-4n3
This commit is contained in:
parent
56da2e0fdc
commit
4e856cd206
5 changed files with 350 additions and 2 deletions
|
|
@ -33,3 +33,80 @@ func (c *Client) GetCategory(ctx context.Context, categoryID int) (*Category, er
|
|||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// CreateCategory creates a new category and returns its ID.
|
||||
func (c *Client) CreateCategory(ctx context.Context, projectID int, name string, colorID string) (int, error) {
|
||||
params := map[string]interface{}{
|
||||
"project_id": projectID,
|
||||
"name": name,
|
||||
}
|
||||
if colorID != "" {
|
||||
params["color_id"] = colorID
|
||||
}
|
||||
|
||||
var result IntOrFalse
|
||||
if err := c.call(ctx, "createCategory", params, &result); err != nil {
|
||||
return 0, fmt.Errorf("createCategory: %w", err)
|
||||
}
|
||||
|
||||
if int(result) == 0 {
|
||||
return 0, fmt.Errorf("createCategory: failed to create category %q", name)
|
||||
}
|
||||
|
||||
return int(result), nil
|
||||
}
|
||||
|
||||
// UpdateCategory updates a category's name and optionally its color.
|
||||
func (c *Client) UpdateCategory(ctx context.Context, categoryID int, name string, colorID string) error {
|
||||
params := map[string]interface{}{
|
||||
"id": categoryID,
|
||||
"name": name,
|
||||
}
|
||||
if colorID != "" {
|
||||
params["color_id"] = colorID
|
||||
}
|
||||
|
||||
var result bool
|
||||
if err := c.call(ctx, "updateCategory", params, &result); err != nil {
|
||||
return fmt.Errorf("updateCategory: %w", err)
|
||||
}
|
||||
|
||||
if !result {
|
||||
return fmt.Errorf("updateCategory: failed to update category %d", categoryID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveCategory deletes a category.
|
||||
func (c *Client) RemoveCategory(ctx context.Context, categoryID int) error {
|
||||
params := map[string]int{"category_id": categoryID}
|
||||
|
||||
var result bool
|
||||
if err := c.call(ctx, "removeCategory", params, &result); err != nil {
|
||||
return fmt.Errorf("removeCategory: %w", err)
|
||||
}
|
||||
|
||||
if !result {
|
||||
return fmt.Errorf("removeCategory: failed to remove category %d", categoryID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCategoryByName returns a category by name within a project.
|
||||
// Returns ErrCategoryNotFound if no category matches.
|
||||
func (c *Client) GetCategoryByName(ctx context.Context, projectID int, name string) (*Category, error) {
|
||||
categories, err := c.GetAllCategories(ctx, projectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range categories {
|
||||
if categories[i].Name == name {
|
||||
return &categories[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%w: category %q in project %d", ErrCategoryNotFound, name, projectID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue