wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
342 stars 130 forks source link

Search Unique Tag Name #141

Open PeterX29 opened 6 years ago

PeterX29 commented 6 years ago

I have one array of tags and need to check if the tag already exists in database.

The below code works well, however , "queryBuilder.Search = t[i].Name" works the same way as the LIKE in SQL. I mean, if I have too similar tags, ex "first tag is the best" and "first tag" ig get error.

How to use the queryBuilder.Search to get exact match?

 var queryBuilder = new TagsQueryBuilder();

                for (int i = 0; i < t.Count(); i++)
                {
                queryBuilder.Search = t[i].Name;

                var tags = await client.Tags.Query(queryBuilder);

                    if (tags.Count() == 0) //If none in BD
                    {
                        var createdTag = await client.Tags.Create(t[i]);

                        t[i].Id = createdTag.Id;
                    }
polushinmk commented 6 years ago

We cannot change tags searching behavior, cause it is responsibility of wordpress rest api. I can suggest you theese modification of your code:

if (tags.Count() == 0 || tags.Any(t => t.Name == t[i].Name)) //If none in DB or not exactly match
{
...
}

may be @ThomasPe see another convinient methods

PeterX29 commented 6 years ago

Someone sugested to use the inherit from TagsQueryBuilder:

public class ExtendedTagsQueryBuilder : TagsQueryBuilder
 {
   [QueryText("exact")]
   public bool Exact { get; set; }
 }

And specify this parameter:

 ExtendedTagsQueryBuilder queryBuilder = new ExtendedTagsQueryBuilder();
 queryBuilder.Exact = true;
 queryBuilder.Search = t[i].Name;

...works great :)

polushinmk commented 6 years ago

Interesting thing. There is no information about 'exact' field in official WP REST API docs. @ThomasPe I think, we should include this in our lib, but I cannot find any info about this field and in which endpoints it can be used

ThomasPe commented 6 years ago

I agree we should add this if it's an API feature. Mabye we can add it for Tags for now and do some research if this applies to other entities as well.