mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
update to use queryTags
This commit is contained in:
parent
fa8d7d1bc8
commit
aa0c95fb86
1 changed files with 43 additions and 12 deletions
|
|
@ -5,22 +5,24 @@ import (
|
|||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/stashbox/graphql"
|
||||
)
|
||||
|
||||
func (c Client) FindTag(ctx context.Context, query string) (*models.ScrapedTag, error) {
|
||||
var id *string
|
||||
var name *string
|
||||
|
||||
// QueryTag searches for tags by name or ID.
|
||||
// If query is a valid UUID, it searches by ID (returns single result).
|
||||
// Otherwise, it searches by name (returns multiple results).
|
||||
func (c Client) QueryTag(ctx context.Context, query string) ([]*models.ScrapedTag, error) {
|
||||
_, err := uuid.Parse(query)
|
||||
if err == nil {
|
||||
// Confirmed the user passed in a Stash ID
|
||||
id = &query
|
||||
} else {
|
||||
// Otherwise assume they're searching on a name
|
||||
name = &query
|
||||
// Query is a UUID, use findTag for exact match
|
||||
return c.findTagByID(ctx, query)
|
||||
}
|
||||
// Otherwise search by name
|
||||
return c.queryTagsByName(ctx, query)
|
||||
}
|
||||
|
||||
tag, err := c.client.FindTag(ctx, id, name)
|
||||
func (c Client) findTagByID(ctx context.Context, id string) ([]*models.ScrapedTag, error) {
|
||||
tag, err := c.client.FindTag(ctx, &id, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -29,8 +31,37 @@ func (c Client) FindTag(ctx context.Context, query string) (*models.ScrapedTag,
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
return &models.ScrapedTag{
|
||||
return []*models.ScrapedTag{{
|
||||
Name: tag.FindTag.Name,
|
||||
RemoteSiteID: &tag.FindTag.ID,
|
||||
}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func (c Client) queryTagsByName(ctx context.Context, name string) ([]*models.ScrapedTag, error) {
|
||||
input := graphql.TagQueryInput{
|
||||
Name: &name,
|
||||
Page: 1,
|
||||
PerPage: 25,
|
||||
Direction: graphql.SortDirectionEnumAsc,
|
||||
Sort: graphql.TagSortEnumName,
|
||||
}
|
||||
|
||||
result, err := c.client.QueryTags(ctx, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.QueryTags.Tags == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var ret []*models.ScrapedTag
|
||||
for _, t := range result.QueryTags.Tags {
|
||||
ret = append(ret, &models.ScrapedTag{
|
||||
Name: t.Name,
|
||||
RemoteSiteID: &t.ID,
|
||||
})
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue