Can update account information
This commit is contained in:
		@@ -1,31 +1,17 @@
 | 
				
			|||||||
use crate::controllers::HttpResult;
 | 
					use crate::controllers::HttpResult;
 | 
				
			||||||
use crate::controllers::server_controller::ServerConstraints;
 | 
					 | 
				
			||||||
use crate::extractors::account_extractor::AccountInPath;
 | 
					use crate::extractors::account_extractor::AccountInPath;
 | 
				
			||||||
use crate::extractors::auth_extractor::AuthExtractor;
 | 
					use crate::extractors::auth_extractor::AuthExtractor;
 | 
				
			||||||
use crate::services::accounts_service;
 | 
					use crate::services::accounts_service;
 | 
				
			||||||
use crate::services::accounts_service::UpdateAccountQuery;
 | 
					use crate::services::accounts_service::UpdateAccountQuery;
 | 
				
			||||||
use actix_web::{HttpResponse, web};
 | 
					use actix_web::{HttpResponse, web};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(serde::Deserialize)]
 | 
					 | 
				
			||||||
pub struct CreateAccountRequest {
 | 
					 | 
				
			||||||
    name: String,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/// Create a new account
 | 
					/// Create a new account
 | 
				
			||||||
pub async fn create(auth: AuthExtractor, req: web::Json<CreateAccountRequest>) -> HttpResult {
 | 
					pub async fn create(auth: AuthExtractor, req: web::Json<UpdateAccountQuery>) -> HttpResult {
 | 
				
			||||||
    let constraints = ServerConstraints::default();
 | 
					    if let Some(err) = req.check_error() {
 | 
				
			||||||
 | 
					        return Ok(HttpResponse::BadRequest().json(err));
 | 
				
			||||||
    if !constraints.account_name.check_str(&req.name) {
 | 
					 | 
				
			||||||
        return Ok(HttpResponse::BadRequest().json("Invalid account name length!"));
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    accounts_service::create(
 | 
					    accounts_service::create(auth.user_id(), &req).await?;
 | 
				
			||||||
        auth.user_id(),
 | 
					 | 
				
			||||||
        &UpdateAccountQuery {
 | 
					 | 
				
			||||||
            name: req.name.clone(),
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    .await?;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(HttpResponse::Created().finish())
 | 
					    Ok(HttpResponse::Created().finish())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -40,6 +26,17 @@ pub async fn get_single(account: AccountInPath) -> HttpResult {
 | 
				
			|||||||
    Ok(HttpResponse::Ok().json(account.as_ref()))
 | 
					    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
 | 
					/// Delete an account
 | 
				
			||||||
pub async fn delete(account: AccountInPath) -> HttpResult {
 | 
					pub async fn delete(account: AccountInPath) -> HttpResult {
 | 
				
			||||||
    accounts_service::delete(account.as_ref().id()).await?;
 | 
					    accounts_service::delete(account.as_ref().id()).await?;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -107,7 +107,10 @@ async fn main() -> std::io::Result<()> {
 | 
				
			|||||||
                "/api/account/{account_id}",
 | 
					                "/api/account/{account_id}",
 | 
				
			||||||
                web::get().to(accounts_controller::get_single),
 | 
					                web::get().to(accounts_controller::get_single),
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            // TODO : update account
 | 
					            .route(
 | 
				
			||||||
 | 
					                "/api/account/{account_id}",
 | 
				
			||||||
 | 
					                web::put().to(accounts_controller::update),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
            .route(
 | 
					            .route(
 | 
				
			||||||
                "/api/account/{account_id}",
 | 
					                "/api/account/{account_id}",
 | 
				
			||||||
                web::delete().to(accounts_controller::delete),
 | 
					                web::delete().to(accounts_controller::delete),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
use crate::connections::db_connection::db;
 | 
					use crate::connections::db_connection::db;
 | 
				
			||||||
 | 
					use crate::controllers::server_controller::ServerConstraints;
 | 
				
			||||||
use crate::models::accounts::{Account, AccountID, NewAccount};
 | 
					use crate::models::accounts::{Account, AccountID, NewAccount};
 | 
				
			||||||
use crate::models::users::UserID;
 | 
					use crate::models::users::UserID;
 | 
				
			||||||
use crate::schema::accounts;
 | 
					use crate::schema::accounts;
 | 
				
			||||||
@@ -11,6 +12,18 @@ pub struct UpdateAccountQuery {
 | 
				
			|||||||
    pub name: String,
 | 
					    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
 | 
					/// Create a new account
 | 
				
			||||||
pub async fn create(user_id: UserID, query: &UpdateAccountQuery) -> anyhow::Result<Account> {
 | 
					pub async fn create(user_id: UserID, query: &UpdateAccountQuery) -> anyhow::Result<Account> {
 | 
				
			||||||
    let new_account = NewAccount {
 | 
					    let new_account = NewAccount {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user