summaryrefslogtreecommitdiff
path: root/src/channel/routes/post.rs
blob: d694f8b1d5720babb0adc232cfbf170e22f3855a (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
use axum::{
    extract::{Json, State},
    http::StatusCode,
    response::{self, IntoResponse},
};

use crate::{
    app::App,
    channel::{app, Channel},
    clock::RequestedAt,
    error::Internal,
    login::Login,
};

pub async fn handler(
    State(app): State<App>,
    _: Login, // requires auth, but doesn't actually care who you are
    RequestedAt(created_at): RequestedAt,
    Json(request): Json<Request>,
) -> Result<Json<Channel>, Error> {
    let channel = app
        .channels()
        .create(&request.name, &created_at)
        .await
        .map_err(Error)?;

    Ok(Json(channel))
}

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

#[derive(Debug)]
pub struct Error(pub app::CreateError);

impl IntoResponse for Error {
    fn into_response(self) -> response::Response {
        let Self(error) = self;
        #[allow(clippy::match_wildcard_for_single_variants)]
        match error {
            app::CreateError::DuplicateName(_) => {
                (StatusCode::CONFLICT, error.to_string()).into_response()
            }
            other => Internal::from(other).into_response(),
        }
    }
}