diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-10-05 22:05:58 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-10-05 22:05:58 -0400 |
commit | 743ae22168b1fcdf2e1e54bcadbb1bb3fce3370d (patch) | |
tree | 3d393ff1ffd06d1bf470430b2316f46000794806 /dichroism/src/handlers.rs | |
parent | f8bf353073220ce329d8eb347e3574d5793b6d26 (diff) | |
download | theglassyladies-743ae22168b1fcdf2e1e54bcadbb1bb3fce3370d.tar.xz theglassyladies-743ae22168b1fcdf2e1e54bcadbb1bb3fce3370d.zip |
started work on schema, models, and repos
Diffstat (limited to 'dichroism/src/handlers.rs')
-rw-r--r-- | dichroism/src/handlers.rs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs index 1e0ae28..e4ea6a0 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -1,13 +1,29 @@ +use super::image_repo; +use super::product_repo; +use super::types::DbPool; +use crate::config::Config; use crate::image_api; -use actix_web::{get, post, HttpResponse, Responder}; +use actix_web::{get, 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() + })?; + Ok(HttpResponse::Ok().json(images)) +} + #[post("/images")] -async fn create_image(req_body: String) -> impl Responder { +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, @@ -24,6 +40,13 @@ async fn create_image(req_body: String) -> impl Responder { } #[get("/products")] -async fn get_products(_req_body: String) -> impl Responder { - HttpResponse::Ok().body("got 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)) } |