From cf9dd826c1c08d4a32ee81478e59c68793256b28 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Sun, 5 Sep 2021 22:15:53 -0400 Subject: [PATCH] Improve add tag endpoint and add get tag endpoint Flattened out the add tag logic to make it more functional. --- src/main.rs | 102 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6dcf71b..92ce082 100644 --- a/src/main.rs +++ b/src/main.rs @@ -301,13 +301,13 @@ async fn add_tag(_: Claims, body: web::Json) -> impl Responder { use database::schema::tags; let connection = &connect(); - let tag_id = tags::table + match tags::table .filter(tags::name.eq(&tag)) .get_result::(connection) .optional() - .map_or(-1, |t| { + .and_then(|t| { if let Some(t) = t { - t.id + Ok(t.id) } else { match diesel::insert_into(tags::table) .values(InsertTag { name: tag.clone() }) @@ -318,57 +318,80 @@ async fn add_tag(_: Claims, body: web::Json) -> impl Responder { diesel::sql_types::Integer, "Represents the SQL last_insert_row() function" ); - diesel::select(last_insert_rowid).get_results::(connection) + diesel::select(last_insert_rowid).get_result::(connection) }) { Err(e) => { error!("Error inserting tag: '{}'. {:?}", tag, e); - -1 + Err(e) } Ok(id) => { debug!("Inserted tag: '{}' with id: {:?}", tag, id); - *id.first().expect("We should have just inserted the row") + Ok(id) } } } - }); + }) + .map(|tag_id| { + use database::schema::tagged_photo; - if tag_id == -1 { - HttpResponse::InternalServerError() - } else { - use database::schema::tagged_photo; + let file_name = body.file_name.clone(); - 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::(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); - match tagged_photo::table - .filter(tagged_photo::photo_name.eq(&file_name)) - .filter(tagged_photo::tag_id.eq(tag_id)) - .get_result::(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::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() - }), - Err(e) => { - error!("Error querying tagged photo: {:?}", e); - HttpResponse::InternalServerError() + } } + }) { + Ok(resp) => resp, + Err(e) => { + error!("{:?}", e); + HttpResponse::InternalServerError() + } + } +} + +#[get("image/tags")] +async fn get_tags(_: Claims, request: web::Query) -> 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::(&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(get_file_metadata) .service(add_tag) + .service(get_tags) .app_data(app_data.clone()) .data::>(Box::new(user_dao)) .data::>(Box::new(favorites_dao))