Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix delete account #11

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sohfin_api"
version = "0.1.8"
version = "0.1.9"
authors = ["Andhika <okumaru07@gmail.com>"]
edition = "2021"
readme = "README.md"
Expand Down
19 changes: 4 additions & 15 deletions src/handlers/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,12 @@ impl<'a> AccountHandler<'a> {
let query_id = req_query_id(self.request);
let delete_acc = self.account_repo.account_delete(query_id).await?;

let account = new_account(
&delete_acc.id,
&delete_acc.name,
&delete_acc.description,
&delete_acc.star,
&delete_acc.r#type,
&delete_acc.balance,
&delete_acc.created_at,
&delete_acc.updated_at,
);

let res = match serde_json::to_string(&account) {
Ok(json) => Response::builder()
let res = match delete_acc {
true => Response::builder()
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(json))
.body(Body::from(delete_acc.to_string()))
.unwrap(),
Err(_) => Response::builder()
false => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(INTERNAL_SERVER_ERROR.into())
.unwrap(),
Expand Down
61 changes: 23 additions & 38 deletions src/repositories/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait AccountTrait {
async fn account_delete(
&mut self,
id: i32,
) -> Result<ExistAccount, Box<dyn std::error::Error + Send + Sync + 'static>>;
) -> Result<bool, Box<dyn std::error::Error + Send + Sync + 'static>>;
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -105,7 +105,11 @@ impl<E: 'static + Executor> AccountTrait for AccountRepo<E> {
&mut self,
account: NewAccount,
) -> Result<ExistAccount, Box<dyn std::error::Error + Send + Sync + 'static>> {
let account = query_add_account(&mut self.db, account).await;

let add = query_add_account(&mut self.db, account).await;
let account_id = i32::try_from(add.last_insert_id()).unwrap();

let account = query_detail_account(&mut self.db, account_id).await;

Ok(account)
}
Expand All @@ -115,18 +119,23 @@ impl<E: 'static + Executor> AccountTrait for AccountRepo<E> {
id: i32,
account: UpdateAccount,
) -> Result<ExistAccount, Box<dyn std::error::Error + Send + Sync + 'static>> {
let account = query_update_account(&mut self.db, id, account).await;

let _ = query_update_account(&mut self.db, id, account).await;

let account = query_detail_account(&mut self.db, id).await;

Ok(account)
}

async fn account_delete(
&mut self,
id: i32,
) -> Result<ExistAccount, Box<dyn std::error::Error + Send + Sync + 'static>> {
let account = query_delete_account(&mut self.db, id).await;
) -> Result<bool, Box<dyn std::error::Error + Send + Sync + 'static>> {

Ok(account)
let res = query_delete_account(&mut self.db, id).await;
let success_delete = res.rows_affected() != 0;

Ok(success_delete)
}
}

Expand Down Expand Up @@ -169,7 +178,7 @@ pub fn query_detail_account<'a>(
pub fn query_add_account<'a>(
db: &'a mut impl Executor,
account: NewAccount,
) -> BoxFuture<'a, ExistAccount> {
) -> BoxFuture<'a, MySqlQueryResult> {
async move {

let account_name = account.name;
Expand All @@ -195,15 +204,7 @@ pub fn query_add_account<'a>(
.await
.unwrap();

let mut query = sqlx::QueryBuilder::new(r#"SELECT * FROM tblaccounts WHERE id = "#);
let accounts = query
.push_bind(add.last_insert_id())
.build_query_as::<ExistAccount>()
.fetch_one(db.as_executor())
.await
.unwrap();

accounts
add
}
.boxed()
}
Expand Down Expand Up @@ -235,7 +236,7 @@ pub fn query_update_account<'a>(
db: &'a mut impl Executor,
id: i32,
account: UpdateAccount,
) -> BoxFuture<'a, ExistAccount> {
) -> BoxFuture<'a, MySqlQueryResult> {
async move {

let mut query = sqlx::QueryBuilder::new(r#"UPDATE tblaccounts SET "#);
Expand Down Expand Up @@ -288,46 +289,30 @@ pub fn query_update_account<'a>(
.push_unseparated(" WHERE id = ")
.push_bind_unseparated(id);

query.build()
let res = query.build()
.execute(db.as_executor())
.await
.unwrap();

let mut query = sqlx::QueryBuilder::new(r#"SELECT * FROM tblaccounts WHERE id = "#);
let accounts = query
.push_bind(id)
.build_query_as::<ExistAccount>()
.fetch_one(db.as_executor())
.await
.unwrap();

accounts
res
}
.boxed()
}

pub fn query_delete_account<'a>(
db: &'a mut impl Executor,
id: i32,
) -> BoxFuture<'a, ExistAccount> {
) -> BoxFuture<'a, MySqlQueryResult> {
async move {

let mut query = sqlx::QueryBuilder::new(r#"SELECT * FROM tblaccounts WHERE id = "#);
let accounts = query
.push_bind(id)
.build_query_as::<ExistAccount>()
.fetch_one(db.as_executor())
.await
.unwrap();

let mut query = sqlx::QueryBuilder::new(r#"DELETE FROM tblaccounts WHERE id = "#);
query.push_bind(id)
let res = query.push_bind(id)
.build()
.execute(db.as_executor())
.await
.unwrap();

accounts
res
}
.boxed()
}