public async Task<IEnumerable<Post>> GetPostsByTagInCategory(string categorySlug, string tagSlug, int pageSize = 15, int page = 1) { // todo: standardize return null vs throwing exceptions if (string.IsNullOrEmpty(categorySlug) || string.IsNullOrEmpty(tagSlug)) return null; var category = await _query.GetOne<Category>() .Where(nameof(Category.Slug), Is.EqualTo, categorySlug).Execute(); var tagName = Slug.Reverse(tagSlug); if (category == null) return null; var postsInCategory = await GetPostsByCategory(categorySlug, 1000, 1); var postIds = postsInCategory.Select(post => post.Id); return await _query.GetMany<Post>() .InnerJoin<PostTag>(nameof(Post.Id), nameof(PostTag.PostId)) .Where(nameof(PostTag.PostId), Is.In, postIds) .Where(nameof(PostTag.Tag), Is.EqualTo, tagName) .Order(nameof(Post.Id), Order.Descending) .Pagination(pageSize, page) .Execute(); }
public async Task <int> GetTotalPosts(string tagSlug) { var tagName = Slug.Reverse(tagSlug); var tag = await _query.GetMany <PostTag>() .Where(nameof(PostTag.Tag), Is.EqualTo, tagName).Execute(); return(tag.Count()); }
private async Task <Tag> BuildTag(string tagSlug) { if (string.IsNullOrEmpty(tagSlug)) { return(null); } var tag = await Task.Run(() => { var tagName = Slug.Reverse(tagSlug); var tagInfo = new Tag(tagName, tagSlug); return(tagInfo); }); return(tag); }
public async Task<IEnumerable<Post>> GetPostsByTag(string tagSlug, int pageSize = 15, int page = 1) { if (string.IsNullOrEmpty(tagSlug)) return null; var tagName = Slug.Reverse(tagSlug); var tags = await _query.GetMany<PostTag>() .Where(nameof(PostTag.Tag), Is.EqualTo, tagName).Execute(); if (tags != null && tags.Any()) { var posts = await _query.GetMany<Post>() .Where(nameof(Post.Id), Is.In, tags.Select(t => t.PostId)) .Order(nameof(Post.Id), Order.Descending) .Pagination(pageSize, page) .Execute(); return posts; } return null; }