From c3ba5ad5ebe1d5bb28ed0a340af93e8547b1c5bc Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 7 Apr 2021 01:08:31 +0300 Subject: Initial commit --- src/handlers.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/handlers.rs (limited to 'src/handlers.rs') 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 @@ +// 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 { + 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 { + debug!("list all transactions"); + + let transactions = db.lock().await; + + let transactions: Vec = 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 { +// debug!("list all games"); + +// let games = db.lock().await; +// let games: Vec = 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 { +// 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 { +// 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 { +// 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) +// } +// } -- cgit v1.2.3-70-g09d2