✨ New features to post listing API ♻️ Merge search and
listing API
This commit is contained in:
@@ -42,7 +42,10 @@ public class PostController(
|
|||||||
[FromQuery(Name = "pub")] string? pubName = null,
|
[FromQuery(Name = "pub")] string? pubName = null,
|
||||||
[FromQuery(Name = "type")] int? type = null,
|
[FromQuery(Name = "type")] int? type = null,
|
||||||
[FromQuery(Name = "categories")] List<string>? categories = null,
|
[FromQuery(Name = "categories")] List<string>? categories = null,
|
||||||
[FromQuery(Name = "tags")] List<string>? tags = null
|
[FromQuery(Name = "tags")] List<string>? tags = null,
|
||||||
|
[FromQuery(Name = "query")] string? queryTerm = null,
|
||||||
|
[FromQuery(Name = "vector")] bool queryVector = false,
|
||||||
|
[FromQuery(Name = "replies")] bool includeReplies = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
@@ -72,6 +75,21 @@ public class PostController(
|
|||||||
query = query.Where(p => p.Categories.Any(c => categories.Contains(c.Slug)));
|
query = query.Where(p => p.Categories.Any(c => categories.Contains(c.Slug)));
|
||||||
if (tags is { Count: > 0 })
|
if (tags is { Count: > 0 })
|
||||||
query = query.Where(p => p.Tags.Any(c => tags.Contains(c.Slug)));
|
query = query.Where(p => p.Tags.Any(c => tags.Contains(c.Slug)));
|
||||||
|
if (!includeReplies)
|
||||||
|
query = query.Where(e => e.RepliedPostId == null);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(queryTerm))
|
||||||
|
{
|
||||||
|
if (queryVector)
|
||||||
|
query = query.Where(p => p.SearchVector.Matches(EF.Functions.ToTsQuery(queryTerm)));
|
||||||
|
else
|
||||||
|
query = query.Where(p =>
|
||||||
|
(p.Title != null && EF.Functions.ILike(p.Title, $"%{query}%")) ||
|
||||||
|
(p.Description != null && EF.Functions.ILike(p.Description, $"%{query}%")) ||
|
||||||
|
(p.Content != null && EF.Functions.ILike(p.Content, $"%{query}%"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
query = query
|
query = query
|
||||||
.FilterWithVisibility(currentUser, userFriends, userPublishers, isListing: true);
|
.FilterWithVisibility(currentUser, userFriends, userPublishers, isListing: true);
|
||||||
|
|
||||||
@@ -80,7 +98,6 @@ public class PostController(
|
|||||||
var posts = await query
|
var posts = await query
|
||||||
.Include(e => e.RepliedPost)
|
.Include(e => e.RepliedPost)
|
||||||
.Include(e => e.ForwardedPost)
|
.Include(e => e.ForwardedPost)
|
||||||
.Where(e => e.RepliedPostId == null)
|
|
||||||
.OrderByDescending(e => e.PublishedAt ?? e.CreatedAt)
|
.OrderByDescending(e => e.PublishedAt ?? e.CreatedAt)
|
||||||
.Skip(offset)
|
.Skip(offset)
|
||||||
.Take(take)
|
.Take(take)
|
||||||
@@ -124,6 +141,7 @@ public class PostController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("search")]
|
[HttpGet("search")]
|
||||||
|
[Obsolete("Use the new ListPost API")]
|
||||||
public async Task<ActionResult<List<Post>>> SearchPosts(
|
public async Task<ActionResult<List<Post>>> SearchPosts(
|
||||||
[FromQuery] string query,
|
[FromQuery] string query,
|
||||||
[FromQuery] int offset = 0,
|
[FromQuery] int offset = 0,
|
||||||
|
Reference in New Issue
Block a user