Skip to content

Commit

Permalink
add put endpoint for recipe image
Browse files Browse the repository at this point in the history
  • Loading branch information
Austionian committed Oct 27, 2023
1 parent 50f303b commit 5f2a3da
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/routes/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub use fish::{delete_fish, new_fish, update_fish};
pub use fish_type::{
create_fish_type, read_all_fish_types, read_fish_type, update_fish_type, update_fish_type_image,
};
pub use recipe::{delete_recipe, new_recipe, update_recipe};
pub use recipe::{delete_recipe, new_recipe, update_recipe, update_recipe_image};
2 changes: 2 additions & 0 deletions src/routes/admin/recipe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod create;
mod delete;
mod update;
mod update_image;

pub use create::new_recipe;
pub use delete::delete_recipe;
pub use update::{update_recipe, RecipeData};
pub use update_image::update_recipe_image;
58 changes: 58 additions & 0 deletions src/routes/admin/recipe/update_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use actix_web::{put, web, HttpResponse};
use anyhow::Result;
use sqlx::PgPool;
use uuid::Uuid;

#[derive(serde::Deserialize, Debug)]
pub struct RecipeUuid {
uuid: Uuid,
}

#[derive(serde::Deserialize)]
pub struct RecipeImageData {
image_url: String,
}

#[tracing::instrument(name = "Updating a recipe image.", skip(data, db_pool))]
#[put("/{uuid}/image")]
pub async fn update_recipe_image(
recipe_id: web::Path<RecipeUuid>,
data: web::Json<RecipeImageData>,
db_pool: web::Data<PgPool>,
) -> HttpResponse {
match update_recipe_image_db(&db_pool, recipe_id.uuid, data).await {
Ok(_) => {
tracing::info!("Recipe image has been updated.");
HttpResponse::Ok().finish()
}
Err(e) => {
tracing::error!("Failed to execute query: {:?}", e);
HttpResponse::InternalServerError().finish()
}
}
}

#[tracing::instrument(name = "Saving new recipe image url to db.", skip(db_pool, data))]
async fn update_recipe_image_db(
db_pool: &PgPool,
recipe_id: Uuid,
data: web::Json<RecipeImageData>,
) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
UPDATE recipe
SET image_url = $1
WHERE id = $2;
"#,
data.image_url,
recipe_id,
)
.execute(db_pool)
.await
.map_err(|e| {
tracing::error!("Failed to execute the query: {:?}", e);
e
})?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod user;
pub use admin::{
create_fish_type, delete_fish, delete_recipe, get_analytics, new_fish, new_recipe,
read_all_fish_types, read_fish_type, update_fish, update_fish_type, update_fish_type_image,
update_recipe,
update_recipe, update_recipe_image,
};
pub use everything::*;
pub use favorite::{favorite_fish, favorite_recipe, favorites};
Expand Down
3 changes: 2 additions & 1 deletion src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Er
web::scope("/recipe")
.service(routes::new_recipe)
.service(routes::update_recipe)
.service(routes::delete_recipe),
.service(routes::delete_recipe)
.service(routes::update_recipe_image),
)
.service(
web::scope("/fish")
Expand Down

0 comments on commit 5f2a3da

Please sign in to comment.