summaryrefslogtreecommitdiff
path: root/src/channel/handlers/delete/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel/handlers/delete/mod.rs')
-rw-r--r--src/channel/handlers/delete/mod.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/channel/handlers/delete/mod.rs b/src/channel/handlers/delete/mod.rs
new file mode 100644
index 0000000..b986bec
--- /dev/null
+++ b/src/channel/handlers/delete/mod.rs
@@ -0,0 +1,59 @@
+use axum::{
+ extract::{Json, Path, State},
+ http::StatusCode,
+ response::{self, IntoResponse},
+};
+
+use crate::{
+ app::App,
+ channel::{self, app, handlers::PathInfo},
+ clock::RequestedAt,
+ error::{Internal, NotFound},
+ token::extract::Identity,
+};
+
+#[cfg(test)]
+mod test;
+
+pub async fn handler(
+ State(app): State<App>,
+ Path(channel): Path<PathInfo>,
+ RequestedAt(deleted_at): RequestedAt,
+ _: Identity,
+) -> Result<Response, Error> {
+ app.channels().delete(&channel, &deleted_at).await?;
+
+ Ok(Response { id: channel })
+}
+
+#[derive(Debug, serde::Serialize)]
+pub struct Response {
+ pub id: channel::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 app::DeleteError);
+
+impl IntoResponse for Error {
+ fn into_response(self) -> response::Response {
+ let Self(error) = self;
+ match error {
+ app::DeleteError::NotFound(_) | app::DeleteError::Deleted(_) => {
+ NotFound(error).into_response()
+ }
+ app::DeleteError::NotEmpty(_) => {
+ (StatusCode::CONFLICT, error.to_string()).into_response()
+ }
+ app::DeleteError::Name(_) | app::DeleteError::Database(_) => {
+ Internal::from(error).into_response()
+ }
+ }
+ }
+}