-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50f303b
commit 5f2a3da
Showing
5 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters