diff options
Diffstat (limited to 'src/routers/account.rs')
| -rw-r--r-- | src/routers/account.rs | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/src/routers/account.rs b/src/routers/account.rs index 51ce911..e39327a 100644 --- a/src/routers/account.rs +++ b/src/routers/account.rs | |||
| @@ -2,30 +2,23 @@ use axum::extract::State; | |||
| 2 | use chrono::{DateTime, Duration, Utc}; | 2 | use chrono::{DateTime, Duration, Utc}; |
| 3 | use entity::users::{self}; | 3 | use entity::users::{self}; |
| 4 | use sea_orm::{ | 4 | use sea_orm::{ |
| 5 | ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait, | 5 | ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, ModelTrait, |
| 6 | IntoActiveModel, ModelTrait, QueryFilter, | 6 | QueryFilter, |
| 7 | }; | 7 | }; |
| 8 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
| 9 | use utoipa::ToSchema; | 9 | use utoipa::ToSchema; |
| 10 | use utoipa_axum::{router::OpenApiRouter, routes}; | 10 | use utoipa_axum::{router::OpenApiRouter, routes}; |
| 11 | 11 | ||
| 12 | use crate::{ | 12 | use crate::{ |
| 13 | ApiError, ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError, | 13 | ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError, SuccessResponse, |
| 14 | SuccessResponse, create_jwt, create_password, | 14 | create_jwt, create_password, |
| 15 | extract::{ApiJson, Auth}, | 15 | extract::{ApiJson, Auth}, |
| 16 | models::Account, | 16 | models::Account, |
| 17 | tags::ACCOUNT, | 17 | tags::ACCOUNT, |
| 18 | util::username_exists, | ||
| 18 | validate_password, | 19 | validate_password, |
| 19 | }; | 20 | }; |
| 20 | 21 | ||
| 21 | async fn username_exists(username: &str, db: &DatabaseConnection) -> Result<bool, ApiError> { | ||
| 22 | Ok(users::Entity::find() | ||
| 23 | .filter(users::Column::Username.eq(username)) | ||
| 24 | .one(db) | ||
| 25 | .await? | ||
| 26 | .is_some()) | ||
| 27 | } | ||
| 28 | |||
| 29 | #[derive(Serialize, ToSchema)] | 22 | #[derive(Serialize, ToSchema)] |
| 30 | #[schema(description = "Authorization token information")] | 23 | #[schema(description = "Authorization token information")] |
| 31 | struct Token { | 24 | struct Token { |
| @@ -205,7 +198,7 @@ async fn login( | |||
| 205 | } | 198 | } |
| 206 | 199 | ||
| 207 | #[utoipa::path( | 200 | #[utoipa::path( |
| 208 | put, | 201 | patch, |
| 209 | path = "/update/password", | 202 | path = "/update/password", |
| 210 | tag = ACCOUNT, | 203 | tag = ACCOUNT, |
| 211 | summary = "Change password", | 204 | summary = "Change password", |
| @@ -214,7 +207,7 @@ async fn login( | |||
| 214 | responses( | 207 | responses( |
| 215 | ( | 208 | ( |
| 216 | status = 200, body = SuccessResponse<Account>, | 209 | status = 200, body = SuccessResponse<Account>, |
| 217 | description = "Success response with the updated account data" | 210 | description = "Success response with the changed account data" |
| 218 | ), | 211 | ), |
| 219 | GlobalResponses | 212 | GlobalResponses |
| 220 | ), | 213 | ), |
| @@ -238,7 +231,7 @@ async fn update_password( | |||
| 238 | } | 231 | } |
| 239 | 232 | ||
| 240 | #[utoipa::path( | 233 | #[utoipa::path( |
| 241 | put, | 234 | patch, |
| 242 | path = "/update/username", | 235 | path = "/update/username", |
| 243 | tag = ACCOUNT, | 236 | tag = ACCOUNT, |
| 244 | summary = "Change username", | 237 | summary = "Change username", |
| @@ -247,7 +240,7 @@ async fn update_password( | |||
| 247 | responses( | 240 | responses( |
| 248 | ( | 241 | ( |
| 249 | status = 200, body = SuccessResponse<Account>, | 242 | status = 200, body = SuccessResponse<Account>, |
| 250 | description = "Success response with the updated account data" | 243 | description = "Success response with the changed account data" |
| 251 | ), | 244 | ), |
| 252 | GlobalResponses | 245 | GlobalResponses |
| 253 | ), | 246 | ), |
| @@ -273,7 +266,7 @@ async fn update_username( | |||
| 273 | } | 266 | } |
| 274 | 267 | ||
| 275 | #[utoipa::path( | 268 | #[utoipa::path( |
| 276 | put, | 269 | patch, |
| 277 | path = "/update/name", | 270 | path = "/update/name", |
| 278 | tag = ACCOUNT, | 271 | tag = ACCOUNT, |
| 279 | summary = "Change name", | 272 | summary = "Change name", |
| @@ -282,7 +275,7 @@ async fn update_username( | |||
| 282 | responses( | 275 | responses( |
| 283 | ( | 276 | ( |
| 284 | status = 200, body = SuccessResponse<Account>, | 277 | status = 200, body = SuccessResponse<Account>, |
| 285 | description = "Success response with the updated account data" | 278 | description = "Success response with the changed account data" |
| 286 | ), | 279 | ), |
| 287 | GlobalResponses | 280 | GlobalResponses |
| 288 | ), | 281 | ), |
| @@ -332,7 +325,6 @@ async fn delete( | |||
| 332 | } | 325 | } |
| 333 | 326 | ||
| 334 | user.clone().delete(&state.db).await?; | 327 | user.clone().delete(&state.db).await?; |
| 335 | |||
| 336 | Ok(SuccessResponse::ok(user.into())) | 328 | Ok(SuccessResponse::ok(user.into())) |
| 337 | } | 329 | } |
| 338 | 330 | ||
