feat: add tag filtering (resolves WEB-1)

This commit is contained in:
Oliver Booth 2023-09-24 17:03:06 +01:00
parent 856c33a74f
commit f48713c470
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
3 changed files with 39 additions and 1 deletions

View File

@ -41,6 +41,17 @@ public sealed class BlogApiController : ControllerBase
return Ok(allPosts.Select(post => CreatePostObject(post))); return Ok(allPosts.Select(post => CreatePostObject(post)));
} }
[HttpGet("posts/tagged/{tag}/{page:int?}")]
public IActionResult GetTaggedBlogPosts(string tag, int page = 0)
{
const int itemsPerPage = 10;
tag = tag.Replace('-', ' ').ToLowerInvariant();
IReadOnlyList<IBlogPost> allPosts = _blogPostService.GetBlogPosts(page, itemsPerPage);
allPosts = allPosts.Where(post => post.Tags.Contains(tag)).ToList();
return Ok(allPosts.Select(post => CreatePostObject(post)));
}
[HttpGet("author/{id:guid}")] [HttpGet("author/{id:guid}")]
public IActionResult GetAuthor(Guid id) public IActionResult GetAuthor(Guid id)
{ {

View File

@ -20,6 +20,11 @@ class API {
return response.map(obj => new BlogPost(obj)); return response.map(obj => new BlogPost(obj));
} }
static async getBlogPostsByTag(tag: string, page: number): Promise<BlogPost[]> {
const response = await API.getResponse(`posts/tagged/${tag}/${page}`);
return response.map(obj => new BlogPost(obj));
}
static async getAuthor(id: string): Promise<Author> { static async getAuthor(id: string): Promise<Author> {
const response = await API.getResponse(`author/${id}`); const response = await API.getResponse(`author/${id}`);
return new Author(response); return new Author(response);

View File

@ -2,6 +2,7 @@ import API from "./API";
import UI from "./UI"; import UI from "./UI";
import Input from "./Input"; import Input from "./Input";
import Author from "./Author"; import Author from "./Author";
import BlogPost from "./BlogPost";
declare const Handlebars: any; declare const Handlebars: any;
declare const Prism: any; declare const Prism: any;
@ -27,6 +28,19 @@ declare const Prism: any;
} }
}); });
function getQueryVariable(variable: string): string {
const query = window.location.search.substring(1);
const vars = query.split("&");
for (const element of vars) {
const pair = element.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return null;
}
Input.registerShortcut(Input.KONAMI_CODE, () => { Input.registerShortcut(Input.KONAMI_CODE, () => {
window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "_blank"); window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "_blank");
}); });
@ -46,7 +60,15 @@ declare const Prism: any;
const template = Handlebars.compile(UI.blogPostTemplate.innerHTML); const template = Handlebars.compile(UI.blogPostTemplate.innerHTML);
API.getBlogPostCount().then(async (count) => { API.getBlogPostCount().then(async (count) => {
for (let i = 0; i <= count / 10; i++) { for (let i = 0; i <= count / 10; i++) {
const posts = await API.getBlogPosts(i); let posts: BlogPost[];
const tag = getQueryVariable("tag");
if (tag !== null) {
posts = await API.getBlogPostsByTag(tag, i);
} else {
posts = await API.getBlogPosts(i);
}
for (const post of posts) { for (const post of posts) {
let author: Author; let author: Author;
if (authors[post.authorId]) { if (authors[post.authorId]) {