Can update account information

This commit is contained in:
2025-04-07 21:13:36 +02:00
parent 559eadfd05
commit 1977b5209c
3 changed files with 32 additions and 19 deletions

View File

@ -1,31 +1,17 @@
use crate::controllers::HttpResult;
use crate::controllers::server_controller::ServerConstraints;
use crate::extractors::account_extractor::AccountInPath;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::services::accounts_service;
use crate::services::accounts_service::UpdateAccountQuery;
use actix_web::{HttpResponse, web};
#[derive(serde::Deserialize)]
pub struct CreateAccountRequest {
name: String,
}
/// Create a new account
pub async fn create(auth: AuthExtractor, req: web::Json<CreateAccountRequest>) -> HttpResult {
let constraints = ServerConstraints::default();
if !constraints.account_name.check_str(&req.name) {
return Ok(HttpResponse::BadRequest().json("Invalid account name length!"));
pub async fn create(auth: AuthExtractor, req: web::Json<UpdateAccountQuery>) -> HttpResult {
if let Some(err) = req.check_error() {
return Ok(HttpResponse::BadRequest().json(err));
}
accounts_service::create(
auth.user_id(),
&UpdateAccountQuery {
name: req.name.clone(),
},
)
.await?;
accounts_service::create(auth.user_id(), &req).await?;
Ok(HttpResponse::Created().finish())
}
@ -40,6 +26,17 @@ pub async fn get_single(account: AccountInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(account.as_ref()))
}
/// Update an account information
pub async fn update(account: AccountInPath, req: web::Json<UpdateAccountQuery>) -> HttpResult {
if let Some(err) = req.check_error() {
return Ok(HttpResponse::BadRequest().json(err));
}
accounts_service::update(account.as_ref().id(), &req).await?;
Ok(HttpResponse::Accepted().finish())
}
/// Delete an account
pub async fn delete(account: AccountInPath) -> HttpResult {
accounts_service::delete(account.as_ref().id()).await?;