|
@ -2,7 +2,7 @@ use actix_web::{get, post, web, HttpResponse}; |
|
|
|
|
|
|
|
|
use crate::actions;
|
|
|
use crate::actions;
|
|
|
use crate::models;
|
|
|
use crate::models;
|
|
|
use crate::types::DbPool;
|
|
|
|
|
|
|
|
|
use crate::AppConfig;
|
|
|
use actix_web::error::ResponseError;
|
|
|
use actix_web::error::ResponseError;
|
|
|
use std::rc::Rc;
|
|
|
use std::rc::Rc;
|
|
|
use validator::Validate;
|
|
|
use validator::Validate;
|
|
@ -10,12 +10,13 @@ use validator::Validate; |
|
|
/// Finds user by UID.
|
|
|
/// Finds user by UID.
|
|
|
#[get("/get/users/{user_id}")]
|
|
|
#[get("/get/users/{user_id}")]
|
|
|
pub async fn get_user(
|
|
|
pub async fn get_user(
|
|
|
pool: web::Data<DbPool>,
|
|
|
|
|
|
|
|
|
config: web::Data<AppConfig>,
|
|
|
user_id: web::Path<i32>,
|
|
|
user_id: web::Path<i32>,
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
let u_id = user_id.into_inner();
|
|
|
let u_id = user_id.into_inner();
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
let res = web::block(move || {
|
|
|
let res = web::block(move || {
|
|
|
|
|
|
let pool = &config.pool;
|
|
|
let conn = pool.get()?;
|
|
|
let conn = pool.get()?;
|
|
|
actions::find_user_by_uid(u_id, &conn)
|
|
|
actions::find_user_by_uid(u_id, &conn)
|
|
|
})
|
|
|
})
|
|
@ -34,10 +35,11 @@ pub async fn get_user( |
|
|
|
|
|
|
|
|
#[get("/get/users")]
|
|
|
#[get("/get/users")]
|
|
|
pub async fn get_all_users(
|
|
|
pub async fn get_all_users(
|
|
|
pool: web::Data<DbPool>,
|
|
|
|
|
|
|
|
|
config: web::Data<AppConfig>,
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
let res = web::block(move || {
|
|
|
let res = web::block(move || {
|
|
|
|
|
|
let pool = &config.pool;
|
|
|
let conn = pool.get()?;
|
|
|
let conn = pool.get()?;
|
|
|
actions::get_all(&conn)
|
|
|
actions::get_all(&conn)
|
|
|
})
|
|
|
})
|
|
@ -65,12 +67,13 @@ pub async fn get_all_users( |
|
|
/// Inserts new user with name defined in form.
|
|
|
/// Inserts new user with name defined in form.
|
|
|
#[post("/do_registration")]
|
|
|
#[post("/do_registration")]
|
|
|
pub async fn add_user(
|
|
|
pub async fn add_user(
|
|
|
pool: web::Data<DbPool>,
|
|
|
|
|
|
|
|
|
config: web::Data<AppConfig>,
|
|
|
form: web::Json<models::NewUser>,
|
|
|
form: web::Json<models::NewUser>,
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
) -> Result<HttpResponse, impl ResponseError> {
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
// use web::block to offload blocking Diesel code without blocking server thread
|
|
|
let res = match form.0.validate() {
|
|
|
let res = match form.0.validate() {
|
|
|
Ok(_) => web::block(move || {
|
|
|
Ok(_) => web::block(move || {
|
|
|
|
|
|
let pool = &config.pool;
|
|
|
let conn = pool.get()?;
|
|
|
let conn = pool.get()?;
|
|
|
actions::insert_new_user(Rc::new(form.0), &conn)
|
|
|
actions::insert_new_user(Rc::new(form.0), &conn)
|
|
|
})
|
|
|
})
|
|
|