summaryrefslogtreecommitdiff
path: root/src/channel/routes.rs
blob: 383ec5828624d437a8a040a5166e3c03ef8b5027 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use axum::{
    extract::{Json, Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::{get, post},
    Router,
};

use super::app::{self, EventsError};
use crate::{
    app::App,
    clock::RequestedAt,
    error::InternalError,
    repo::{
        channel::{self, Channel},
        login::Login,
    },
};

pub fn router() -> Router<App> {
    Router::new()
        .route("/api/channels", get(list_channels))
        .route("/api/channels", post(on_create))
        .route("/api/channels/:channel", post(on_send))
}

async fn list_channels(State(app): State<App>, _: Login) -> Result<Channels, InternalError> {
    let channels = app.channels().all().await?;
    let response = Channels(channels);

    Ok(response)
}

struct Channels(Vec<Channel>);

impl IntoResponse for Channels {
    fn into_response(self) -> Response {
        let Self(channels) = self;
        Json(channels).into_response()
    }
}

#[derive(serde::Deserialize)]
struct CreateRequest {
    name: String,
}

async fn on_create(
    State(app): State<App>,
    _: Login, // requires auth, but doesn't actually care who you are
    Json(form): Json<CreateRequest>,
) -> Result<Json<Channel>, CreateError> {
    let channel = app
        .channels()
        .create(&form.name)
        .await
        .map_err(CreateError)?;

    Ok(Json(channel))
}

struct CreateError(app::CreateError);

impl IntoResponse for CreateError {
    fn into_response(self) -> Response {
        let Self(error) = self;
        match error {
            duplicate @ app::CreateError::DuplicateName(_) => {
                (StatusCode::BAD_REQUEST, duplicate.to_string()).into_response()
            }
            other => InternalError::from(other).into_response(),
        }
    }
}

#[derive(serde::Deserialize)]
struct SendRequest {
    message: String,
}

async fn on_send(
    Path(channel): Path<channel::Id>,
    RequestedAt(sent_at): RequestedAt,
    State(app): State<App>,
    login: Login,
    Json(form): Json<SendRequest>,
) -> Result<StatusCode, ErrorResponse> {
    app.channels()
        .send(&login, &channel, &form.message, &sent_at)
        .await
        // Could impl `From` here, but it's more code and this is used once.
        .map_err(ErrorResponse)?;

    Ok(StatusCode::ACCEPTED)
}

struct ErrorResponse(EventsError);

impl IntoResponse for ErrorResponse {
    fn into_response(self) -> Response {
        let Self(error) = self;
        match error {
            not_found @ EventsError::ChannelNotFound(_) => {
                (StatusCode::NOT_FOUND, not_found.to_string()).into_response()
            }
            other => InternalError::from(other).into_response(),
        }
    }
}