diff options
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/handlers.rs b/src/handlers.rs new file mode 100644 index 0000000..51c7b63 --- /dev/null +++ b/src/handlers.rs | |||
@@ -0,0 +1,122 @@ | |||
1 | // API handlers, the ends of each filter chain | ||
2 | |||
3 | use log::debug; | ||
4 | use std::convert::Infallible; | ||
5 | use warp::{http::StatusCode, reply}; | ||
6 | |||
7 | use crate::schema::{Db, Transaction}; // `Block` coming later | ||
8 | |||
9 | // PROPOSE Transaction | ||
10 | // POST /transaction | ||
11 | pub async fn propose_transaction( | ||
12 | new_transaction: Transaction, | ||
13 | db: Db, | ||
14 | ) -> Result<impl warp::Reply, warp::Rejection> { | ||
15 | debug!("new transaction request {:?}", new_transaction); | ||
16 | |||
17 | let mut transactions = db.lock().await; | ||
18 | |||
19 | transactions.push(new_transaction); | ||
20 | |||
21 | Ok(StatusCode::CREATED) | ||
22 | } | ||
23 | |||
24 | // GET Transaction List | ||
25 | // GET /transaction | ||
26 | // Returns JSON array of transactions | ||
27 | // Cannot fail? | ||
28 | pub async fn list_transactions(db: Db) -> Result<impl warp::Reply, Infallible> { | ||
29 | debug!("list all transactions"); | ||
30 | |||
31 | let transactions = db.lock().await; | ||
32 | |||
33 | let transactions: Vec<Transaction> = transactions.clone().into_iter().collect(); | ||
34 | |||
35 | Ok(reply::with_status( | ||
36 | reply::json(&transactions), | ||
37 | StatusCode::OK, | ||
38 | )) | ||
39 | } | ||
40 | |||
41 | // PROPOSE Block | ||
42 | // POST /block | ||
43 | |||
44 | // `GET /games` | ||
45 | // Returns JSON array of todos | ||
46 | // Allows pagination, for example: `GET /games?offset=10&limit=5` | ||
47 | // pub async fn list_games(options: ListOptions, db: Db) -> Result<impl Reply, Infallible> { | ||
48 | // debug!("list all games"); | ||
49 | |||
50 | // let games = db.lock().await; | ||
51 | // let games: Vec<Game> = games | ||
52 | // .clone() | ||
53 | // .into_iter() | ||
54 | // .skip(options.offset.unwrap_or(0)) | ||
55 | // .take(options.limit.unwrap_or(std::usize::MAX)) | ||
56 | // .collect(); | ||
57 | |||
58 | // Ok(warp::reply::json(&games)) | ||
59 | // } | ||
60 | |||
61 | // `POST /games` | ||
62 | // Create new game entry with JSON body | ||
63 | // pub async fn create_game(new_game: Game, db: Db) -> Result<impl Reply, Infallible> { | ||
64 | // debug!("create new game: {:?}", new_game); | ||
65 | |||
66 | // let mut games = db.lock().await; | ||
67 | |||
68 | // match games.iter().find(|game| game.id == new_game.id) { | ||
69 | // Some(game) => { | ||
70 | // debug!("game of given id already exists: {}", game.id); | ||
71 | |||
72 | // Ok(StatusCode::BAD_REQUEST) | ||
73 | // } | ||
74 | // None => { | ||
75 | // games.push(new_game); | ||
76 | // Ok(StatusCode::CREATED) | ||
77 | // } | ||
78 | // } | ||
79 | // } | ||
80 | |||
81 | // `PUT /games/:id` | ||
82 | // pub async fn update_game(id: u64, updated_game: Game, db: Db) -> Result<impl Reply, Infallible> { | ||
83 | // debug!("update existing game: id={}, game={:?}", id, updated_game); | ||
84 | |||
85 | // let mut games = db.lock().await; | ||
86 | |||
87 | // match games.iter_mut().find(|game| game.id == id) { | ||
88 | // Some(game) => { | ||
89 | // *game = updated_game; | ||
90 | |||
91 | // Ok(StatusCode::OK) | ||
92 | // } | ||
93 | // None => { | ||
94 | // debug!("game of given id not found"); | ||
95 | |||
96 | // Ok(StatusCode::NOT_FOUND) | ||
97 | // } | ||
98 | // } | ||
99 | // } | ||
100 | |||
101 | // `DELETE /games/:id` | ||
102 | // pub async fn delete_game(id: u64, db: Db) -> Result<impl Reply, Infallible> { | ||
103 | // debug!("delete game: id={}", id); | ||
104 | |||
105 | // let mut games = db.lock().await; | ||
106 | |||
107 | // let len = games.len(); | ||
108 | |||
109 | // // Removes all games with given id | ||
110 | // games.retain(|game| game.id != id); | ||
111 | |||
112 | // // If games length was smaller that means specyfic game was found and removed | ||
113 | // let deleted = games.len() != len; | ||
114 | |||
115 | // if deleted { | ||
116 | // Ok(StatusCode::NO_CONTENT) | ||
117 | // } else { | ||
118 | // debug!("game of given id not found"); | ||
119 | |||
120 | // Ok(StatusCode::NOT_FOUND) | ||
121 | // } | ||
122 | // } | ||