✨ Optimize post categories, tags usage counting
This commit is contained in:
@@ -97,6 +97,8 @@ public class PostTag : ModelBase
|
|||||||
[MaxLength(128)] public string Slug { get; set; } = null!;
|
[MaxLength(128)] public string Slug { get; set; } = null!;
|
||||||
[MaxLength(256)] public string? Name { get; set; }
|
[MaxLength(256)] public string? Name { get; set; }
|
||||||
[JsonIgnore] public ICollection<Post> Posts { get; set; } = new List<Post>();
|
[JsonIgnore] public ICollection<Post> Posts { get; set; } = new List<Post>();
|
||||||
|
|
||||||
|
[NotMapped] public int? Usage { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PostCategory : ModelBase
|
public class PostCategory : ModelBase
|
||||||
@@ -105,6 +107,8 @@ public class PostCategory : ModelBase
|
|||||||
[MaxLength(128)] public string Slug { get; set; } = null!;
|
[MaxLength(128)] public string Slug { get; set; } = null!;
|
||||||
[MaxLength(256)] public string? Name { get; set; }
|
[MaxLength(256)] public string? Name { get; set; }
|
||||||
[JsonIgnore] public ICollection<Post> Posts { get; set; } = new List<Post>();
|
[JsonIgnore] public ICollection<Post> Posts { get; set; } = new List<Post>();
|
||||||
|
|
||||||
|
[NotMapped] public int? Usage { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PostCollection : ModelBase
|
public class PostCollection : ModelBase
|
||||||
|
@@ -33,10 +33,27 @@ public class PostCategoryController(AppDatabase db) : ControllerBase
|
|||||||
|
|
||||||
var totalCount = await categoriesQuery.CountAsync();
|
var totalCount = await categoriesQuery.CountAsync();
|
||||||
Response.Headers.Append("X-Total", totalCount.ToString());
|
Response.Headers.Append("X-Total", totalCount.ToString());
|
||||||
|
|
||||||
|
// Get categories with their post counts in a single query
|
||||||
var categories = await categoriesQuery
|
var categories = await categoriesQuery
|
||||||
.Skip(offset)
|
.Skip(offset)
|
||||||
.Take(take)
|
.Take(take)
|
||||||
|
.Select(c => new
|
||||||
|
{
|
||||||
|
Category = c,
|
||||||
|
PostCount = c.Posts.Count
|
||||||
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Project results back to the original type and set the Usage property
|
||||||
|
var result = categories.Select(x =>
|
||||||
|
{
|
||||||
|
x.Category.Usage = x.PostCount;
|
||||||
|
return x.Category;
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
|
|
||||||
return Ok(categories);
|
return Ok(categories);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,12 +83,26 @@ public class PostCategoryController(AppDatabase db) : ControllerBase
|
|||||||
|
|
||||||
var totalCount = await tagsQuery.CountAsync();
|
var totalCount = await tagsQuery.CountAsync();
|
||||||
Response.Headers.Append("X-Total", totalCount.ToString());
|
Response.Headers.Append("X-Total", totalCount.ToString());
|
||||||
|
|
||||||
|
// Get tags with their post counts in a single query
|
||||||
var tags = await tagsQuery
|
var tags = await tagsQuery
|
||||||
.Skip(offset)
|
.Skip(offset)
|
||||||
.Take(take)
|
.Take(take)
|
||||||
|
.Select(t => new
|
||||||
|
{
|
||||||
|
Tag = t,
|
||||||
|
PostCount = t.Posts.Count
|
||||||
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return Ok(tags);
|
// Project results back to the original type and set the Usage property
|
||||||
|
var result = tags.Select(x =>
|
||||||
|
{
|
||||||
|
x.Tag.Usage = x.PostCount;
|
||||||
|
return x.Tag;
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("categories/{slug}")]
|
[HttpGet("categories/{slug}")]
|
||||||
|
Reference in New Issue
Block a user