summaryrefslogtreecommitdiffstats
path: root/src/routes
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-13 14:02:35 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-13 14:02:35 +0000
commitf3aa3792255652371ceea6ec032c8962061eb3dc (patch)
treeb6c5e377e5801473462520f77a7137f1bcc74f6d /src/routes
parentd7ad64ab7b779886af75d2500365372921c9181e (diff)
Add endpoint to delete avatar
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/user.rs23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/routes/user.rs b/src/routes/user.rs
index 407f7b4..94db791 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -17,7 +17,7 @@ pub fn create_route() -> Router {
Router::new()
.route("/", get(list_users))
.route("/me", get(get_me))
- .route("/me/avatar", put(edit_my_avatar))
+ .route("/me/avatar", put(edit_my_avatar).delete(delete_my_avatar))
.route("/:id", get(get_user))
}
@@ -72,7 +72,7 @@ async fn edit_my_avatar(
.await
{
Ok(saved_file) => {
- user.edit_avatar(saved_file).await?;
+ user.edit_avatar(Some(saved_file)).await?;
Ok(Json(user))
}
@@ -80,6 +80,25 @@ async fn edit_my_avatar(
}
}
+/// Delete the avatar of the user linked to the claims
+async fn delete_my_avatar(claims: Claims) -> Result<Json<UserList>, AppError> {
+ let mut user = match User::find_by_id(claims.user_id).await {
+ Ok(user) => user,
+ Err(_) => {
+ return Err(AppError::NotFound("User not found".to_string()));
+ }
+ };
+
+ if user.avatar.is_some() {
+ let avatar_url = user.avatar.as_ref().unwrap();
+ delete_upload(&avatar_url)?;
+ }
+
+ user.edit_avatar(None).await?;
+
+ Ok(Json(user))
+}
+
/// Get an user with id = `user_id`. Checks Authorization token
async fn get_user(Path(user_id): Path<i32>, _: Claims) -> Result<Json<UserList>, AppError> {
match User::find_by_id(user_id).await {