Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding filter by status flag to Challenges #20

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions cmd/challenge/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
)

type catalogOptions struct {
category string
category []string
status []string
}

func newCatalogCommand(cli labcli.CLI) *cobra.Command {
var opts catalogOptions

cmd := &cobra.Command{
Use: "catalog [--category <linux|containers|kubernetes|...>]",
Use: "catalog [--category <linux|containers|kubernetes|...>] --status <todo|attempted|solved|...>",
Aliases: []string{"catalog"},
Short: "List challenges from the catalog, optionally filtered by category",
Args: cobra.NoArgs,
Expand All @@ -30,11 +31,18 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command {

flags := cmd.Flags()

flags.StringVar(
flags.StringSliceVar(
&opts.category,
"category",
"",
`Category to filter by - one of linux, containers, kubernetes, ... (an empty string means all)`,
[]string{},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The an empty string means all part is probably a leftover from the previous implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

`Category to filter by - one or multiple categories like linux, containers, kubernetes`,
)

flags.StringSliceVar(
&opts.status,
"status",
[]string{},
`status to filter by - one or multiple status like todo, attempted, solved)`,
)

return cmd
Expand All @@ -54,6 +62,7 @@ type catalogItem struct {
func runCatalogChallenges(ctx context.Context, cli labcli.CLI, opts *catalogOptions) error {
challenges, err := cli.Client().ListChallenges(ctx, &api.ListChallengesOptions{
Category: opts.category,
Status: opts.status,
})
if err != nil {
return fmt.Errorf("cannot list challenges: %w", err)
Expand Down
13 changes: 10 additions & 3 deletions internal/api/challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,22 @@ func (c *Client) GetChallenge(ctx context.Context, name string) (*Challenge, err
}

type ListChallengesOptions struct {
Category string
Category []string
Status []string
}

func (c *Client) ListChallenges(ctx context.Context, opts *ListChallengesOptions) ([]*Challenge, error) {
var challenges []*Challenge
query := url.Values{}
if opts.Category != "" {
query.Set("category", opts.Category)

for _, category := range opts.Category {
query.Add("category", category)
}

for _, status := range opts.Status {
query.Add("status", status)
}

return challenges, c.GetInto(ctx, "/challenges", query, nil, &challenges)
}

Expand Down