Merge pull request #158 from Ilphrin/master

Show the total number of article on a blog
This commit is contained in:
Baptiste Gelez 2018-07-21 22:32:45 +02:00 committed by GitHub
commit 24f2030881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 2 deletions

View File

@ -91,6 +91,12 @@ impl Post {
.expect("Error loading recent posts for blog") .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> { pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users; use schema::users;
use schema::post_authors; use schema::post_authors;

View File

@ -325,3 +325,8 @@ msgstr ""
msgid "Optional" msgid "Optional"
msgstr "" msgstr ""
msgid "One article in this blog"
msgid_plural "{{ count }} articles in this blog"
msgstr[0] ""
msgstr[1] ""

View File

@ -23,6 +23,7 @@ use plume_models::{
fn details(name: String, conn: DbConn, user: Option<User>) -> Template { 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| { 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 recents = Post::get_recents_for_blog(&*conn, &blog, 5);
let articles = Post::get_for_blog(&*conn, &blog);
let authors = &blog.list_authors(&*conn); let authors = &blog.list_authors(&*conn);
Template::render("blogs/details", json!({ 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())), "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>>(), "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>>(), "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 %} <a class="author" href="/@/{{ author.fqn }}">{{ name }}</a>{% if not loop.last %},{% endif %}
{% endfor %} {% endfor %}
</p> </p>
<p>
{{ "{{ count }} articles in this blog" | _n(singular="One article in this blog", count = n_articles) }}
</p>
<section> <section>
<h2>{{ "Latest articles" | _ }}</h2> <h2>{{ "Latest articles" | _ }}</h2>