blob: 23d20397a69dd2dcb7c5b6db7169ce1558f909b2 (
plain) (
tree)
|
|
use crate::dtos::*;
use crate::product_repo;
use crate::types::DbPool;
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> {
let conn = pool.get().expect("Couldn't get DB connection from pool.");
let products: Vec<Product> = web::block(move || product_repo::read_products(&conn))
.await
.map_err(|e| {
eprintln!("{}", e);
HttpResponse::InternalServerError().body(e.to_string())
})?
.into_iter()
.map(|p| p.into())
.collect();
Ok(HttpResponse::Ok().json(products))
}
#[patch("/products/{id}")]
async fn update_product(
_pool: web::Data<DbPool>,
id: web::Path<u32>,
updated_product: web::Json<ProductPatch>,
) -> 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())
}
|