mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Эндпоинт users/change-group.
This commit is contained in:
@@ -3,11 +3,11 @@ pub mod users {
|
|||||||
use crate::database::models::User;
|
use crate::database::models::User;
|
||||||
use crate::database::schema::users::dsl::users;
|
use crate::database::schema::users::dsl::users;
|
||||||
use crate::database::schema::users::dsl::*;
|
use crate::database::schema::users::dsl::*;
|
||||||
use actix_web::web;
|
|
||||||
use diesel::SelectableHelper;
|
|
||||||
use diesel::{insert_into, ExpressionMethods, QueryResult};
|
|
||||||
use diesel::{QueryDsl, RunQueryDsl};
|
|
||||||
use crate::utility::mutex::MutexScope;
|
use crate::utility::mutex::MutexScope;
|
||||||
|
use actix_web::web;
|
||||||
|
use diesel::{ExpressionMethods, QueryResult, insert_into};
|
||||||
|
use diesel::{QueryDsl, RunQueryDsl};
|
||||||
|
use diesel::{SaveChangesDsl, SelectableHelper};
|
||||||
|
|
||||||
pub fn get(state: &web::Data<AppState>, _id: &String) -> QueryResult<User> {
|
pub fn get(state: &web::Data<AppState>, _id: &String) -> QueryResult<User> {
|
||||||
state.database.scope(|conn| {
|
state.database.scope(|conn| {
|
||||||
@@ -69,7 +69,58 @@ pub mod users {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
|
pub fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
|
||||||
state.database.scope(|conn| insert_into(users).values(user).execute(conn))
|
state
|
||||||
|
.database
|
||||||
|
.scope(|conn| insert_into(users).values(user).execute(conn))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function declaration [User::save][UserSave::save].
|
||||||
|
pub trait UserSave {
|
||||||
|
/// Saves the user's changes to the database.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `state`: The state of the actix-web application that stores the mutex of the [connection][diesel::PgConnection].
|
||||||
|
///
|
||||||
|
/// returns: `QueryResult<User>`
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use crate::database::driver::users;
|
||||||
|
///
|
||||||
|
/// #[derive(Deserialize)]
|
||||||
|
/// struct Params {
|
||||||
|
/// pub username: String,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[patch("/")]
|
||||||
|
/// async fn patch_user(
|
||||||
|
/// app_state: web::Data<AppState>,
|
||||||
|
/// user: SyncExtractor<User>,
|
||||||
|
/// web::Query(params): web::Query<Params>,
|
||||||
|
/// ) -> web::Json<User> {
|
||||||
|
/// let mut user = user.into_inner();
|
||||||
|
///
|
||||||
|
/// user.username = params.username;
|
||||||
|
///
|
||||||
|
/// match user.save(&app_state) {
|
||||||
|
/// Ok(user) => web::Json(user),
|
||||||
|
/// Err(e) => {
|
||||||
|
/// eprintln!("Failed to save user: {e}");
|
||||||
|
/// panic!();
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
fn save(&self, state: &web::Data<AppState>) -> QueryResult<User>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implementation of [UserSave][UserSave] trait.
|
||||||
|
impl UserSave for User {
|
||||||
|
fn save(&self, state: &web::Data<AppState>) -> QueryResult<User> {
|
||||||
|
state.database.scope(|conn| self.save_changes::<Self>(conn))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -95,12 +146,12 @@ pub mod users {
|
|||||||
|
|
||||||
pub mod fcm {
|
pub mod fcm {
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
use crate::database::models::{User, FCM};
|
use crate::database::models::{FCM, User};
|
||||||
|
use crate::utility::mutex::MutexScope;
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use diesel::QueryDsl;
|
use diesel::QueryDsl;
|
||||||
use diesel::RunQueryDsl;
|
use diesel::RunQueryDsl;
|
||||||
use diesel::{BelongingToDsl, QueryResult, SelectableHelper};
|
use diesel::{BelongingToDsl, QueryResult, SelectableHelper};
|
||||||
use crate::utility::mutex::MutexScope;
|
|
||||||
|
|
||||||
pub fn from_user(state: &web::Data<AppState>, user: &User) -> QueryResult<FCM> {
|
pub fn from_user(state: &web::Data<AppState>, user: &User) -> QueryResult<FCM> {
|
||||||
state.database.scope(|conn| {
|
state.database.scope(|conn| {
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ pub fn get_api_scope<
|
|||||||
|
|
||||||
let users_scope = utoipa_actix_web::scope("/users")
|
let users_scope = utoipa_actix_web::scope("/users")
|
||||||
.wrap(JWTAuthorization)
|
.wrap(JWTAuthorization)
|
||||||
|
.service(routes::users::change_group)
|
||||||
.service(routes::users::change_username)
|
.service(routes::users::change_username)
|
||||||
.service(routes::users::me);
|
.service(routes::users::me);
|
||||||
|
|
||||||
|
|||||||
85
src/routes/users/change_group.rs
Normal file
85
src/routes/users/change_group.rs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::app_state::AppState;
|
||||||
|
use crate::database::driver::users::UserSave;
|
||||||
|
use crate::database::models::User;
|
||||||
|
use crate::extractors::base::SyncExtractor;
|
||||||
|
use crate::routes::schema::IntoResponseAsError;
|
||||||
|
use crate::utility::mutex::MutexScope;
|
||||||
|
use actix_web::{post, web};
|
||||||
|
|
||||||
|
#[utoipa::path(responses((status = OK)))]
|
||||||
|
#[post("/change-group")]
|
||||||
|
pub async fn change_group(
|
||||||
|
app_state: web::Data<AppState>,
|
||||||
|
user: SyncExtractor<User>,
|
||||||
|
data: web::Json<Request>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
let mut user = user.into_inner();
|
||||||
|
|
||||||
|
if user.group == data.group {
|
||||||
|
return ErrorCode::SameGroup.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(e) = app_state.schedule.scope(|schedule| match schedule {
|
||||||
|
Some(schedule) => {
|
||||||
|
if schedule.data.groups.contains_key(&data.group) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(ErrorCode::NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Some(ErrorCode::NoSchedule),
|
||||||
|
}) {
|
||||||
|
return e.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
user.group = data.into_inner().group;
|
||||||
|
|
||||||
|
if let Some(e) = user.save(&app_state).err() {
|
||||||
|
eprintln!("Failed to update user: {e}");
|
||||||
|
return ErrorCode::InternalServerError.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(()).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[schema(as = ChangeGroup::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// Название группы.
|
||||||
|
pub group: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||||
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
|
#[schema(as = ChangeGroup::ErrorCode)]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
|
||||||
|
pub enum ErrorCode {
|
||||||
|
/// Расписания ещё не получены.
|
||||||
|
#[display("Schedule not parsed yet.")]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
|
||||||
|
NoSchedule,
|
||||||
|
|
||||||
|
/// Передано то же название группы, что есть на данный момент.
|
||||||
|
#[display("Passed the same group name as it is at the moment.")]
|
||||||
|
SameGroup,
|
||||||
|
|
||||||
|
/// Требуемая группа не существует.
|
||||||
|
#[display("The required group does not exist.")]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
||||||
|
NotFound,
|
||||||
|
|
||||||
|
/// Ошибка на стороне сервера.
|
||||||
|
#[display("Internal server error.")]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::INTERNAL_SERVER_ERROR"]
|
||||||
|
InternalServerError,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
use self::schema::*;
|
use self::schema::*;
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
use crate::database::driver;
|
use crate::database::driver;
|
||||||
|
use crate::database::driver::users::UserSave;
|
||||||
use crate::database::models::User;
|
use crate::database::models::User;
|
||||||
use crate::extractors::base::SyncExtractor;
|
use crate::extractors::base::SyncExtractor;
|
||||||
use crate::routes::schema::IntoResponseAsError;
|
use crate::routes::schema::IntoResponseAsError;
|
||||||
use crate::utility::mutex::MutexScope;
|
|
||||||
use actix_web::{post, web};
|
use actix_web::{post, web};
|
||||||
use diesel::SaveChangesDsl;
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK)))]
|
#[utoipa::path(responses((status = OK)))]
|
||||||
#[post("/change-username")]
|
#[post("/change-username")]
|
||||||
@@ -27,11 +26,7 @@ pub async fn change_username(
|
|||||||
|
|
||||||
user.username = data.into_inner().username;
|
user.username = data.into_inner().username;
|
||||||
|
|
||||||
if let Some(e) = app_state
|
if let Some(e) = user.save(&app_state).err() {
|
||||||
.database
|
|
||||||
.scope(|conn| user.save_changes::<User>(conn))
|
|
||||||
.err()
|
|
||||||
{
|
|
||||||
eprintln!("Failed to update user: {e}");
|
eprintln!("Failed to update user: {e}");
|
||||||
return ErrorCode::InternalServerError.into_response();
|
return ErrorCode::InternalServerError.into_response();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
mod change_group;
|
||||||
mod change_username;
|
mod change_username;
|
||||||
mod me;
|
mod me;
|
||||||
|
|
||||||
|
pub use change_group::*;
|
||||||
pub use change_username::*;
|
pub use change_username::*;
|
||||||
pub use me::*;
|
pub use me::*;
|
||||||
|
|
||||||
// TODO: change-group
|
|
||||||
|
|||||||
Reference in New Issue
Block a user