Add first routes for accounts management

This commit is contained in:
2025-04-03 23:14:55 +02:00
parent 03f57a0ad7
commit 72e67d9e91
14 changed files with 202 additions and 43 deletions

View File

@ -0,0 +1,35 @@
use crate::controllers::HttpResult;
use crate::controllers::server_controller::ServerConstraints;
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!"));
}
accounts_service::create(
auth.user_id(),
&UpdateAccountQuery {
name: req.name.clone(),
},
)
.await?;
Ok(HttpResponse::Created().finish())
}
/// Get the list of accounts of the user
pub async fn get_list(auth: AuthExtractor) -> HttpResult {
Ok(HttpResponse::Ok().json(accounts_service::get_list_user(auth.user_id()).await?))
}

View File

@ -2,6 +2,7 @@ use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use std::error::Error;
pub mod accounts_controller;
pub mod auth_controller;
pub mod server_controller;
pub mod static_controller;

View File

@ -39,6 +39,7 @@ pub struct ServerConstraints {
pub token_name: LenConstraints,
pub token_ip_net: LenConstraints,
pub token_max_inactivity: LenConstraints,
pub account_name: LenConstraints,
}
impl Default for ServerConstraints {
@ -47,6 +48,7 @@ impl Default for ServerConstraints {
token_name: LenConstraints::new(5, 255),
token_ip_net: LenConstraints::max_only(44),
token_max_inactivity: LenConstraints::new(3600, 3600 * 24 * 365),
account_name: LenConstraints::not_empty(50),
}
}
}