diff --git a/DysonNetwork.Sphere/Post/Post.cs b/DysonNetwork.Sphere/Post/Post.cs index d9ff3e0..6bd9754 100644 --- a/DysonNetwork.Sphere/Post/Post.cs +++ b/DysonNetwork.Sphere/Post/Post.cs @@ -97,6 +97,8 @@ public class PostTag : ModelBase [MaxLength(128)] public string Slug { get; set; } = null!; [MaxLength(256)] public string? Name { get; set; } [JsonIgnore] public ICollection Posts { get; set; } = new List(); + + [NotMapped] public int? Usage { get; set; } } public class PostCategory : ModelBase @@ -105,6 +107,8 @@ public class PostCategory : ModelBase [MaxLength(128)] public string Slug { get; set; } = null!; [MaxLength(256)] public string? Name { get; set; } [JsonIgnore] public ICollection Posts { get; set; } = new List(); + + [NotMapped] public int? Usage { get; set; } } public class PostCollection : ModelBase diff --git a/DysonNetwork.Sphere/Post/PostCategoryController.cs b/DysonNetwork.Sphere/Post/PostCategoryController.cs index 943d249..d6cb98b 100644 --- a/DysonNetwork.Sphere/Post/PostCategoryController.cs +++ b/DysonNetwork.Sphere/Post/PostCategoryController.cs @@ -18,7 +18,7 @@ public class PostCategoryController(AppDatabase db) : ControllerBase var categoriesQuery = db.PostCategories .OrderBy(e => e.Name) .AsQueryable(); - + if (!string.IsNullOrEmpty(query)) categoriesQuery = categoriesQuery .Where(e => EF.Functions.ILike(e.Slug, $"%{query}%")); @@ -30,13 +30,30 @@ public class PostCategoryController(AppDatabase db) : ControllerBase _ => categoriesQuery.OrderByDescending(e => e.CreatedAt) }; } - + var totalCount = await categoriesQuery.CountAsync(); Response.Headers.Append("X-Total", totalCount.ToString()); + + // Get categories with their post counts in a single query var categories = await categoriesQuery .Skip(offset) .Take(take) + .Select(c => new + { + Category = c, + PostCount = c.Posts.Count + }) .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); } @@ -51,7 +68,7 @@ public class PostCategoryController(AppDatabase db) : ControllerBase var tagsQuery = db.PostTags .OrderBy(e => e.Name) .AsQueryable(); - + if (!string.IsNullOrEmpty(query)) tagsQuery = tagsQuery .Where(e => EF.Functions.ILike(e.Slug, $"%{query}%")); @@ -63,15 +80,29 @@ public class PostCategoryController(AppDatabase db) : ControllerBase _ => tagsQuery.OrderByDescending(e => e.CreatedAt) }; } - + var totalCount = await tagsQuery.CountAsync(); Response.Headers.Append("X-Total", totalCount.ToString()); + + // Get tags with their post counts in a single query var tags = await tagsQuery .Skip(offset) .Take(take) + .Select(t => new + { + Tag = t, + PostCount = t.Posts.Count + }) .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}")]