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

Search post description and title for any keyword #205

Merged
merged 7 commits into from
Jan 23, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Case insensitive keyword post search
jbytes1027 committed Jan 23, 2024
commit bab4cfba7afff539b352c87f8a0374cccba835fb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FU.API.Migrations
{
/// <inheritdoc />
public partial class NormalizedPostDescAndTitle : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "NormalizedDescription",
table: "Posts",
type: "text",
nullable: false,
defaultValue: "");

migrationBuilder.AddColumn<string>(
name: "NormalizedTitle",
table: "Posts",
type: "text",
nullable: false,
defaultValue: "");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "NormalizedDescription",
table: "Posts");

migrationBuilder.DropColumn(
name: "NormalizedTitle",
table: "Posts");
}
}
}
8 changes: 8 additions & 0 deletions FU.API/FU.API/Migrations/AppDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -310,6 +310,14 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int?>("MaxPlayers")
.HasColumnType("integer");

b.Property<string>("NormalizedDescription")
.IsRequired()
.HasColumnType("text");

b.Property<string>("NormalizedTitle")
.IsRequired()
.HasColumnType("text");

b.Property<DateTime?>("StartTime")
.HasColumnType("timestamp with time zone");

34 changes: 31 additions & 3 deletions FU.API/FU.API/Models/Post.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FU.API.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace FU.API.Models;

/// <summary>
/// The post class.
@@ -10,10 +12,23 @@ public class Post
/// </summary>
public int Id { get; set; }

[NotMapped]
private string _title = string.Empty;

/// <summary>
/// Gets or sets the title of the post.
/// </summary>
public string Title { get; set; } = string.Empty;
public string Title
{
get => _title;
set
{
_title = value;
NormalizedTitle = value.ToUpper();
}
}

public string NormalizedTitle { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the game of the post.
@@ -28,7 +43,20 @@ public class Post
/// <summary>
/// Gets or sets the description of the post.
/// </summary>
public string Description { get; set; } = string.Empty;
[NotMapped]
private string _description = string.Empty;

public string Description
{
get => _description;
set
{
_description = value;
NormalizedDescription = value.ToUpper();
}
}

public string NormalizedDescription { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the start time.
2 changes: 1 addition & 1 deletion FU.API/FU.API/Services/SearchService.cs
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ private static Expression<Func<Post, bool>> ContainsKeywords(List<String> keywor
var predicate = PredicateBuilder.New<Post>(false); // create a predicate that's false by default
foreach (string keyword in keywords)
{
predicate = predicate.Or(p => p.Description.Contains(keyword) || p.Title.Contains(keyword));
predicate = predicate.Or(p => p.NormalizedDescription.Contains(keyword.ToUpper()) || p.NormalizedTitle.Contains(keyword.ToUpper()));
}
return predicate;
}