Can change default account

This commit is contained in:
Pierre HUBERT 2025-04-07 21:42:35 +02:00
parent 1977b5209c
commit 90bb4db806
3 changed files with 30 additions and 1 deletions

View File

@ -37,6 +37,13 @@ pub async fn update(account: AccountInPath, req: web::Json<UpdateAccountQuery>)
Ok(HttpResponse::Accepted().finish())
}
/// Set an account as the default one
pub async fn set_default(account: AccountInPath) -> HttpResult {
accounts_service::set_default(account.as_ref().user_id(), account.as_ref().id()).await?;
Ok(HttpResponse::Accepted().finish())
}
/// Delete an account
pub async fn delete(account: AccountInPath) -> HttpResult {
accounts_service::delete(account.as_ref().id()).await?;

View File

@ -111,11 +111,14 @@ async fn main() -> std::io::Result<()> {
"/api/account/{account_id}",
web::put().to(accounts_controller::update),
)
.route(
"/api/account/{account_id}/default",
web::put().to(accounts_controller::set_default),
)
.route(
"/api/account/{account_id}",
web::delete().to(accounts_controller::delete),
)
// TODO : set as default
// Static assets
.route("/", web::get().to(static_controller::root_index))
.route(

View File

@ -68,6 +68,25 @@ pub async fn get_list_user(id: UserID) -> anyhow::Result<Vec<Account>> {
.get_results(&mut db()?)?)
}
/// Change the default account of a user
pub async fn set_default(user_id: UserID, account_id: AccountID) -> anyhow::Result<()> {
diesel::update(accounts::dsl::accounts.filter(accounts::dsl::user_id.eq(user_id.0)))
.set((accounts::dsl::default_account.eq(false),))
.execute(&mut db()?)?;
diesel::update(
accounts::dsl::accounts.filter(
accounts::dsl::user_id
.eq(user_id.0)
.and(accounts::dsl::id.eq(account_id.0)),
),
)
.set((accounts::dsl::default_account.eq(true),))
.execute(&mut db()?)?;
Ok(())
}
/// Delete an account
pub async fn delete(id: AccountID) -> anyhow::Result<()> {
diesel::delete(accounts::dsl::accounts.filter(accounts::dsl::id.eq(id.0)))