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
65
66
67
68
69
70
71
72
73
74
use crate::rest::{v1::logic, ContextLock};
use jormungandr_lib::interfaces::{FragmentsBatch, VotePlanId};
use warp::{reject::Reject, Rejection, Reply};

impl Reject for logic::Error {}

pub async fn post_fragments(
    fragments: FragmentsBatch,
    context: ContextLock,
) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    logic::post_fragments(&context, fragments)
        .await
        .map(|r| warp::reply::json(&r))
        .map_err(warp::reject::custom)
}

#[derive(Deserialize)]
pub struct GetMessageStatusesQuery {
    fragment_ids: String,
}

pub async fn get_fragment_statuses(
    query: GetMessageStatusesQuery,
    context: ContextLock,
) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    let fragment_ids = query.fragment_ids.split(',');
    logic::get_fragment_statuses(&context, fragment_ids)
        .await
        .map_err(warp::reject::custom)
        .map(|r| warp::reply::json(&r))
}

pub async fn get_fragment_logs(context: ContextLock) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    logic::get_fragment_logs(&context)
        .await
        .map_err(warp::reject::custom)
        .map(|r| warp::reply::json(&r))
}

pub async fn get_account_votes_with_plan(
    vote_plan_id: VotePlanId,
    account_id_hex: String,
    context: ContextLock,
) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    logic::get_account_votes_with_plan(&context, vote_plan_id, account_id_hex)
        .await
        .map_err(warp::reject::custom)?
        .ok_or_else(warp::reject::not_found)
        .map(|r| warp::reply::json(&r))
}

pub async fn get_account_votes(
    account_id_hex: String,
    context: ContextLock,
) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    logic::get_account_votes(&context, account_id_hex)
        .await
        .map_err(warp::reject::custom)?
        .ok_or_else(warp::reject::not_found)
        .map(|r| warp::reply::json(&r))
}

pub async fn get_accounts_votes_all(context: ContextLock) -> Result<impl Reply, Rejection> {
    let context = context.read().await;
    logic::get_accounts_votes_all(&context)
        .await
        .map_err(warp::reject::custom)
        .map(|r| warp::reply::json(&r))
}