summaryrefslogtreecommitdiff
path: root/src/ui/handlers/invite.rs
blob: e552318368ca125be21a3a20c258c076812d1c3e (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
use axum::{
    extract::{Path, State},
    response::{self, IntoResponse},
};
use axum_extra::TypedHeader;
use headers::IfNoneMatch;

use crate::{
    error::Internal,
    invite,
    invite::app::Invites,
    ui::{
        assets,
        assets::{Asset, Assets},
        error::NotFound,
    },
};

pub async fn handler(
    State(invites): State<Invites>,
    Path(invite): Path<invite::Id>,
    TypedHeader(if_none_match): TypedHeader<IfNoneMatch>,
) -> Result<Response, Internal> {
    if invites.get(&invite).await?.is_some() {
        let index = assets::Response::index(&if_none_match)?;
        Ok(Response::Found(index))
    } else {
        let index = Assets::index()?;
        Ok(Response::NotFound(index))
    }
}

pub enum Response {
    Found(assets::Response),
    NotFound(Asset),
}

impl IntoResponse for Response {
    fn into_response(self) -> response::Response {
        match self {
            Self::Found(asset) => asset.into_response(),
            Self::NotFound(asset) => NotFound(asset).into_response(),
        }
    }
}