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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// API handlers, the ends of each filter chain
use log::debug;
use std::convert::Infallible;
use warp::{http::StatusCode, reply};
use crate::schema::{Db, Transaction}; // `Block` coming later
// PROPOSE Transaction
// POST /transaction
pub async fn propose_transaction(
new_transaction: Transaction,
db: Db,
) -> Result<impl warp::Reply, warp::Rejection> {
debug!("new transaction request {:?}", new_transaction);
let mut transactions = db.lock().await;
transactions.push(new_transaction);
Ok(StatusCode::CREATED)
}
// GET Transaction List
// GET /transaction
// Returns JSON array of transactions
// Cannot fail?
pub async fn list_transactions(db: Db) -> Result<impl warp::Reply, Infallible> {
debug!("list all transactions");
let transactions = db.lock().await;
let transactions: Vec<Transaction> = transactions.clone().into_iter().collect();
Ok(reply::with_status(
reply::json(&transactions),
StatusCode::OK,
))
}
// PROPOSE Block
// POST /block
// `GET /games`
// Returns JSON array of todos
// Allows pagination, for example: `GET /games?offset=10&limit=5`
// pub async fn list_games(options: ListOptions, db: Db) -> Result<impl Reply, Infallible> {
// debug!("list all games");
// let games = db.lock().await;
// let games: Vec<Game> = games
// .clone()
// .into_iter()
// .skip(options.offset.unwrap_or(0))
// .take(options.limit.unwrap_or(std::usize::MAX))
// .collect();
// Ok(warp::reply::json(&games))
// }
// `POST /games`
// Create new game entry with JSON body
// pub async fn create_game(new_game: Game, db: Db) -> Result<impl Reply, Infallible> {
// debug!("create new game: {:?}", new_game);
// let mut games = db.lock().await;
// match games.iter().find(|game| game.id == new_game.id) {
// Some(game) => {
// debug!("game of given id already exists: {}", game.id);
// Ok(StatusCode::BAD_REQUEST)
// }
// None => {
// games.push(new_game);
// Ok(StatusCode::CREATED)
// }
// }
// }
// `PUT /games/:id`
// pub async fn update_game(id: u64, updated_game: Game, db: Db) -> Result<impl Reply, Infallible> {
// debug!("update existing game: id={}, game={:?}", id, updated_game);
// let mut games = db.lock().await;
// match games.iter_mut().find(|game| game.id == id) {
// Some(game) => {
// *game = updated_game;
// Ok(StatusCode::OK)
// }
// None => {
// debug!("game of given id not found");
// Ok(StatusCode::NOT_FOUND)
// }
// }
// }
// `DELETE /games/:id`
// pub async fn delete_game(id: u64, db: Db) -> Result<impl Reply, Infallible> {
// debug!("delete game: id={}", id);
// let mut games = db.lock().await;
// let len = games.len();
// // Removes all games with given id
// games.retain(|game| game.id != id);
// // If games length was smaller that means specyfic game was found and removed
// let deleted = games.len() != len;
// if deleted {
// Ok(StatusCode::NO_CONTENT)
// } else {
// debug!("game of given id not found");
// Ok(StatusCode::NOT_FOUND)
// }
// }
|