diff --git a/migrations/2021-09-02-000740_create_tags/up.sql b/migrations/2021-09-02-000740_create_tags/up.sql index 4cbb6ef..9938d55 100644 --- a/migrations/2021-09-02-000740_create_tags/up.sql +++ b/migrations/2021-09-02-000740_create_tags/up.sql @@ -1,11 +1,13 @@ CREATE TABLE tags ( id INTEGER PRIMARY KEY NOT NULL, - name TEXT NOT NULL + name TEXT NOT NULL, + created_time BIGINT NOT NULL ); CREATE TABLE tagged_photo ( id INTEGER PRIMARY KEY NOT NULL, photo_name TEXT NOT NULL, tag_id INTEGER NOT NULL, + created_time BIGINT NOT NULL, CONSTRAINT tagid FOREIGN KEY (tag_id) REFERENCES tags (id) ON DELETE CASCADE ON UPDATE CASCADE ); diff --git a/src/database/models.rs b/src/database/models.rs index fecd900..13ccc4d 100644 --- a/src/database/models.rs +++ b/src/database/models.rs @@ -34,12 +34,14 @@ pub struct Favorite { pub struct Tag { pub id: i32, pub name: String, + pub created_time: i64, } #[derive(Insertable, Clone, Debug)] #[table_name = "tags"] pub struct InsertTag { pub name: String, + pub created_time: i64, } #[derive(Insertable, Clone, Debug)] @@ -47,6 +49,7 @@ pub struct InsertTag { pub struct InsertTaggedPhoto { pub tag_id: i32, pub photo_name: String, + pub created_time: i64, } #[derive(Queryable, Clone, Debug)] @@ -54,4 +57,5 @@ pub struct TaggedPhoto { pub id: i32, pub photo_name: String, pub tag_id: i32, + pub created_time: i64, } diff --git a/src/database/schema.rs b/src/database/schema.rs index 3bb2549..340c604 100644 --- a/src/database/schema.rs +++ b/src/database/schema.rs @@ -11,6 +11,7 @@ table! { id -> Integer, photo_name -> Text, tag_id -> Integer, + created_time -> BigInt, } } @@ -18,6 +19,7 @@ table! { tags (id) { id -> Integer, name -> Text, + created_time -> BigInt, } } diff --git a/src/main.rs b/src/main.rs index 92ce082..2a3ce64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate diesel; extern crate rayon; use actix_web_prom::PrometheusMetrics; +use chrono::Utc; use futures::stream::StreamExt; use lazy_static::lazy_static; use prometheus::{self, IntGauge}; @@ -310,7 +311,10 @@ async fn add_tag(_: Claims, body: web::Json) -> impl Responder { Ok(t.id) } else { match diesel::insert_into(tags::table) - .values(InsertTag { name: tag.clone() }) + .values(InsertTag { + name: tag.clone(), + created_time: Utc::now().timestamp(), + }) .execute(connection) .and_then(|_| { no_arg_sql_function!( @@ -347,6 +351,7 @@ async fn add_tag(_: Claims, body: web::Json) -> impl Responder { .values(InsertTaggedPhoto { tag_id, photo_name: file_name.clone(), + created_time: Utc::now().timestamp(), }) .execute(connection) .map(|_| { @@ -384,7 +389,7 @@ async fn get_tags(_: Claims, request: web::Query) -> impl Resp match tags::table .left_join(tagged_photo::table) .filter(tagged_photo::photo_name.eq(&request.path)) - .select((tags::id, tags::name)) + .select((tags::id, tags::name, tags::created_time)) .get_results::(&connect()) { Ok(tags) => HttpResponse::Ok().json(tags),