💄 Optimize post controller
This commit is contained in:
@@ -53,6 +53,7 @@ public class PostController(
|
|||||||
/// <param name="queryVector">If true, uses vector search with the query term. If false, performs a simple ILIKE search.</param>
|
/// <param name="queryVector">If true, uses vector search with the query term. If false, performs a simple ILIKE search.</param>
|
||||||
/// <param name="onlyMedia">If true, only returns posts that have attachments.</param>
|
/// <param name="onlyMedia">If true, only returns posts that have attachments.</param>
|
||||||
/// <param name="shuffle">If true, returns posts in random order. If false, orders by published/created date (newest first).</param>
|
/// <param name="shuffle">If true, returns posts in random order. If false, orders by published/created date (newest first).</param>
|
||||||
|
/// <param name="pinned">If true, returns posts that pinned. If false, returns posts that are not pinned. If null, returns all posts.</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// Returns an ActionResult containing a list of Post objects that match the specified criteria.
|
/// Returns an ActionResult containing a list of Post objects that match the specified criteria.
|
||||||
/// Includes an X-Total header with the total count of matching posts before pagination.
|
/// Includes an X-Total header with the total count of matching posts before pagination.
|
||||||
@@ -63,14 +64,14 @@ public class PostController(
|
|||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[SwaggerOperation(
|
[SwaggerOperation(
|
||||||
Summary = "Retrieves a paginated list of posts",
|
Summary = "Retrieves a paginated list of posts",
|
||||||
Description = "Gets posts with various filtering and sorting options. Supports pagination and advanced search capabilities.",
|
Description =
|
||||||
|
"Gets posts with various filtering and sorting options. Supports pagination and advanced search capabilities.",
|
||||||
OperationId = "ListPosts",
|
OperationId = "ListPosts",
|
||||||
Tags = ["Posts"]
|
Tags = ["Posts"]
|
||||||
)]
|
)]
|
||||||
[SwaggerResponse(StatusCodes.Status200OK, "Successfully retrieved the list of posts", typeof(List<Post>))]
|
[SwaggerResponse(StatusCodes.Status200OK, "Successfully retrieved the list of posts", typeof(List<Post>))]
|
||||||
[SwaggerResponse(StatusCodes.Status400BadRequest, "Invalid request parameters")]
|
[SwaggerResponse(StatusCodes.Status400BadRequest, "Invalid request parameters")]
|
||||||
public async Task<ActionResult<List<Post>>> ListPosts(
|
public async Task<ActionResult<List<Post>>> ListPosts(
|
||||||
[FromQuery(Name = "replies")] bool? includeReplies,
|
|
||||||
[FromQuery] int offset = 0,
|
[FromQuery] int offset = 0,
|
||||||
[FromQuery] int take = 20,
|
[FromQuery] int take = 20,
|
||||||
[FromQuery(Name = "pub")] string? pubName = null,
|
[FromQuery(Name = "pub")] string? pubName = null,
|
||||||
@@ -82,7 +83,8 @@ public class PostController(
|
|||||||
[FromQuery(Name = "vector")] bool queryVector = false,
|
[FromQuery(Name = "vector")] bool queryVector = false,
|
||||||
[FromQuery(Name = "media")] bool onlyMedia = false,
|
[FromQuery(Name = "media")] bool onlyMedia = false,
|
||||||
[FromQuery(Name = "shuffle")] bool shuffle = false,
|
[FromQuery(Name = "shuffle")] bool shuffle = false,
|
||||||
[FromQuery(Name = "pinned")] bool pinned = false
|
[FromQuery(Name = "replies")] bool? includeReplies = null,
|
||||||
|
[FromQuery(Name = "pinned")] bool? pinned = null
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
@@ -118,11 +120,20 @@ public class PostController(
|
|||||||
if (onlyMedia)
|
if (onlyMedia)
|
||||||
query = query.Where(e => e.Attachments.Count > 0);
|
query = query.Where(e => e.Attachments.Count > 0);
|
||||||
|
|
||||||
if (pinned)
|
switch (pinned)
|
||||||
{
|
{
|
||||||
if (realm != null) query = query.Where(p => p.PinMode == PostPinMode.RealmPage);
|
case true when realm != null:
|
||||||
else if (publisher != null) query = query.Where(p => p.PinMode == PostPinMode.PublisherPage);
|
query = query.Where(p => p.PinMode == PostPinMode.RealmPage);
|
||||||
else return BadRequest("You need pass extra realm or publisher params in order to filter with pinned posts.");
|
break;
|
||||||
|
case true when publisher != null:
|
||||||
|
query = query.Where(p => p.PinMode == PostPinMode.PublisherPage);
|
||||||
|
break;
|
||||||
|
case true:
|
||||||
|
return BadRequest(
|
||||||
|
"You need pass extra realm or publisher params in order to filter with pinned posts.");
|
||||||
|
case false:
|
||||||
|
query = query.Where(p => p.PinMode == null);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
query = includeReplies switch
|
query = includeReplies switch
|
||||||
|
Reference in New Issue
Block a user