1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
//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>,
}
|