[USERS] Finish crud
This commit is contained in:
parent
4ddc96e983
commit
db68f3fbcf
3 changed files with 61 additions and 28 deletions
|
@ -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 yew::ServerRenderer;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -26,20 +26,48 @@ async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("create")]
|
#[get("/{id}")]
|
||||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
async fn show(pool: web::Data<DbPool>, id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||||
let user = run_query(|conn| {
|
let user = run_query(move |conn| User::find_by_id(conn, &id), &pool).await?;
|
||||||
let user = User::new(NewUser {
|
|
||||||
name: "John".to_string(),
|
|
||||||
});
|
|
||||||
user.insert(conn)
|
|
||||||
}, &pool).await?;
|
|
||||||
|
|
||||||
let renderer =
|
let renderer =
|
||||||
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
||||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_routes(cfg: &mut web::ServiceConfig) {
|
#[post("")]
|
||||||
cfg.service(web::scope("/users").service(index).service(create));
|
async fn create(pool: web::Data<DbPool>, user: web::Json<NewUser>) -> Result<HttpResponse, Error> {
|
||||||
|
let user = run_query(move |conn| User::create(conn, &user), &pool).await?;
|
||||||
|
|
||||||
|
let renderer =
|
||||||
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
||||||
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/{id}")]
|
||||||
|
async fn update(pool: web::Data<DbPool>, id: web::Path<String>, user: web::Json<User>) -> Result<HttpResponse, Error> {
|
||||||
|
let user = run_query(move |conn| User::find_by_id(conn, &id)?.update(conn, &user), &pool).await?;
|
||||||
|
|
||||||
|
let renderer =
|
||||||
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
||||||
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/{id}")]
|
||||||
|
async fn delete(pool: web::Data<DbPool>, id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||||
|
let user = run_query(move |conn| User::find_by_id(conn, &id)?.delete(conn), &pool).await?;
|
||||||
|
|
||||||
|
let renderer = ServerRenderer::<UserComponent>::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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
use actix_web::{web, Error};
|
use actix_web::{web, Error};
|
||||||
use diesel::{r2d2::{self, ConnectionManager}, SqliteConnection};
|
use diesel::{
|
||||||
|
r2d2::{self, ConnectionManager},
|
||||||
|
SqliteConnection,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
pub(crate) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
pub(crate) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
pub(crate) async fn run_query<T, F>(query: F, pool: &web::Data<DbPool>) -> Result<T, Error>
|
pub(crate) async fn run_query<T, F>(query: F, pool: &web::Data<DbPool>) -> Result<T, Error>
|
||||||
where
|
where
|
||||||
F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult<T> + Send + 'static,
|
F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult<T> + Send + 'static,
|
||||||
T: Send + 'static
|
T: Send + 'static,
|
||||||
{
|
{
|
||||||
let conn = pool.get()
|
let conn = pool
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
.get()
|
||||||
web::block(move || query(&conn) )
|
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||||
.await?
|
web::block(move || query(&conn))
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)
|
.await?
|
||||||
}
|
.map_err(actix_web::error::ErrorInternalServerError)
|
||||||
|
}
|
||||||
|
|
|
@ -27,25 +27,26 @@ impl User {
|
||||||
users::table.load(conn)
|
users::table.load(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_by_id(conn: &SqliteConnection, id: String) -> QueryResult<Self> {
|
pub fn find_by_id(conn: &SqliteConnection, id: &String) -> QueryResult<Self> {
|
||||||
users::table
|
users::table
|
||||||
.filter(users::id.eq(id))
|
.filter(users::id.eq(id))
|
||||||
.first(conn)
|
.first(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
pub fn create(conn: &SqliteConnection, new_user: &NewUser) -> QueryResult<Self> {
|
||||||
use crate::schema::users::dsl::*;
|
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(user)
|
||||||
|
|
||||||
Ok(self.clone())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
pub fn update(&self, conn: &SqliteConnection, data: &User) -> QueryResult<Self> {
|
||||||
use crate::schema::users::dsl::*;
|
use crate::schema::users::dsl::*;
|
||||||
|
|
||||||
diesel::update(users.find(self.id.clone()))
|
diesel::update(users.find(self.id.clone()))
|
||||||
.set(self)
|
.set(data)
|
||||||
.execute(conn)?;
|
.execute(conn)?;
|
||||||
|
|
||||||
Ok(self.clone())
|
Ok(self.clone())
|
||||||
|
|
Loading…
Reference in a new issue