Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
census article view count and comment count
- Loading branch information
1 parent
8dbff31
commit a12a24b
Showing
11 changed files
with
199 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE article_stats; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE article_stats ( | ||
id uuid primary key default gen_random_uuid(), | ||
article_id uuid references article (id) not null, | ||
created_time timestamp not null default current_timestamp, | ||
ruser_id uuid references ruser (id), | ||
user_agent varchar, | ||
visitor_ip varchar | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use super::super::{article_stats}; | ||
use super::super::article_stats::dsl::article_stats as all_articles_stats; | ||
|
||
use chrono::NaiveDateTime; | ||
use diesel; | ||
use diesel::PgConnection; | ||
use diesel::prelude::*; | ||
use uuid::Uuid; | ||
|
||
#[derive(Queryable, Debug, Clone, Serialize, Deserialize)] | ||
pub struct QueryArticleStats { | ||
pub id: Uuid, | ||
pub article_id: Uuid, | ||
pub created_time: NaiveDateTime, | ||
pub ruser_id: Option<Uuid>, | ||
pub user_agent: Option<String>, | ||
pub visitor_ip: Option<String>, | ||
} | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[table_name = "article_stats"] | ||
pub struct NewArticleStats { | ||
pub article_id: Uuid, | ||
pub ruser_id: Option<Uuid>, | ||
pub user_agent: Option<String>, | ||
pub visitor_ip: Option<String>, | ||
} | ||
|
||
impl NewArticleStats { | ||
pub fn new(self, conn: &PgConnection) -> Result<usize, String> { | ||
let res = diesel::insert_into(all_articles_stats) | ||
.values(&self) | ||
.execute(conn); | ||
match res { | ||
Ok(data) => Ok(data), | ||
Err(err) => Err(format!("{}", err)), | ||
} | ||
} | ||
pub fn insert(self, conn: &PgConnection) -> Result<usize, String> { | ||
let res = diesel::insert_into(all_articles_stats) | ||
.values(&self) | ||
.execute(conn); | ||
match res { | ||
Ok(data) => Ok(data), | ||
Err(err) => Err(format!("{}", err)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.