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

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

#[cfg(test)]
mod test;

pub async fn handler(
    State(invites): State<Invites>,
    Path(invite): Path<PathInfo>,
) -> Result<Json<Summary>, Error> {
    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(),
        }
    }
}