summaryrefslogtreecommitdiff
path: root/dichroism/src/handlers.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-20 09:50:08 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-20 09:50:08 -0400
commit76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e (patch)
treedfc108120f067880b708a64ef6567879f4bb3284 /dichroism/src/handlers.rs
parentd3a28fde46bb06f084c74904fa8849b40e5f8c87 (diff)
downloadtheglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.tar.xz
theglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.zip
back to basics -- working on the most-required functionality first, like handlers
Diffstat (limited to 'dichroism/src/handlers.rs')
-rw-r--r--dichroism/src/handlers.rs79
1 files changed, 40 insertions, 39 deletions
diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs
index f8f10d3..1f657c2 100644
--- a/dichroism/src/handlers.rs
+++ b/dichroism/src/handlers.rs
@@ -1,53 +1,54 @@
-use super::image_repo;
-use super::product_repo;
+//use super::product_repo;
use super::types::DbPool;
-use crate::config::Config;
-use crate::image_api;
-use actix_web::{get, post, web, Error, HttpResponse, Responder};
+//use crate::config::Config;
+use actix_web::{get, patch, post, web, Error, HttpResponse, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hey, this is an API!")
}
-#[get("/images")]
-async fn get_images(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
- let conn = pool.get().expect("Couldn't get DB connection from pool.");
- let images = web::block(move || image_repo::read_images(&conn))
- .await
- .map_err(|e| {
- eprintln!("{}", e);
- HttpResponse::InternalServerError().finish()
- })?;
- dbg!(&images);
- Ok(HttpResponse::Ok().json(images))
+#[get("/products")]
+async fn get_products(_pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
+ dbg!("got products");
+ Ok(HttpResponse::Ok().finish())
+ //let conn = pool.get().expect("Couldn't get DB connection from pool.");
+ //let products = web::block(move || product_repo::read_products(&conn))
+ // .await
+ // .map_err(|e| {
+ // eprintln!("{}", e);
+ // HttpResponse::InternalServerError().finish()
+ // })?;
+ //Ok(HttpResponse::Ok().json(products))
}
-#[post("/images")]
-async fn create_image(_config: web::Data<Config>, req_body: String) -> impl Responder {
- // let data = match image_api::extract_data(&req_body) {
- // Err(e) => return HttpResponse::BadRequest().body(format!("fail: {}", e.to_string())),
- // Ok(d) => d,
- // };
+#[patch("/products/{id}")]
+async fn update_product(
+ _pool: web::Data<DbPool>,
+ id: web::Path<u32>,
+ updated_product: web::Json<UpdatedProduct>,
+) -> Result<HttpResponse, Error> {
+ dbg!(id, updated_product);
+ Ok(HttpResponse::Ok().finish())
+}
- // if let Err(e) = image_api::generate_images(data) {
- // return HttpResponse::BadRequest().body(format!(
- // "Unable to extract image from data URI: {}",
- // e.to_string()
- // ));
- // }
+#[post("/products")]
+async fn create_product(
+ _pool: web::Data<DbPool>,
+ new_product: web::Json<NewProduct>,
+) -> Result<HttpResponse, Error> {
+ dbg!(new_product);
+ Ok(HttpResponse::Ok().finish())
+}
- HttpResponse::Ok().body("Image created.")
+#[derive(Debug, Deserialize)]
+pub struct NewProduct {
+ pub name: String,
+ pub description: String,
}
-#[get("/products")]
-async fn get_products(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
- let conn = pool.get().expect("Couldn't get DB connection from pool.");
- let products = web::block(move || product_repo::read_products(&conn))
- .await
- .map_err(|e| {
- eprintln!("{}", e);
- HttpResponse::InternalServerError().finish()
- })?;
- Ok(HttpResponse::Ok().json(products))
+#[derive(Debug, Deserialize)]
+pub struct UpdatedProduct {
+ pub name: Option<String>,
+ pub description: Option<String>,
}