diff options
| author | Joe Carstairs <me@joeac.net> | 2024-12-12 08:28:04 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-12-12 08:28:04 +0000 |
| commit | 54894f9de9bad4fecb2a116d59a03cdd003f84c0 (patch) | |
| tree | 6ce5517cecf23d4e93d7a381a374e1fa6c810cee /schist_backend/src/routes/account.rs | |
| parent | 8a30bcbdf9264d235bf93da3df8e170cfde53d02 (diff) | |
Moves rust workspace to root
Diffstat (limited to 'schist_backend/src/routes/account.rs')
| -rw-r--r-- | schist_backend/src/routes/account.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/schist_backend/src/routes/account.rs b/schist_backend/src/routes/account.rs new file mode 100644 index 0000000..83d4d1b --- /dev/null +++ b/schist_backend/src/routes/account.rs @@ -0,0 +1,48 @@ +use schist_models::account::Account; +use schist_queries::accounts::{get_all_accounts as get_all_accounts_query, insert_accounts}; +use rocket::http::Status; +use rocket::serde::json::Json; +use rocket::{get, post, Responder}; + +use crate::databases::UserDataDb; + +#[get("/account")] +pub async fn get_all_accounts(user_data_db: UserDataDb) -> Result<Json<Vec<Account>>, (Status, String)> { + user_data_db + .run(get_all_accounts_query) + .await + .map(Json) + .map_err(|err| (Status::InternalServerError, err.to_string())) +} + +#[derive(Responder)] +pub enum CreateAccountResponder { + #[response(status = 500, content_type = "text")] + InternalServerError(String), + + #[response(status = 202, content_type = "json")] + Created(Json<Account>), + + #[response(status = 400, content_type = "text")] + BadRequest(String), +} + +#[post("/account", data = "<account>", format = "json")] +pub async fn create_account( + account: Json<Account>, + user_data_db: UserDataDb, +) -> CreateAccountResponder { + let account = account.into_inner(); + let accounts_vec = vec![account.clone()]; + + let num_rows_inserted = user_data_db + .run(move |connection| insert_accounts(&accounts_vec, connection)) + .await; + + match num_rows_inserted { + Ok(_) => CreateAccountResponder::Created(Json(account)), + Err(err) => CreateAccountResponder::InternalServerError(format!( + "Error creating account: {err}" + )), + } +} |
