diff --git a/migrations/2019-10-07-174500_create_files/up.sql b/migrations/2019-10-07-174500_create_files/up.sql index 01a068a..000ba0c 100644 --- a/migrations/2019-10-07-174500_create_files/up.sql +++ b/migrations/2019-10-07-174500_create_files/up.sql @@ -1,6 +1,5 @@ CREATE TABLE files ( id INTEGER NOT NULL PRIMARY KEY, filepath TEXT NOT NULL, - created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')), - updated INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) -) + created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) +); diff --git a/migrations/2019-10-07-174501_create_links/up.sql b/migrations/2019-10-07-174501_create_links/up.sql index c0e5f47..c199095 100644 --- a/migrations/2019-10-07-174501_create_links/up.sql +++ b/migrations/2019-10-07-174501_create_links/up.sql @@ -1,6 +1,5 @@ CREATE TABLE links ( id INTEGER NOT NULL PRIMARY KEY, forward TEXT NOT NULL, - created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')), - updated INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) -) + created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) +); diff --git a/migrations/2019-10-07-174502_create_texts/up.sql b/migrations/2019-10-07-174502_create_texts/up.sql index 308780e..12fed12 100644 --- a/migrations/2019-10-07-174502_create_texts/up.sql +++ b/migrations/2019-10-07-174502_create_texts/up.sql @@ -1,6 +1,5 @@ CREATE TABLE texts ( id INTEGER NOT NULL PRIMARY KEY, contents TEXT NOT NULL, - created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')), - updated INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) -) + created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) +); diff --git a/src/handlers.rs b/src/handlers.rs index 2af3b66..13da4f3 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -70,8 +70,8 @@ pub mod files { use chrono::{Datelike, Utc}; use futures::future::{self, Either}; use futures::Future; - use std::path::PathBuf; use std::fs; + use std::path::PathBuf; select!(files); @@ -189,7 +189,7 @@ pub mod links { web::block(move || queries::links::find(id, pool)).then(|result| match result { Ok(link) => Ok(HttpResponse::Found() .header("Location", link.forward) - .header("Last-Modified", timestamp_to_last_modified(link.updated)) + .header("Last-Modified", timestamp_to_last_modified(link.created)) .finish()), Err(_) => Err(HttpResponse::NotFound().finish().into()), }), @@ -237,7 +237,7 @@ pub mod texts { Either::A( web::block(move || queries::texts::find(id, pool)).then(|result| match result { Ok(text) => Ok(HttpResponse::Ok() - .header("Last-Modified", timestamp_to_last_modified(text.updated)) + .header("Last-Modified", timestamp_to_last_modified(text.created)) .body(text.contents)), Err(_) => Err(HttpResponse::NotFound().finish().into()), }), diff --git a/src/models.rs b/src/models.rs index 423b3b4..5517e81 100644 --- a/src/models.rs +++ b/src/models.rs @@ -13,8 +13,6 @@ pub mod files { pub filepath: String, /// Creation date and time as a UNIX timestamp pub created: i32, - /// Update date and time as a UNIX timestamp - pub updated: i32, } /// A new entry to the `files` table @@ -41,8 +39,6 @@ pub mod links { pub forward: String, /// Creation date and time as a UNIX timestamp pub created: i32, - /// Update date and time as a UNIX timestamp - pub updated: i32, } /// A new entry to the `links` table @@ -65,11 +61,10 @@ pub mod texts { pub struct Text { /// Primary key, its radix 36 value is used as an url pub id: i32, + /// Text contents pub contents: String, /// Creation date and time as a UNIX timestamp pub created: i32, - /// Update date and time as a UNIX timestamp - pub updated: i32, } /// A new entry to the `texts` table diff --git a/src/queries.rs b/src/queries.rs index 933eca2..256ec2c 100644 --- a/src/queries.rs +++ b/src/queries.rs @@ -1,57 +1,34 @@ //! Helper functions for SQL queries -// A lot of duplicate code here could be merged by using macros -// but that would make adding different fields more troublesome - -/// Date and time range specifying ranges for creation and update -pub struct SelectRange { - /// Creation time range - pub created: (Option, Option), - /// Update time range - pub updated: (Option, Option), -} - /// Query string for SELECT queries #[derive(Deserialize)] pub struct SelectQuery { /// Left creation bounder timestamp - pub cf: Option, + pub from: Option, /// Right creation bounder timestamp - pub ct: Option, - /// Left update bounder timestamp - pub uf: Option, - /// Right update bounder timestamp - pub ut: Option, + pub to: Option, /// Query size limit pub limit: Option, /// Whether to sort the results in ascending order pub asc: Option, - /// Whether to sort the results by creation date - pub created: Option, } /// Filters for SELECT queries pub struct SelectFilters { /// Creation and update date and time ranges - pub range: SelectRange, + pub range: (Option, Option), /// Query size limit pub limit: Option, /// Whether to sort the results in ascending order - pub order_asc: bool, - /// Whether to sort the results by creation date - pub order_created: bool, + pub asc: bool, } impl From for SelectFilters { fn from(query: SelectQuery) -> Self { SelectFilters { - range: SelectRange { - created: (query.cf, query.ct), - updated: (query.uf, query.ut), - }, + range: (query.from, query.to), limit: query.limit, - order_asc: query.asc.unwrap_or(false), - order_created: query.created.unwrap_or(false), + asc: query.asc.unwrap_or(false), } } } @@ -59,29 +36,21 @@ impl From for SelectFilters { /// Code common to all select functions macro_rules! common_select { ($q:expr, $f:expr) => { - if let Some(cf) = $f.range.created.0 { - $q = $q.filter(created.ge(cf)); + if let Some(from) = $f.range.0 { + $q = $q.filter(created.ge(from)); } - if let Some(ct) = $f.range.created.1 { - $q = $q.filter(created.lt(ct)); - } - if let Some(uf) = $f.range.updated.0 { - $q = $q.filter(updated.ge(uf)); - } - if let Some(ut) = $f.range.updated.1 { - $q = $q.filter(updated.lt(ut)); + if let Some(to) = $f.range.1 { + $q = $q.filter(created.lt(to)); } if let Some(limit) = $f.limit { $q = $q.limit(limit); } - $q = match ($f.order_asc, $f.order_created) { - (false, false) => $q.order(updated.desc()), - (true, false) => $q.order(updated.asc()), - (false, true) => $q.order(created.desc()), - (true, true) => $q.order(created.asc()), - }; + match $f.asc { + false => $q = $q.order(created.desc()), + true => $q = $q.order(created.asc()), + } }; } @@ -143,39 +112,6 @@ pub mod files { find(r_id, pool) } - /// UPDATE a file entry - pub fn update( - u_id: i32, - new_id: Option, - new_filepath: Option<&str>, - pool: Data, - ) -> QueryResult { - let conn: &SqliteConnection = &pool.get().unwrap(); - let file = find(u_id, pool)?; - let query = diesel::update(&file); - let time_update = updated.eq(chrono::Utc::now().timestamp() as i32); - match (new_id, new_filepath) { - (Some(new_id), Some(new_filepath)) => { - query - .set((id.eq(new_id), filepath.eq(new_filepath), time_update)) - .execute(conn)?; - } - (Some(new_id), None) => { - query.set((id.eq(new_id), time_update)).execute(conn)?; - } - (None, Some(new_filepath)) => { - query - .set((filepath.eq(new_filepath), time_update)) - .execute(conn)?; - } - (None, None) => { - return Ok(file); - } - } - - Ok(file) - } - delete!(files); } @@ -215,37 +151,6 @@ pub mod links { find(r_id, pool) } - /// UPDATE a link entry - pub fn update( - u_id: i32, - new_id: Option, - new_forward: Option<&str>, - pool: Data, - ) -> QueryResult { - let conn: &SqliteConnection = &pool.get().unwrap(); - let link = find(u_id, pool)?; - let query = diesel::update(&link); - let time_update = updated.eq(chrono::Utc::now().timestamp() as i32); - match (new_id, new_forward) { - (Some(new_id), Some(new_forward)) => { - query - .set((id.eq(new_id), forward.eq(new_forward), time_update)) - .execute(conn)?; - } - (Some(new_id), None) => { - query.set((id.eq(new_id), time_update)).execute(conn)?; - } - (None, Some(new_forward)) => { - query - .set((forward.eq(new_forward), time_update)) - .execute(conn)?; - } - (None, None) => (), - } - - Ok(link) - } - delete!(links); } @@ -285,36 +190,5 @@ pub mod texts { find(r_id, pool) } - /// UPDATE a text entry - pub fn update( - u_id: i32, - new_id: Option, - new_contents: Option<&str>, - pool: Data, - ) -> QueryResult { - let conn: &SqliteConnection = &pool.get().unwrap(); - let text = find(u_id, pool)?; - let query = diesel::update(&text); - let time_update = updated.eq(chrono::Utc::now().timestamp() as i32); - match (new_id, new_contents) { - (Some(new_id), Some(new_contents)) => { - query - .set((id.eq(new_id), contents.eq(new_contents), time_update)) - .execute(conn)?; - } - (Some(new_id), None) => { - query.set((id.eq(new_id), time_update)).execute(conn)?; - } - (None, Some(new_contents)) => { - query - .set((contents.eq(new_contents), time_update)) - .execute(conn)?; - } - (None, None) => (), - } - - Ok(text) - } - delete!(texts); } diff --git a/src/schema.rs b/src/schema.rs index f7341e8..43cb46e 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -3,7 +3,6 @@ table! { id -> Integer, filepath -> Text, created -> Integer, - updated -> Integer, } } @@ -12,7 +11,6 @@ table! { id -> Integer, forward -> Text, created -> Integer, - updated -> Integer, } } @@ -21,7 +19,6 @@ table! { id -> Integer, contents -> Text, created -> Integer, - updated -> Integer, } } diff --git a/src/setup.rs b/src/setup.rs index 6107b3c..f52c615 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -10,10 +10,10 @@ use std::path::PathBuf; #[cfg(debug_assertions)] use dotenv; -#[cfg(debug_assertions)] -use std::str::FromStr; #[cfg(not(debug_assertions))] use std::fs; +#[cfg(debug_assertions)] +use std::str::FromStr; /// Returns a path to the directory storing application data pub fn get_data_dir() -> PathBuf {