Improve add tag endpoint and add get tag endpoint
Flattened out the add tag logic to make it more functional.
This commit is contained in:
46
src/main.rs
46
src/main.rs
@@ -301,13 +301,13 @@ async fn add_tag(_: Claims, body: web::Json<AddTagRequest>) -> impl Responder {
|
|||||||
use database::schema::tags;
|
use database::schema::tags;
|
||||||
|
|
||||||
let connection = &connect();
|
let connection = &connect();
|
||||||
let tag_id = tags::table
|
match tags::table
|
||||||
.filter(tags::name.eq(&tag))
|
.filter(tags::name.eq(&tag))
|
||||||
.get_result::<Tag>(connection)
|
.get_result::<Tag>(connection)
|
||||||
.optional()
|
.optional()
|
||||||
.map_or(-1, |t| {
|
.and_then(|t| {
|
||||||
if let Some(t) = t {
|
if let Some(t) = t {
|
||||||
t.id
|
Ok(t.id)
|
||||||
} else {
|
} else {
|
||||||
match diesel::insert_into(tags::table)
|
match diesel::insert_into(tags::table)
|
||||||
.values(InsertTag { name: tag.clone() })
|
.values(InsertTag { name: tag.clone() })
|
||||||
@@ -318,23 +318,20 @@ async fn add_tag(_: Claims, body: web::Json<AddTagRequest>) -> impl Responder {
|
|||||||
diesel::sql_types::Integer,
|
diesel::sql_types::Integer,
|
||||||
"Represents the SQL last_insert_row() function"
|
"Represents the SQL last_insert_row() function"
|
||||||
);
|
);
|
||||||
diesel::select(last_insert_rowid).get_results::<i32>(connection)
|
diesel::select(last_insert_rowid).get_result::<i32>(connection)
|
||||||
}) {
|
}) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Error inserting tag: '{}'. {:?}", tag, e);
|
error!("Error inserting tag: '{}'. {:?}", tag, e);
|
||||||
-1
|
Err(e)
|
||||||
}
|
}
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
debug!("Inserted tag: '{}' with id: {:?}", tag, id);
|
debug!("Inserted tag: '{}' with id: {:?}", tag, id);
|
||||||
*id.first().expect("We should have just inserted the row")
|
Ok(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.map(|tag_id| {
|
||||||
if tag_id == -1 {
|
|
||||||
HttpResponse::InternalServerError()
|
|
||||||
} else {
|
|
||||||
use database::schema::tagged_photo;
|
use database::schema::tagged_photo;
|
||||||
|
|
||||||
let file_name = body.file_name.clone();
|
let file_name = body.file_name.clone();
|
||||||
@@ -370,6 +367,32 @@ async fn add_tag(_: Claims, body: web::Json<AddTagRequest>) -> impl Responder {
|
|||||||
HttpResponse::InternalServerError()
|
HttpResponse::InternalServerError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}) {
|
||||||
|
Ok(resp) => resp,
|
||||||
|
Err(e) => {
|
||||||
|
error!("{:?}", e);
|
||||||
|
HttpResponse::InternalServerError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("image/tags")]
|
||||||
|
async fn get_tags(_: Claims, request: web::Query<ThumbnailRequest>) -> impl Responder {
|
||||||
|
use schema::tagged_photo;
|
||||||
|
use schema::tags;
|
||||||
|
|
||||||
|
match tags::table
|
||||||
|
.left_join(tagged_photo::table)
|
||||||
|
.filter(tagged_photo::photo_name.eq(&request.path))
|
||||||
|
.select((tags::id, tags::name))
|
||||||
|
.get_results::<Tag>(&connect())
|
||||||
|
{
|
||||||
|
Ok(tags) => HttpResponse::Ok().json(tags),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error getting tags for image: '{}'. {:?}", request.path, e);
|
||||||
|
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -556,6 +579,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
.service(delete_favorite)
|
.service(delete_favorite)
|
||||||
.service(get_file_metadata)
|
.service(get_file_metadata)
|
||||||
.service(add_tag)
|
.service(add_tag)
|
||||||
|
.service(get_tags)
|
||||||
.app_data(app_data.clone())
|
.app_data(app_data.clone())
|
||||||
.data::<Box<dyn UserDao>>(Box::new(user_dao))
|
.data::<Box<dyn UserDao>>(Box::new(user_dao))
|
||||||
.data::<Box<dyn FavoriteDao>>(Box::new(favorites_dao))
|
.data::<Box<dyn FavoriteDao>>(Box::new(favorites_dao))
|
||||||
|
|||||||
Reference in New Issue
Block a user