Show the total number of article on a blog

fixes #150
This commit is contained in:
Kevin "Ilphrin" Pellet 2018-07-21 16:58:30 +02:00
parent 7319cf9829
commit cd24b0f057
3 changed files with 13 additions and 2 deletions

View File

@ -91,6 +91,12 @@ impl Post {
.expect("Error loading recent posts for blog")
}
pub fn get_for_blog(conn: &PgConnection, blog:&Blog) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id))
.load::<Post>(conn)
.expect("Error loading posts for blog")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View File

@ -23,6 +23,7 @@ use plume_models::{
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
may_fail!(user, Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
let articles = Post::get_for_blog(&*conn, &blog);
let authors = &blog.list_authors(&*conn);
Template::render("blogs/details", json!({
@ -31,7 +32,8 @@ fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
"is_author": user.map(|x| x.is_author_in(&*conn, blog.clone())),
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"n_authors": authors.len()
"n_authors": authors.len(),
"n_articles": articles.len()
}))
})
}

View File

@ -19,6 +19,9 @@
<a class="author" href="/@/{{ author.fqn }}">{{ name }}</a>{% if not loop.last %},{% endif %}
{% endfor %}
</p>
<p>
{{ "{{ count }} articles in this blog" | _n(singular="One article in this blog", count = n_articles) }}
</p>
<section>
<h2>{{ "Latest articles" | _ }}</h2>