diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/routers/account.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/routers/account.rs b/src/routers/account.rs index 0d6d4c1..8e51a69 100644 --- a/src/routers/account.rs +++ b/src/routers/account.rs | |||
| @@ -82,6 +82,23 @@ struct ChangePasswordRequest { | |||
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | #[derive(Deserialize, ToSchema)] | 84 | #[derive(Deserialize, ToSchema)] |
| 85 | #[schema(description = "Change account password data")] | ||
| 86 | struct ChangeUsernameRequest { | ||
| 87 | #[schema(examples("ivanov_ivan", "john_doe"))] | ||
| 88 | new_username: String, | ||
| 89 | } | ||
| 90 | |||
| 91 | #[derive(Deserialize, ToSchema)] | ||
| 92 | #[schema(description = "Change account name data")] | ||
| 93 | #[serde(rename_all = "snake_case")] | ||
| 94 | enum ChangeNameRequest { | ||
| 95 | #[schema(examples("John", "Иван"))] | ||
| 96 | FirstName(String), | ||
| 97 | #[schema(examples("Doe", "Иванов"))] | ||
| 98 | LastName(String), | ||
| 99 | } | ||
| 100 | |||
| 101 | #[derive(Deserialize, ToSchema)] | ||
| 85 | #[schema(description = "Account delete data")] | 102 | #[schema(description = "Account delete data")] |
| 86 | struct DeleteUserRequest { | 103 | struct DeleteUserRequest { |
| 87 | #[schema(examples("secret-password"))] | 104 | #[schema(examples("secret-password"))] |
| @@ -242,6 +259,90 @@ async fn change_password( | |||
| 242 | } | 259 | } |
| 243 | 260 | ||
| 244 | #[utoipa::path( | 261 | #[utoipa::path( |
| 262 | put, | ||
| 263 | path = "/change/username", | ||
| 264 | tag = ACCOUNT, | ||
| 265 | summary = "Change username", | ||
| 266 | request_body = ChangeUsernameRequest, | ||
| 267 | responses( | ||
| 268 | ( | ||
| 269 | status = 200, body = SuccessResponse<Account>, | ||
| 270 | description = "Success response with changed account data" | ||
| 271 | ), | ||
| 272 | GlobalResponses | ||
| 273 | ), | ||
| 274 | security(("auth" = [])) | ||
| 275 | )] | ||
| 276 | async fn change_username( | ||
| 277 | State(state): State<AppState>, | ||
| 278 | Auth(user): Auth, | ||
| 279 | ApiJson(req): ApiJson<ChangeUsernameRequest>, | ||
| 280 | ) -> ApiResult<Account> { | ||
| 281 | let user_exists = users::Entity::find() | ||
| 282 | .filter(users::Column::Username.eq(&req.new_username)) | ||
| 283 | .one(&state.db) | ||
| 284 | .await? | ||
| 285 | .is_some(); | ||
| 286 | |||
| 287 | if user_exists { | ||
| 288 | return Err(ClientError::UserAlreadyExists { | ||
| 289 | username: req.new_username, | ||
| 290 | } | ||
| 291 | .into()); | ||
| 292 | } | ||
| 293 | |||
| 294 | let mut active_user = user.into_active_model(); | ||
| 295 | active_user.username = Set(req.new_username); | ||
| 296 | |||
| 297 | let user = active_user.update(&state.db).await?; | ||
| 298 | Ok(SuccessResponse::ok(Account { | ||
| 299 | id: user.id, | ||
| 300 | username: user.username, | ||
| 301 | first_name: user.first_name, | ||
| 302 | last_name: user.last_name, | ||
| 303 | })) | ||
| 304 | } | ||
| 305 | |||
| 306 | #[utoipa::path( | ||
| 307 | put, | ||
| 308 | path = "/change/name", | ||
| 309 | tag = ACCOUNT, | ||
| 310 | summary = "Change name", | ||
| 311 | request_body = ChangeNameRequest, | ||
| 312 | responses( | ||
| 313 | ( | ||
| 314 | status = 200, body = SuccessResponse<Account>, | ||
| 315 | description = "Success response with changed account data" | ||
| 316 | ), | ||
| 317 | GlobalResponses | ||
| 318 | ), | ||
| 319 | security(("auth" = [])) | ||
| 320 | )] | ||
| 321 | async fn change_name( | ||
| 322 | State(state): State<AppState>, | ||
| 323 | Auth(user): Auth, | ||
| 324 | ApiJson(req): ApiJson<ChangeNameRequest>, | ||
| 325 | ) -> ApiResult<Account> { | ||
| 326 | let mut active_user = user.into_active_model(); | ||
| 327 | match req { | ||
| 328 | ChangeNameRequest::FirstName(new_first_name) => { | ||
| 329 | active_user.first_name = Set(new_first_name); | ||
| 330 | } | ||
| 331 | ChangeNameRequest::LastName(new_last_name) => { | ||
| 332 | active_user.first_name = Set(new_last_name); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | let user = active_user.update(&state.db).await?; | ||
| 337 | Ok(SuccessResponse::ok(Account { | ||
| 338 | id: user.id, | ||
| 339 | username: user.username, | ||
| 340 | first_name: user.first_name, | ||
| 341 | last_name: user.last_name, | ||
| 342 | })) | ||
| 343 | } | ||
| 344 | |||
| 345 | #[utoipa::path( | ||
| 245 | delete, | 346 | delete, |
| 246 | path = "/delete", | 347 | path = "/delete", |
| 247 | tag = ACCOUNT, | 348 | tag = ACCOUNT, |
| @@ -281,5 +382,7 @@ pub(crate) fn router() -> OpenApiRouter<AppState> { | |||
| 281 | .routes(routes!(register)) | 382 | .routes(routes!(register)) |
| 282 | .routes(routes!(login)) | 383 | .routes(routes!(login)) |
| 283 | .routes(routes!(change_password)) | 384 | .routes(routes!(change_password)) |
| 385 | .routes(routes!(change_username)) | ||
| 386 | .routes(routes!(change_name)) | ||
| 284 | .routes(routes!(delete)) | 387 | .routes(routes!(delete)) |
| 285 | } | 388 | } |
