From 3947415d97b06f7fa92df3a417055c20096efe97 Mon Sep 17 00:00:00 2001 From: Kilerd Chan Date: Wed, 5 Jun 2019 11:33:41 +0800 Subject: [PATCH] feat: add article view --- .../2019-06-05-030704_add_view_for_article/down.sql | 2 ++ migrations/2019-06-05-030704_add_view_for_article/up.sql | 2 ++ src/models/article.rs | 9 +++++++++ src/routers/article.rs | 2 ++ src/schema.rs | 8 +++++++- templates/admin/panel.html | 4 ++++ 6 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 migrations/2019-06-05-030704_add_view_for_article/down.sql create mode 100644 migrations/2019-06-05-030704_add_view_for_article/up.sql diff --git a/migrations/2019-06-05-030704_add_view_for_article/down.sql b/migrations/2019-06-05-030704_add_view_for_article/down.sql new file mode 100644 index 0000000..f615321 --- /dev/null +++ b/migrations/2019-06-05-030704_add_view_for_article/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE "public"."articles" DROP COLUMN "view"; \ No newline at end of file diff --git a/migrations/2019-06-05-030704_add_view_for_article/up.sql b/migrations/2019-06-05-030704_add_view_for_article/up.sql new file mode 100644 index 0000000..041d663 --- /dev/null +++ b/migrations/2019-06-05-030704_add_view_for_article/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE "public"."articles" ADD COLUMN "view" int4 NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/src/models/article.rs b/src/models/article.rs index dfe00c3..8e2bc73 100644 --- a/src/models/article.rs +++ b/src/models/article.rs @@ -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, pub keywords: Vec, + pub view: i32, } // #[derive(Debug, Insertable, AsChangeset, Serialize, Deserialize)] @@ -43,6 +45,13 @@ impl Article { .filter(articles::published.eq(true)) .first::
(conn) } + + pub fn increase_view(&self, conn: &PgConnection) { + diesel::sql_query(r#"UPDATE articles SET "view" = "view" + 1 where articles.id = $1"#) + .bind::(self.id) + .execute(conn) + .expect("error on incr view"); + } } impl CRUD for Article { diff --git a/src/routers/article.rs b/src/routers/article.rs index e4cb576..da25271 100644 --- a/src/routers/article.rs +++ b/src/routers/article.rs @@ -45,6 +45,7 @@ pub fn single_article(archives_id: web::Path, data: web::Data) } } + 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, data: web::Data) - return RubbleResponder::not_found(); } let article1 = article.unwrap(); + article1.increase_view(&data.postgres()); let view = ArticleView::from(&article1); diff --git a/src/schema.rs b/src/schema.rs index aa9d6ab..095e476 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -8,6 +8,7 @@ table! { publish_at -> Timestamp, url -> Nullable, keywords -> Array, + 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, +); diff --git a/templates/admin/panel.html b/templates/admin/panel.html index ee3eb81..ae2f218 100644 --- a/templates/admin/panel.html +++ b/templates/admin/panel.html @@ -27,6 +27,7 @@ ID Status Title + View Published Time Tags @@ -55,6 +56,9 @@ {{ article.url }} {% endif %} + + {{ article.view }} + {{ article.publish_at | date(format="%B %d, %Y") }}