Skip to content

Commit

Permalink
Search title for post keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytes1027 committed Jan 23, 2024
1 parent 2ba639f commit 9627ef9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions FU.API/FU.API/Helpers/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public static PostQuery ToPostQuery(this PostSearchRequestDTO dto)
MinimumRequiredPlayers = dto.MinPlayers ?? 0,
Limit = dto.Limit ?? 20,
Offset = dto.Offset ?? 0,
SortBy = new ()
SortBy = new()
};

if (dto.Keywords is not null)
{
query.DescriptionContains = dto.Keywords.Split(" ").ToList();
query.Keywords = dto.Keywords.Split(" ").ToList();
}

if (dto.Games is not null)
Expand Down Expand Up @@ -196,4 +196,4 @@ public static GroupSimpleDTO ToSimpleDto(this Group group)

public static IEnumerable<GroupSimpleDTO> ToSimpleDtos(this IEnumerable<Group> groups) =>
groups.Select(group => group.ToSimpleDto());
}
}
8 changes: 4 additions & 4 deletions FU.API/FU.API/Models/PostQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace FU.API.Models;

public record PostQuery
{
public List<int> GameIds { get; set; } = new ();
public List<int> TagIds { get; set; } = new ();
public List<int> GameIds { get; set; } = new();
public List<int> TagIds { get; set; } = new();
public DateTime? After { get; set; } = null;
public int MinimumRequiredPlayers { get; set; } = 0;
public List<string> DescriptionContains { get; set; } = new ();
public List<string> Keywords { get; set; } = new();
public SortOption? SortBy { get; set; } = null;
public int Limit { get; set; } = 20;
public int Offset { get; set; } = 0;
}
}
6 changes: 3 additions & 3 deletions FU.API/FU.API/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<List<Post>> SearchPosts(PostQuery query)
}

// Filter by search keywords
dbQuery = dbQuery.Where(DescriptionContains(query.DescriptionContains));
dbQuery = dbQuery.Where(ContainsKeywords(query.Keywords));

// Sort results
IOrderedQueryable<Post> orderedDbQuery = query.SortBy?.Direction == SortDirection.Ascending
Expand All @@ -70,14 +70,14 @@ public async Task<List<Post>> SearchPosts(PostQuery query)
.ToListAsync();
}

private static Expression<Func<Post, bool>> DescriptionContains(List<String> keywords)
private static Expression<Func<Post, bool>> ContainsKeywords(List<String> keywords)
{
if (keywords.Count == 0) return PredicateBuilder.New<Post>(true); // nothing to do so return a true predicate

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));
predicate = predicate.Or(p => p.Description.Contains(keyword) || p.Title.Contains(keyword));
}
return predicate;
}
Expand Down

0 comments on commit 9627ef9

Please sign in to comment.