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:
Cameron Cordes
2021-09-05 22:15:53 -04:00
parent 4834cacfc3
commit cf9dd826c1

View File

@@ -301,13 +301,13 @@ async fn add_tag(_: Claims, body: web::Json<AddTagRequest>) -> impl Responder {
use database::schema::tags;
let connection = &connect();
let tag_id = tags::table
match tags::table
.filter(tags::name.eq(&tag))
.get_result::<Tag>(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<AddTagRequest>) -> impl Responder {
diesel::sql_types::Integer,
"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) => {
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::<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);
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::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<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(get_file_metadata)
.service(add_tag)
.service(get_tags)
.app_data(app_data.clone())
.data::<Box<dyn UserDao>>(Box::new(user_dao))
.data::<Box<dyn FavoriteDao>>(Box::new(favorites_dao))