aboutsummaryrefslogtreecommitdiff
path: root/src/handlers.rs
blob: d2a3af6a44d88345d41286c49966248694907d8b (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
use crate::models::ListResponse;
use crate::storage::SharedStorage;
use axum::{
    extract::{Path, State},
    http::{StatusCode, header},
    response::{IntoResponse, Response},
    Json,
};

pub async fn list_all(State(storage): State<SharedStorage>) -> Json<ListResponse> {
    let storage = storage.read().unwrap();
    Json(ListResponse::from(&*storage))
}

pub async fn get_item(
    State(storage): State<SharedStorage>,
    Path((content_type, name)): Path<(String, String)>,
) -> Response {
    let storage = storage.read().unwrap();
    
    let item = match content_type.as_str() {
        "car" => storage.car.get(&name),
        "track" => storage.track.get(&name),
        "luaapp" => storage.luaapp.get(&name),
        "app" => storage.app.get(&name),
        "filter" => storage.filter.get(&name),
        _ => return (StatusCode::NOT_FOUND, "Invalid content type").into_response(),
    };

    match item {
        Some(item) => Json(item.clone()).into_response(),
        None => (StatusCode::NOT_FOUND, "Item not found").into_response(),
    }
}

pub async fn get_download(
    State(storage): State<SharedStorage>,
    Path((content_type, name)): Path<(String, String)>,
) -> Response {
    let storage = storage.read().unwrap();
    
    let item = match content_type.as_str() {
        "car" => storage.car.get(&name),
        "track" => storage.track.get(&name),
        "luaapp" => storage.luaapp.get(&name),
        "app" => storage.app.get(&name),
        "filter" => storage.filter.get(&name),
        _ => return (StatusCode::NOT_FOUND, "Invalid content type").into_response(),
    };

    match item {
        Some(item) => {
            (
                StatusCode::FOUND,
                [(header::LOCATION, item.download_url.as_str())],
            ).into_response()
        },
        None => (StatusCode::NOT_FOUND, "Item not found").into_response(),
    }
}