示例#1
0
    public void Matches_All_Fields(SearchPostField field)
    {
        // Arrange

        // Act
        var result = SearchPostField.All & field;

        // Assert
        Assert.Equal(field, result);
    }
示例#2
0
    public void Adds_SearchPostField(SearchPostField field, string column)
    {
        // Arrange
        var(builder, v) = Setup();
        var table = builder.TTest.Posts.ToString();

        // Act
        var result = builder.AddWhereSearch(v.Parts, field, Compare.Equal, Rnd.Str);

        // Assert
        var some = result.AssertSome();

        Assert.Collection(some.WhereCustom,
                          x =>
        {
            Assert.Contains($"`{table}`.`{column}`", x.clause);
        }
                          );
    }
示例#3
0
    /// <inheritdoc/>
    public Maybe <QueryParts> AddWhereSearch(QueryParts parts, SearchPostField fields, Compare cmp, string?text)
    {
        // If there isn't any search text, don't do anything
        if (text is null)
        {
            return(parts);
        }

        // Create clause
        var clause = new StringBuilder();

        // Trim search text
        var search = text.Trim();

        // Get comparison operator - if it's LIKE and % has not already been added to the search string, add it
        var comparison = cmp.ToOperator();

        if (cmp == Compare.Like && !search.Contains('%', StringComparison.CurrentCulture))
        {
            search = $"%{search}%";
        }

        // Search title
        if ((fields & SearchPostField.Title) != 0)
        {
            _ = clause.Append(CultureInfo.InvariantCulture, $"{__(T.Posts, p => p.Title)} {comparison} @{nameof(search)}");
        }

        // Search slug
        if ((fields & SearchPostField.Slug) != 0)
        {
            if (clause.Length > 0)
            {
                _ = clause.Append(" OR ");
            }

            _ = clause.Append(CultureInfo.InvariantCulture, $"{__(T.Posts, p => p.Slug)} {comparison} @{nameof(search)}");
        }

        // Search content
        if ((fields & SearchPostField.Content) != 0)
        {
            if (clause.Length > 0)
            {
                _ = clause.Append(" OR ");
            }

            _ = clause.Append(CultureInfo.InvariantCulture, $"{__(T.Posts, p => p.Content)} {comparison} @{nameof(search)}");
        }

        // Search excerpt
        if ((fields & SearchPostField.Excerpt) != 0)
        {
            if (clause.Length > 0)
            {
                _ = clause.Append(" OR ");
            }

            _ = clause.Append(CultureInfo.InvariantCulture, $"{__(T.Posts, p => p.Excerpt)} {comparison} @{nameof(search)}");
        }

        // Return
        return(AddWhereCustom(parts, clause.ToString(), new { search }));
    }