diff options
| author | Joe Carstairs <me@joeac.net> | 2024-09-17 11:28:56 +0100 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-09-17 11:28:56 +0100 |
| commit | f0e3e0cfb183253e03b1a61ddecb7b9330d47fdb (patch) | |
| tree | 722eca369743dcfbb95f1efd58cc4cecc0fb8677 /backend/src/routes/account.rs | |
| parent | c737b6df94eae4981bb03dda3583e24046e24865 (diff) | |
Refactors backend
Diffstat (limited to 'backend/src/routes/account.rs')
| -rw-r--r-- | backend/src/routes/account.rs | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/backend/src/routes/account.rs b/backend/src/routes/account.rs index 418ec96..7bbdb4f 100644 --- a/backend/src/routes/account.rs +++ b/backend/src/routes/account.rs @@ -1,24 +1,19 @@ -use diesel::prelude::*; use diesel::result::DatabaseErrorKind; +use rocket::http::Status; use rocket::serde::json::Json; use rocket::{get, post, Responder}; use crate::databases::UserDataDb; use crate::models::account::Account; -use crate::schema::accounts::dsl::accounts; +use crate::queries::accounts::{get_all_accounts as get_all_accounts_query, insert_account}; #[get("/account")] -pub async fn get_all_accounts(user_data_db: UserDataDb) -> Json<Vec<Account>> { +pub async fn get_all_accounts(user_data_db: UserDataDb) -> Result<Json<Vec<Account>>, Status> { user_data_db - .run(|connection| { - Json( - accounts - .select(Account::as_select()) - .load(connection) - .expect("Error getting accounts"), - ) - }) + .run(get_all_accounts_query) .await + .map(Json) + .map_err(|_| Status { code: 500 }) } #[derive(Responder)] @@ -39,14 +34,10 @@ pub async fn create_account( user_data_db: UserDataDb, ) -> CreateAccountResponder { let account = account.into_inner(); - let account_clone = account.clone(); + let accounts_vec = vec![account.clone()]; let num_rows_inserted = user_data_db - .run(|connection| { - diesel::insert_into(accounts) - .values(vec![account_clone]) - .execute(connection) - }) + .run(move |connection| insert_account(&accounts_vec, connection)) .await; match num_rows_inserted { |
