feature/tagging #16

Merged
cameron merged 22 commits from feature/tagging into master 2023-04-10 12:55:28 +00:00
4 changed files with 16 additions and 3 deletions
Showing only changes of commit 2e5ac8861c - Show all commits

View File

@@ -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
);

View File

@@ -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,
}

View File

@@ -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,
}
}

View File

@@ -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<AddTagRequest>) -> 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<AddTagRequest>) -> 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<ThumbnailRequest>) -> 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::<Tag>(&connect())
{
Ok(tags) => HttpResponse::Ok().json(tags),