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...