//use super::product_repo;
use super::types::DbPool;
//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("/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))
}
#[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())
}
#[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())
}
#[derive(Debug, Deserialize)]
pub struct NewProduct {
pub name: String,
pub description: String,
}
#[derive(Debug, Deserialize)]
pub struct UpdatedProduct {
pub name: Option<String>,
pub description: Option<String>,
}