mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Использование функции для осуществления операций в базе данных вместо ручного блокирования мьютекса.
This commit is contained in:
@@ -5,10 +5,10 @@ use crate::routes::auth::shared::parse_vk_id;
|
||||
use crate::routes::auth::sign_in::schema::SignInData::{Default, Vk};
|
||||
use crate::routes::schema::user::UserResponse;
|
||||
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||
use crate::{utility, AppState};
|
||||
use crate::utility::mutex::MutexScope;
|
||||
use crate::{AppState, utility};
|
||||
use actix_web::{post, web};
|
||||
use diesel::SaveChangesDsl;
|
||||
use std::ops::DerefMut;
|
||||
use web::Json;
|
||||
|
||||
async fn sign_in_combined(
|
||||
@@ -16,8 +16,8 @@ async fn sign_in_combined(
|
||||
app_state: &web::Data<AppState>,
|
||||
) -> Result<UserResponse, ErrorCode> {
|
||||
let user = match &data {
|
||||
Default(data) => driver::users::get_by_username(&app_state.database, &data.username),
|
||||
Vk(id) => driver::users::get_by_vk_id(&app_state.database, *id),
|
||||
Default(data) => driver::users::get_by_username(&app_state, &data.username),
|
||||
Vk(id) => driver::users::get_by_vk_id(&app_state, *id),
|
||||
};
|
||||
|
||||
match user {
|
||||
@@ -35,13 +35,12 @@ async fn sign_in_combined(
|
||||
}
|
||||
}
|
||||
|
||||
let mut lock = app_state.connection();
|
||||
let conn = lock.deref_mut();
|
||||
|
||||
user.access_token = utility::jwt::encode(&user.id);
|
||||
|
||||
user.save_changes::<User>(conn)
|
||||
.expect("Failed to update user");
|
||||
app_state.database.scope(|conn| {
|
||||
user.save_changes::<User>(conn)
|
||||
.expect("Failed to update user")
|
||||
});
|
||||
|
||||
Ok(user.into())
|
||||
}
|
||||
@@ -56,7 +55,9 @@ async fn sign_in_combined(
|
||||
))]
|
||||
#[post("/sign-in")]
|
||||
pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||
sign_in_combined(Default(data.into_inner()), &app_state).await.into()
|
||||
sign_in_combined(Default(data.into_inner()), &app_state)
|
||||
.await
|
||||
.into()
|
||||
}
|
||||
|
||||
#[utoipa::path(responses(
|
||||
@@ -64,7 +65,10 @@ pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> Ser
|
||||
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||
))]
|
||||
#[post("/sign-in-vk")]
|
||||
pub async fn sign_in_vk(data_json: Json<vk::Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||
pub async fn sign_in_vk(
|
||||
data_json: Json<vk::Request>,
|
||||
app_state: web::Data<AppState>,
|
||||
) -> ServiceResponse {
|
||||
let data = data_json.into_inner();
|
||||
|
||||
match parse_vk_id(&data.access_token) {
|
||||
@@ -85,7 +89,7 @@ mod schema {
|
||||
/// Имя пользователя
|
||||
#[schema(examples("n08i40k"))]
|
||||
pub username: String,
|
||||
|
||||
|
||||
/// Пароль
|
||||
pub password: String,
|
||||
}
|
||||
@@ -112,7 +116,7 @@ mod schema {
|
||||
pub enum ErrorCode {
|
||||
/// Некорректное имя пользователя или пароль
|
||||
IncorrectCredentials,
|
||||
|
||||
|
||||
/// Недействительный токен VK ID
|
||||
InvalidVkAccessToken,
|
||||
}
|
||||
@@ -123,8 +127,8 @@ mod schema {
|
||||
pub enum SignInData {
|
||||
/// Имя пользователя и пароль
|
||||
Default(Request),
|
||||
|
||||
/// Идентификатор привязанного аккаунта VK
|
||||
|
||||
/// Идентификатор привязанного аккаунта VK
|
||||
Vk(i32),
|
||||
}
|
||||
}
|
||||
@@ -176,7 +180,7 @@ mod tests {
|
||||
|
||||
let app_state = static_app_state();
|
||||
driver::users::insert_or_ignore(
|
||||
&app_state.database,
|
||||
&app_state,
|
||||
&User {
|
||||
id: id.clone(),
|
||||
username,
|
||||
|
||||
@@ -28,19 +28,19 @@ async fn sign_up_combined(
|
||||
}
|
||||
|
||||
// If user with specified username already exists.
|
||||
if driver::users::contains_by_username(&app_state.database, &data.username) {
|
||||
if driver::users::contains_by_username(&app_state, &data.username) {
|
||||
return Err(ErrorCode::UsernameAlreadyExists);
|
||||
}
|
||||
|
||||
// If user with specified VKID already exists.
|
||||
if let Some(id) = data.vk_id {
|
||||
if driver::users::contains_by_vk_id(&app_state.database, id) {
|
||||
if driver::users::contains_by_vk_id(&app_state, id) {
|
||||
return Err(ErrorCode::VkAlreadyExists);
|
||||
}
|
||||
}
|
||||
|
||||
let user = data.into();
|
||||
driver::users::insert(&app_state.database, &user).unwrap();
|
||||
driver::users::insert(&app_state, &user).unwrap();
|
||||
|
||||
Ok(UserResponse::from(&user)).into()
|
||||
}
|
||||
@@ -281,7 +281,7 @@ mod tests {
|
||||
test_env();
|
||||
|
||||
let app_state = static_app_state();
|
||||
driver::users::delete_by_username(&app_state.database, &"test::sign_up_valid".to_string());
|
||||
driver::users::delete_by_username(&app_state, &"test::sign_up_valid".to_string());
|
||||
|
||||
// test
|
||||
|
||||
@@ -303,7 +303,7 @@ mod tests {
|
||||
|
||||
let app_state = static_app_state();
|
||||
driver::users::delete_by_username(
|
||||
&app_state.database,
|
||||
&app_state,
|
||||
&"test::sign_up_multiple".to_string(),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use crate::utility::mutex::MutexScope;
|
||||
use actix_web::{HttpResponse, Responder, post, web};
|
||||
use diesel::SaveChangesDsl;
|
||||
|
||||
@@ -18,7 +19,10 @@ async fn update_callback(
|
||||
|
||||
user.version = version.into_inner();
|
||||
|
||||
match app_state.lock_connection(|con| user.save_changes::<User>(con)) {
|
||||
match app_state
|
||||
.database
|
||||
.scope(|conn| user.save_changes::<User>(conn))
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok(),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to update user: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user