summaryrefslogtreecommitdiff
path: root/src/message/routes.rs
blob: e21c674aa636874372d025c2cd31e92f789539cc (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
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::delete,
    Router,
};

use crate::{
    app::App,
    clock::RequestedAt,
    error::{Internal, NotFound},
    login::Login,
    message::{self, app::DeleteError},
};

pub fn router() -> Router<App> {
    Router::new().route("/api/messages/:message", delete(on_delete))
}

async fn on_delete(
    State(app): State<App>,
    Path(message): Path<message::Id>,
    RequestedAt(deleted_at): RequestedAt,
    _: Login,
) -> Result<StatusCode, ErrorResponse> {
    app.messages().delete(&message, &deleted_at).await?;

    Ok(StatusCode::ACCEPTED)
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
struct ErrorResponse(#[from] DeleteError);

impl IntoResponse for ErrorResponse {
    fn into_response(self) -> Response {
        let Self(error) = self;
        match error {
            not_found @ (DeleteError::ChannelNotFound(_) | DeleteError::NotFound(_)) => {
                NotFound(not_found).into_response()
            }
            other => Internal::from(other).into_response(),
        }
    }
}