feat: add article view

This commit is contained in:
Kilerd Chan 2019-06-05 11:33:41 +08:00
parent 37c582217d
commit 3947415d97
6 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE "public"."articles" DROP COLUMN "view";

View File

@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE "public"."articles" ADD COLUMN "view" int4 NOT NULL DEFAULT 0;

View File

@ -2,6 +2,7 @@ use crate::{models::CRUD, schema::articles};
use chrono::NaiveDateTime;
use diesel::{pg::PgConnection, prelude::*, result::Error};
use diesel::sql_types::Integer;
use diesel::{query_builder::AsChangeset, Insertable, Queryable};
use serde::{Deserialize, Serialize};
@ -15,6 +16,7 @@ pub struct Article {
pub publish_at: NaiveDateTime,
pub url: Option<String>,
pub keywords: Vec<String>,
pub view: i32,
}
//
#[derive(Debug, Insertable, AsChangeset, Serialize, Deserialize)]
@ -43,6 +45,13 @@ impl Article {
.filter(articles::published.eq(true))
.first::<Article>(conn)
}
pub fn increase_view(&self, conn: &PgConnection) {
diesel::sql_query(r#"UPDATE articles SET "view" = "view" + 1 where articles.id = $1"#)
.bind::<Integer, _>(self.id)
.execute(conn)
.expect("error on incr view");
}
}
impl CRUD<NewArticle, NewArticle, i32> for Article {

View File

@ -45,6 +45,7 @@ pub fn single_article(archives_id: web::Path<i32>, data: web::Data<RubbleData>)
}
}
article1.increase_view(&data.postgres());
let view = ArticleView::from(&article1);
let settings = Setting::load(&data.postgres());
@ -64,6 +65,7 @@ pub fn get_article_by_url(url: web::Path<String>, data: web::Data<RubbleData>) -
return RubbleResponder::not_found();
}
let article1 = article.unwrap();
article1.increase_view(&data.postgres());
let view = ArticleView::from(&article1);

View File

@ -8,6 +8,7 @@ table! {
publish_at -> Timestamp,
url -> Nullable<Text>,
keywords -> Array<Text>,
view -> Int4,
}
}
@ -40,4 +41,9 @@ table! {
joinable!(articles -> users (user_id));
joinable!(tokens -> users (user_id));
allow_tables_to_appear_in_same_query!(articles, setting, tokens, users,);
allow_tables_to_appear_in_same_query!(
articles,
setting,
tokens,
users,
);

View File

@ -27,6 +27,7 @@
<th>ID</th>
<th>Status</th>
<th>Title</th>
<th>View</th>
<th>Published Time</th>
<th>Tags</th>
<th></th>
@ -55,6 +56,9 @@
<span class="tag">{{ article.url }}</span>
{% endif %}
</td>
<td>
{{ article.view }}
</td>
<td>{{ article.publish_at | date(format="%B %d, %Y") }}</td>
<td>
<div class="tags">