Allow to add new aliases

This commit is contained in:
traxys 2023-08-29 23:23:28 +02:00
parent 388db93e3c
commit fe471f156c
2 changed files with 46 additions and 0 deletions

View file

@ -686,6 +686,42 @@ async fn add_mail(
}
}
async fn add_alias(
state: State<Arc<AppState>>,
User(user): User,
Form(add): Form<Mail>,
) -> Result<Redirect, Error> {
let has_mail = sqlx::query!(
"SELECT COUNT(*) FROM emails WHERE id != $1 AND mail = $2",
user,
add.mail
)
.fetch_one(&state.db)
.await?
.count
.expect("count should not be null");
if has_mail != 0 {
Ok(Redirect::to(&format!(
"/?{}",
serde_urlencoded::to_string(&HomeQuery {
user_error: Some(UserError::MailAlreadyExists)
})
.expect("could not generate query")
)))
} else {
sqlx::query!(
"INSERT INTO emails (id, mail, alias) VALUES ($1, $2, true) ON CONFLICT DO NOTHING",
user,
add.mail
)
.execute(&state.db)
.await?;
Ok(Redirect::to("/"))
}
}
#[tracing::instrument(skip(state))]
async fn add_recipient(
state: State<Arc<AppState>>,
@ -790,6 +826,7 @@ async fn main() -> color_eyre::Result<()> {
.route("/", get(home))
.route("/mail/delete", post(delete_mail))
.route("/mail/add", post(add_mail))
.route("/alias/add", post(add_alias))
.route("/alias/recipient/add", post(add_recipient))
.route("/alias/recipient/delete", post(delete_recipient))
.route("/alias/delete", post(delete_alias))

View file

@ -154,5 +154,14 @@
</li>
{% endfor %}
</ul>
{{ self::add_modal(modal_id="addAlias",
add_button="Add new alias",
button_classes="mt-2",
add_text="Add new alias",
action="/alias/add",
input_name="mail",
payload=[],
prefill=true)
}}
</div>
{% endblock content %}