Add first routes for accounts management
This commit is contained in:
35
moneymgr_backend/src/controllers/accounts_controller.rs
Normal file
35
moneymgr_backend/src/controllers/accounts_controller.rs
Normal 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?))
|
||||
}
|
@ -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;
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user