[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 crate::{
|
||||
|
@ -26,20 +26,48 @@ async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
|||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||
}
|
||||
|
||||
#[get("create")]
|
||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||
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<DbPool>, id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
let user = run_query(move |conn| User::find_by_id(conn, &id), &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(create));
|
||||
#[post("")]
|
||||
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 diesel::{r2d2::{self, ConnectionManager}, SqliteConnection};
|
||||
use diesel::{
|
||||
r2d2::{self, ConnectionManager},
|
||||
SqliteConnection,
|
||||
};
|
||||
|
||||
pub mod user;
|
||||
|
||||
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
|
||||
F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult<T> + 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)
|
||||
}
|
||||
let conn = pool
|
||||
.get()
|
||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||
web::block(move || query(&conn))
|
||||
.await?
|
||||
.map_err(actix_web::error::ErrorInternalServerError)
|
||||
}
|
||||
|
|
|
@ -27,25 +27,26 @@ impl User {
|
|||
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
|
||||
.filter(users::id.eq(id))
|
||||
.first(conn)
|
||||
}
|
||||
|
||||
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
||||
use crate::schema::users::dsl::*;
|
||||
pub fn create(conn: &SqliteConnection, new_user: &NewUser) -> QueryResult<Self> {
|
||||
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<Self> {
|
||||
pub fn update(&self, conn: &SqliteConnection, data: &User) -> QueryResult<Self> {
|
||||
use crate::schema::users::dsl::*;
|
||||
|
||||
diesel::update(users.find(self.id.clone()))
|
||||
.set(self)
|
||||
.set(data)
|
||||
.execute(conn)?;
|
||||
|
||||
Ok(self.clone())
|
||||
|
|
Loading…
Reference in a new issue