diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 111e2d6..a4a3c57 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -1,4 +1,4 @@ -use actix_web::{get, web, Error, HttpResponse}; +use actix_web::{get, post, web, Error, HttpResponse, delete, patch}; use yew::ServerRenderer; use crate::{ @@ -26,20 +26,48 @@ async fn index(pool: web::Data) -> Result { Ok(HttpResponse::Ok().body(renderer.render().await)) } -#[get("create")] -async fn create(pool: web::Data) -> Result { - let user = run_query(|conn| { - let user = User::new(NewUser { - name: "John".to_string(), - }); - user.insert(conn) - }, &pool).await?; +#[get("/{id}")] +async fn show(pool: web::Data, id: web::Path) -> Result { + let user = run_query(move |conn| User::find_by_id(conn, &id), &pool).await?; let renderer = ServerRenderer::::with_props(UserComponentProps { user: user }); Ok(HttpResponse::Ok().body(renderer.render().await)) } -pub fn init_routes(cfg: &mut web::ServiceConfig) { - cfg.service(web::scope("/users").service(index).service(create)); +#[post("")] +async fn create(pool: web::Data, user: web::Json) -> Result { + let user = run_query(move |conn| User::create(conn, &user), &pool).await?; + + let renderer = + ServerRenderer::::with_props(UserComponentProps { user: user }); + Ok(HttpResponse::Ok().body(renderer.render().await)) +} + +#[patch("/{id}")] +async fn update(pool: web::Data, id: web::Path, user: web::Json) -> Result { + let user = run_query(move |conn| User::find_by_id(conn, &id)?.update(conn, &user), &pool).await?; + + let renderer = + ServerRenderer::::with_props(UserComponentProps { user: user }); + Ok(HttpResponse::Ok().body(renderer.render().await)) +} + +#[delete("/{id}")] +async fn delete(pool: web::Data, id: web::Path) -> Result { + let user = run_query(move |conn| User::find_by_id(conn, &id)?.delete(conn), &pool).await?; + + let renderer = ServerRenderer::::with_props(UserComponentProps { user: user }); + Ok(HttpResponse::Ok().body(renderer.render().await)) +} + +pub fn init_routes(cfg: &mut web::ServiceConfig) { + cfg.service( + web::scope("/users") + .service(index) + .service(show) + .service(create) + .service(update) + .service(delete) + ); } diff --git a/src/models/mod.rs b/src/models/mod.rs index 0319b98..55406a5 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,18 +1,22 @@ use actix_web::{web, Error}; -use diesel::{r2d2::{self, ConnectionManager}, SqliteConnection}; +use diesel::{ + r2d2::{self, ConnectionManager}, + SqliteConnection, +}; pub mod user; pub(crate) type DbPool = r2d2::Pool>; -pub(crate) async fn run_query(query: F, pool: &web::Data) -> Result +pub(crate) async fn run_query(query: F, pool: &web::Data) -> Result where F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult + Send + 'static, - T: Send + 'static + T: Send + 'static, { - let conn = pool.get() - .map_err(actix_web::error::ErrorInternalServerError)?; - web::block(move || query(&conn) ) - .await? - .map_err(actix_web::error::ErrorInternalServerError) -} \ No newline at end of file + let conn = pool + .get() + .map_err(actix_web::error::ErrorInternalServerError)?; + web::block(move || query(&conn)) + .await? + .map_err(actix_web::error::ErrorInternalServerError) +} diff --git a/src/models/user.rs b/src/models/user.rs index f10fd83..d74d323 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -27,25 +27,26 @@ impl User { users::table.load(conn) } - pub fn find_by_id(conn: &SqliteConnection, id: String) -> QueryResult { + pub fn find_by_id(conn: &SqliteConnection, id: &String) -> QueryResult { users::table .filter(users::id.eq(id)) .first(conn) } - pub fn insert(&self, conn: &SqliteConnection) -> QueryResult { - use crate::schema::users::dsl::*; + pub fn create(conn: &SqliteConnection, new_user: &NewUser) -> QueryResult { + let user = User::new(new_user.clone()); + diesel::insert_into(users::table) + .values(&user) + .execute(conn)?; - diesel::insert_into(users).values(self).execute(conn)?; - - Ok(self.clone()) + Ok(user) } - pub fn update(&self, conn: &SqliteConnection) -> QueryResult { + pub fn update(&self, conn: &SqliteConnection, data: &User) -> QueryResult { use crate::schema::users::dsl::*; diesel::update(users.find(self.id.clone())) - .set(self) + .set(data) .execute(conn)?; Ok(self.clone())