28
loading...
This website collects cookies to deliver better user experience
endpoint
from here) and will be more clear if we extract that to functions.async fn dinos_create(mut req: Request<State>) -> tide::Result {
let dino: Dino = req.body_json().await?;
// let get a mut ref of our store ( hashMap )
let mut dinos = req.state().dinos.write().await;
dinos.insert(String::from(&dino.name), dino.clone());
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
}
async fn dinos_list(req: tide::Request<State>) -> tide::Result {
let dinos = req.state().dinos.read().await;
// get all the dinos as a vector
let dinos_vec: Vec<Dino> = dinos.values().cloned().collect();
let mut res = Response::new(200);
res.set_body(Body::from_json(&dinos_vec)?);
Ok(res)
}
( ... )
app.at("/dinos")
.post(dinos_create)
.get(dinos_list);
$ cargo test
Finished test [unoptimized + debuginfo] target(s) in 12.48s
Running target/debug/deps/tide_basic_crud-3d6db2bae3cd08a5
running 5 tests
test delete_dino ... ok
test index_page ... ok
test list_dinos ... ok
test create_dino ... ok
test update_dino ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
app.at("/dinos/:name")
.get( dinos_get )
.put( dinos_update )
.delete( dinos_delete );
dinos_
functions in the main
file. Let's refactor this to be more organizedstruct
to represent a rest
entity with a base_path
field.struct RestEntity {
base_path: String,
}
impl RestEntity {
async fn create(mut req: Request<State>) -> tide::Result {
let dino: Dino = req.body_json().await?;
// let get a mut ref of our store ( hashMap )
let mut dinos = req.state().dinos.write().await;
dinos.insert(String::from(&dino.name), dino.clone());
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
}
async fn list(req: tide::Request<State>) -> tide::Result {
let dinos = req.state().dinos.read().await;
// get all the dinos as a vector
let dinos_vec: Vec<Dino> = dinos.values().cloned().collect();
let mut res = Response::new(200);
res.set_body(Body::from_json(&dinos_vec)?);
Ok(res)
}
async fn get(req: tide::Request<State>) -> tide::Result {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(entry) => {
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
}
async fn update(mut req: tide::Request<State>) -> tide::Result {
let dino_update: Dino = req.body_json().await?;
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(mut entry) => {
*entry.get_mut() = dino_update;
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
}
async fn delete(req: tide::Request<State>) -> tide::Result {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let deleted = dinos.remove(&key);
let res = match deleted {
None => Response::new(404),
Some(_) => Response::new(204),
};
Ok(res)
}
}
helper
function that allow us to register
rest
like entities to our server, registering five different endpoints to handle the list/create/read/update/delete operations.fn register_rest_entity(app: &mut Server<State>, entity: RestEntity) {
app.at(&entity.base_path)
.get(RestEntity::list)
.post(RestEntity::create);
app.at(&format!("{}/:id", entity.base_path))
.get(RestEntity::get)
.put(RestEntity::update)
.delete(RestEntity::delete);
}
server
we just need to create a new instance of the struct with the desired base_path
and call the helper fn
let dinos_endpoint = RestEntity {
base_path: String::from("/dinos"),
};
register_rest_entity(&mut app, dinos_endpoint);
cargo test
Compiling tide-basic-crud v0.1.0
Running target/debug/deps/tide_basic_crud-3d6db2bae3cd08a5
running 5 tests
test delete_dino ... ok
test list_dinos ... ok
test create_dino ... ok
test index_page ... ok
test update_dino ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
rest
like entities and implement the basic operations.HashMap
and persist the entities information in a db
.endpoint
from here) and will be more clear if we extract that to functions.async fn dinos_create(mut req: Request<State>) -> tide::Result {
let dino: Dino = req.body_json().await?;
// let get a mut ref of our store ( hashMap )
let mut dinos = req.state().dinos.write().await;
dinos.insert(String::from(&dino.name), dino.clone());
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
}
async fn dinos_list(req: tide::Request<State>) -> tide::Result {
let dinos = req.state().dinos.read().await;
// get all the dinos as a vector
let dinos_vec: Vec<Dino> = dinos.values().cloned().collect();
let mut res = Response::new(200);
res.set_body(Body::from_json(&dinos_vec)?);
Ok(res)
}
( ... )
app.at("/dinos")
.post(dinos_create)
.get(dinos_list);
$ cargo test
Finished test [unoptimized + debuginfo] target(s) in 12.48s
Running target/debug/deps/tide_basic_crud-3d6db2bae3cd08a5
running 5 tests
test delete_dino ... ok
test index_page ... ok
test list_dinos ... ok
test create_dino ... ok
test update_dino ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
app.at("/dinos/:name")
.get( dinos_get )
.put( dinos_update )
.delete( dinos_delete );
dinos_
functions in the main
file. Let's refactor this to be more organizedstruct
to represent a rest
entity with a base_path
field.struct RestEntity {
base_path: String,
}
impl RestEntity {
async fn create(mut req: Request<State>) -> tide::Result {
let dino: Dino = req.body_json().await?;
// let get a mut ref of our store ( hashMap )
let mut dinos = req.state().dinos.write().await;
dinos.insert(String::from(&dino.name), dino.clone());
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
}
async fn list(req: tide::Request<State>) -> tide::Result {
let dinos = req.state().dinos.read().await;
// get all the dinos as a vector
let dinos_vec: Vec<Dino> = dinos.values().cloned().collect();
let mut res = Response::new(200);
res.set_body(Body::from_json(&dinos_vec)?);
Ok(res)
}
async fn get(req: tide::Request<State>) -> tide::Result {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(entry) => {
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
}
async fn update(mut req: tide::Request<State>) -> tide::Result {
let dino_update: Dino = req.body_json().await?;
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(mut entry) => {
*entry.get_mut() = dino_update;
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
}
async fn delete(req: tide::Request<State>) -> tide::Result {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("id")?;
let deleted = dinos.remove(&key);
let res = match deleted {
None => Response::new(404),
Some(_) => Response::new(204),
};
Ok(res)
}
}
helper
function that allow us to register
rest
like entities to our server, registering five different endpoints to handle the list/create/read/update/delete operations.fn register_rest_entity(app: &mut Server<State>, entity: RestEntity) {
app.at(&entity.base_path)
.get(RestEntity::list)
.post(RestEntity::create);
app.at(&format!("{}/:id", entity.base_path))
.get(RestEntity::get)
.put(RestEntity::update)
.delete(RestEntity::delete);
}
server
we just need to create a new instance of the struct with the desired base_path
and call the helper fn
let dinos_endpoint = RestEntity {
base_path: String::from("/dinos"),
};
register_rest_entity(&mut app, dinos_endpoint);
cargo test
Compiling tide-basic-crud v0.1.0
Running target/debug/deps/tide_basic_crud-3d6db2bae3cd08a5
running 5 tests
test delete_dino ... ok
test list_dinos ... ok
test create_dino ... ok
test index_page ... ok
test update_dino ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
rest
like entities and implement the basic operations.HashMap
and persist the entities information in a db
.