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?))
|
||||
}
|
Reference in New Issue
Block a user