Allow to delete aliases
This commit is contained in:
parent
7414752ddd
commit
913b7e7c98
1 changed files with 33 additions and 0 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -602,6 +602,38 @@ async fn delete_mail(
|
|||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(state))]
|
||||
async fn delete_alias(
|
||||
state: State<Arc<AppState>>,
|
||||
User(user): User,
|
||||
Form(delete): Form<Mail>,
|
||||
) -> Result<Redirect, Error> {
|
||||
let mut tx = state.db.begin().await?;
|
||||
|
||||
sqlx::query!("DELETE FROM alias_recipient WHERE mail = $1", delete.mail)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
let rows_affected = sqlx::query!(
|
||||
"DELETE FROM emails WHERE id = $1 AND mail = $2",
|
||||
user,
|
||||
delete.mail
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
||||
if rows_affected != 1 {
|
||||
tracing::warn!("Invalid number of rows affected in delete: {rows_affected}");
|
||||
tx.rollback().await?;
|
||||
return Err(Error::InternalError);
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
enum UserError {
|
||||
MailAlreadyExists,
|
||||
|
|
@ -725,6 +757,7 @@ async fn main() -> color_eyre::Result<()> {
|
|||
.route("/mail/delete", post(delete_mail))
|
||||
.route("/mail/add", post(add_mail))
|
||||
.route("/alias/recipient/add", post(add_recipient))
|
||||
.route("/alias/delete", post(delete_alias))
|
||||
.fallback(page_not_found)
|
||||
.with_state(Arc::new(AppState {
|
||||
db,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue