Create Tag tables and Add Tag endpoint
This commit is contained in:
84
src/main.rs
84
src/main.rs
@@ -25,6 +25,7 @@ use actix_web::{
|
||||
web::{self, BufMut, BytesMut, HttpRequest, HttpResponse},
|
||||
App, HttpServer, Responder,
|
||||
};
|
||||
use diesel::prelude::*;
|
||||
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
|
||||
use rayon::prelude::*;
|
||||
|
||||
@@ -34,11 +35,12 @@ use crate::auth::login;
|
||||
use crate::data::*;
|
||||
use crate::database::*;
|
||||
use crate::files::{is_image_or_video, is_valid_path};
|
||||
use crate::models::{InsertTag, InsertTaggedPhoto, Tag, TaggedPhoto};
|
||||
use crate::video::*;
|
||||
|
||||
mod auth;
|
||||
mod data;
|
||||
mod database;
|
||||
pub mod database;
|
||||
mod files;
|
||||
mod video;
|
||||
|
||||
@@ -292,6 +294,85 @@ async fn delete_favorite(
|
||||
}
|
||||
}
|
||||
|
||||
#[post("image/tags")]
|
||||
async fn add_tag(_: Claims, body: web::Json<AddTagRequest>) -> impl Responder {
|
||||
let tag = body.tag_name.clone();
|
||||
|
||||
use database::schema::tags;
|
||||
|
||||
let connection = &connect();
|
||||
let tag_id = tags::table
|
||||
.filter(tags::name.eq(&tag))
|
||||
.get_result::<Tag>(connection)
|
||||
.optional()
|
||||
.map_or(-1, |t| {
|
||||
if let Some(t) = t {
|
||||
t.id
|
||||
} else {
|
||||
match diesel::insert_into(tags::table)
|
||||
.values(InsertTag { name: tag.clone() })
|
||||
.execute(connection)
|
||||
.and_then(|_| {
|
||||
no_arg_sql_function!(
|
||||
last_insert_rowid,
|
||||
diesel::sql_types::Integer,
|
||||
"Represents the SQL last_insert_row() function"
|
||||
);
|
||||
diesel::select(last_insert_rowid).get_results::<i32>(connection)
|
||||
}) {
|
||||
Err(e) => {
|
||||
error!("Error inserting tag: '{}'. {:?}", tag, e);
|
||||
-1
|
||||
}
|
||||
Ok(id) => {
|
||||
debug!("Inserted tag: '{}' with id: {:?}", tag, id);
|
||||
*id.first().expect("We should have just inserted the row")
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if tag_id == -1 {
|
||||
HttpResponse::InternalServerError()
|
||||
} else {
|
||||
use database::schema::tagged_photo;
|
||||
|
||||
let file_name = body.file_name.clone();
|
||||
|
||||
match tagged_photo::table
|
||||
.filter(tagged_photo::photo_name.eq(&file_name))
|
||||
.filter(tagged_photo::tag_id.eq(tag_id))
|
||||
.get_result::<TaggedPhoto>(connection)
|
||||
.optional()
|
||||
{
|
||||
Ok(Some(_)) => HttpResponse::NoContent(),
|
||||
Ok(None) => diesel::insert_into(tagged_photo::table)
|
||||
.values(InsertTaggedPhoto {
|
||||
tag_id,
|
||||
photo_name: file_name.clone(),
|
||||
})
|
||||
.execute(connection)
|
||||
.map(|_| {
|
||||
debug!("Inserted tagged photo: {} -> '{}'", tag_id, file_name);
|
||||
|
||||
HttpResponse::Created()
|
||||
})
|
||||
.unwrap_or_else(|e| {
|
||||
error!(
|
||||
"Error inserting tagged photo: '{}' -> '{}'. {:?}",
|
||||
tag_id, body.file_name, e
|
||||
);
|
||||
|
||||
HttpResponse::InternalServerError()
|
||||
}),
|
||||
Err(e) => {
|
||||
error!("Error querying tagged photo: {:?}", e);
|
||||
HttpResponse::InternalServerError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_thumbnails() {
|
||||
let thumbs = &dotenv::var("THUMBNAILS").expect("THUMBNAILS not defined");
|
||||
let thumbnail_directory: &Path = Path::new(thumbs);
|
||||
@@ -474,6 +555,7 @@ fn main() -> std::io::Result<()> {
|
||||
.service(put_add_favorite)
|
||||
.service(delete_favorite)
|
||||
.service(get_file_metadata)
|
||||
.service(add_tag)
|
||||
.app_data(app_data.clone())
|
||||
.data::<Box<dyn UserDao>>(Box::new(user_dao))
|
||||
.data::<Box<dyn FavoriteDao>>(Box::new(favorites_dao))
|
||||
|
||||
Reference in New Issue
Block a user