-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathblock.rs
More file actions
69 lines (60 loc) · 1.47 KB
/
Copy pathblock.rs
File metadata and controls
69 lines (60 loc) · 1.47 KB
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
use axum::{
extract::{Path, Query},
routing::get,
Json, Router,
};
use bitcoin::BlockHash;
use log::debug;
use serde::{Deserialize, Serialize};
use crate::{
api_paged_response::ApiPagedResponse,
api_query::PaginationQuery,
error::OceanResult,
SERVICES,
repository::RepositoryOps,
model::Block,
};
#[derive(Deserialize)]
struct BlockId {
id: String,
}
async fn list_blocks(
Query(query): Query<PaginationQuery>,
) -> OceanResult<Json<ApiPagedResponse<Block>>> {
let blocks = SERVICES
.block
.by_height
.list(None)?
.take(query.size)
.map(|item| {
let (_, id) = item?;
let b = SERVICES
.block
.by_id
.get(&id)?
.ok_or("Missing block index")?;
Ok(b)
})
.collect::<OceanResult<Vec<_>>>()?;
Ok(Json(ApiPagedResponse::of(
blocks,
query.size,
|block| block.clone().id,
)))
}
async fn get_block(Path(id): Path<BlockHash>) -> OceanResult<Json<Option<Block>>> {
let block = SERVICES
.block
.by_id
.get(&id)?;
Ok(Json(block))
}
async fn get_transactions(Path(hash): Path<BlockHash>) -> String {
format!("Transactions for block with hash {}", hash)
}
pub fn router() -> Router {
Router::new()
.route("/", get(list_blocks))
.route("/:id", get(get_block))
.route("/:hash/transactions", get(get_transactions))
}