summaryrefslogtreecommitdiff
path: root/src/conversation/handlers/delete/mod.rs
blob: 2365303c8b2fbbf1d7df9e2a6b21d5887cb64725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use axum::{
    extract::{Json, Path, State},
    http::StatusCode,
    response::{self, IntoResponse},
};

use crate::{
    clock::RequestedAt,
    conversation::{self, app, app::Conversations, handlers::PathInfo},
    error::{Internal, NotFound},
    token::extract::Identity,
};

#[cfg(test)]
mod test;

pub async fn handler(
    State(conversations): State<Conversations>,
    Path(conversation): Path<PathInfo>,
    RequestedAt(deleted_at): RequestedAt,
    _: Identity,
) -> Result<Response, Error> {
    conversations.delete(&conversation, &deleted_at).await?;

    Ok(Response { id: conversation })
}

#[derive(Debug, serde::Serialize)]
pub struct Response {
    pub id: conversation::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::Failed(_) => Internal::from(error).into_response(),
        }
    }
}