Can update account information
This commit is contained in:
parent
559eadfd05
commit
1977b5209c
@ -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?;
|
||||
|
@ -107,7 +107,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/api/account/{account_id}",
|
||||
web::get().to(accounts_controller::get_single),
|
||||
)
|
||||
// TODO : update account
|
||||
.route(
|
||||
"/api/account/{account_id}",
|
||||
web::put().to(accounts_controller::update),
|
||||
)
|
||||
.route(
|
||||
"/api/account/{account_id}",
|
||||
web::delete().to(accounts_controller::delete),
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::connections::db_connection::db;
|
||||
use crate::controllers::server_controller::ServerConstraints;
|
||||
use crate::models::accounts::{Account, AccountID, NewAccount};
|
||||
use crate::models::users::UserID;
|
||||
use crate::schema::accounts;
|
||||
@ -11,6 +12,18 @@ pub struct UpdateAccountQuery {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl UpdateAccountQuery {
|
||||
pub fn check_error(&self) -> Option<&'static str> {
|
||||
let constraints = ServerConstraints::default();
|
||||
|
||||
if !constraints.account_name.check_str(&self.name) {
|
||||
return Some("Invalid account name length!");
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new account
|
||||
pub async fn create(user_id: UserID, query: &UpdateAccountQuery) -> anyhow::Result<Account> {
|
||||
let new_account = NewAccount {
|
||||
|
Loading…
x
Reference in New Issue
Block a user