use axum::{ extract::{Json, Path, State}, http::StatusCode, response::{self, IntoResponse}, }; use crate::{ clock::RequestedAt, error::{Internal, NotFound}, message::{ self, app::{DeleteError, Messages}, }, token::extract::Identity, }; #[cfg(test)] mod test; pub async fn handler( State(messages): State, Path(message): Path, RequestedAt(deleted_at): RequestedAt, identity: Identity, ) -> Result { messages .delete(&identity.login, &message, &deleted_at) .await?; Ok(Response { id: message }) } #[derive(Debug, serde::Serialize)] pub struct Response { pub id: message::Id, } impl IntoResponse for Response { fn into_response(self) -> response::Response { (StatusCode::ACCEPTED, Json(self)).into_response() } } #[derive(Debug, thiserror::Error)] #[error(transparent)] pub struct Error(#[from] pub DeleteError); impl IntoResponse for Error { fn into_response(self) -> response::Response { let Self(error) = self; match error { DeleteError::NotSender(_) => (StatusCode::FORBIDDEN, error.to_string()).into_response(), DeleteError::MessageNotFound(_) | DeleteError::Deleted(_) => { NotFound(error).into_response() } DeleteError::Database(_) | DeleteError::Name(_) => { Internal::from(error).into_response() } } } }