summaryrefslogtreecommitdiff
path: root/src/invite/handlers/get/mod.rs
blob: bb725864484e6d7ef7d9e00223ae83dbd5a26b20 (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
use axum::{
    extract::{Json, Path, State},
    response::{IntoResponse, Response},
};

use crate::{
    app::App,
    error::{Internal, NotFound},
    invite::{Id, Summary, handlers::PathInfo},
};

#[cfg(test)]
mod test;

pub async fn handler(
    State(app): State<App>,
    Path(invite): Path<PathInfo>,
) -> Result<Json<Summary>, Error> {
    app.invites()
        .get(&invite)
        .await?
        .map(Json)
        .ok_or_else(move || Error::NotFound(invite))
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("invite not found: {0}")]
    NotFound(Id),
    #[error(transparent)]
    Database(#[from] sqlx::Error),
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        match self {
            Self::NotFound(_) => NotFound(self).into_response(),
            Self::Database(_) => Internal::from(self).into_response(),
        }
    }
}