Implementing DELETE user
The first thing we want to do to delete a user is to create a method for the User
struct. Let's look at the steps:
- Write the method to delete a user in the
impl User
block insrc/models/user.rs
:pub async fn destroy(connection: &mut PgConnection, uuid: &str) -> Result<(), Box<dyn Error>> { let parsed_uuid = Uuid::parse_str(uuid)?; let query_str = "DELETE FROM users WHERE uuid = $1"; sqlx::query(query_str) .bind(parsed_uuid) .execute(connection) .await?; Ok(()) }
Then, we can implement the delete_user()
function in src/routes/user.rs
:
#[delete("/users/<uuid>", format = "application/x-www-form-urlencoded")] pub async fn...