diff options
author | Adam Carpenter <atc@53hor.net> | 2020-05-17 19:54:26 -0400 |
---|---|---|
committer | Adam Carpenter <atc@53hor.net> | 2020-05-17 19:54:26 -0400 |
commit | bf4e3c07d81d45346052a19f46fa79f0c25ea6cd (patch) | |
tree | 6e527bfa1d044d77b0d3502e7b7e9898e362e1c9 /dichroism | |
parent | 676e527bab65350797cf908b3e3bdbcec2cca811 (diff) | |
download | theglassyladies-bf4e3c07d81d45346052a19f46fa79f0c25ea6cd.tar.xz theglassyladies-bf4e3c07d81d45346052a19f46fa79f0c25ea6cd.zip |
updated main
Diffstat (limited to 'dichroism')
-rw-r--r-- | dichroism/src/main.rs | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 8e97d04..eec1db0 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,7 +1,7 @@ -use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder}; +use actix_web::{guard, web, App, Either, Error, HttpRequest, HttpResponse, HttpServer, Responder}; use futures::future::{ready, Ready}; use listenfd::ListenFd; -use serde::Serialize; +use serde::{Deserialize, Serialize}; #[derive(Serialize)] struct Product { @@ -26,20 +26,56 @@ impl Responder for Product { } } -async fn products() -> impl Responder { - Product { +#[derive(Deserialize)] +struct NewProduct { + name: String, + quantity: i32, + cents: u32, + img_path: String, + description: String, +} + +type ProductResult = Either<HttpResponse, Result<Product, Error>>; + +async fn get_single_product(path: web::Path<(u32,)>) -> ProductResult { + let id = path.0; + + if id != 1 { + Either::A(HttpResponse::BadRequest().body("bad data")) + } else { + Either::B(Ok(Product { + id: 1, + name: "Beach Box".to_string(), + quantity: 3, + cents: 1100, + img_path: "/beach_box.jpg".to_string(), + description: "It's a beach in a box".to_string(), + })) + } +} + +async fn get_all_products() -> impl Responder { + let product = Product { id: 1, name: "Beach Box".to_string(), quantity: 3, cents: 1100, img_path: "/beach_box.jpg".to_string(), description: "It's a beach in a box".to_string(), - } + }; + + web::Json(vec![product]) } -async fn greet(req: HttpRequest) -> impl Responder { - let name = req.match_info().get("name").unwrap_or("World"); - format!("Hello {}!", &name) +async fn create_product(new_product: web::Json<NewProduct>) -> impl Responder { + Product { + id: 2, + name: new_product.name.to_owned(), + quantity: new_product.quantity, + cents: new_product.cents, + img_path: new_product.img_path.to_owned(), + description: new_product.description.to_owned(), + } } async fn index() -> impl Responder { @@ -52,8 +88,29 @@ async fn main() -> std::io::Result<()> { let mut server = HttpServer::new(|| { App::new() .route("/", web::get().to(index)) - .route("/{name}", web::get().to(greet)) - .route("/products", web::get().to(products)) + .service( + web::resource("/products") + .route( + web::route() + .guard(guard::Get()) + .guard(guard::Header("content-type", "application/json")) + .to(get_all_products), + ) + .route( + web::route() + .guard(guard::Post()) + .guard(guard::Header("content-type", "application/json")) + .to(create_product), + ), + ) + .service( + web::resource("/products/{id}").route( + web::route() + .guard(guard::Get()) + .guard(guard::Header("content-type", "application/json")) + .to(get_single_product), + ), + ) }); server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { |