From 42d88cc2dd9510a16cf3cb6c53894de499b9d9af Mon Sep 17 00:00:00 2001 From: Philippe Loctaux Date: Thu, 30 Mar 2023 00:53:57 +0200 Subject: [PATCH] apps: get all with filter, serde --- Cargo.lock | 1 + crates/apps/Cargo.toml | 1 + crates/apps/src/database.rs | 11 ++ crates/apps/src/lib.rs | 3 +- crates/database/queries/apps/get_all.sql | 11 ++ .../database/queries/apps/get_all_active.sql | 12 ++ .../queries/apps/get_all_archived.sql | 12 ++ crates/database/sqlx-data.json | 180 ++++++++++++++++++ crates/database/src/tables/apps.rs | 29 +++ 9 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 crates/database/queries/apps/get_all.sql create mode 100644 crates/database/queries/apps/get_all_active.sql create mode 100644 crates/database/queries/apps/get_all_archived.sql diff --git a/Cargo.lock b/Cargo.lock index 9793b6b..860b192 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,7 @@ dependencies = [ "hash", "id", "openid", + "serde", "thiserror", "url", ] diff --git a/crates/apps/Cargo.toml b/crates/apps/Cargo.toml index 2c93719..8ce9b68 100644 --- a/crates/apps/Cargo.toml +++ b/crates/apps/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" thiserror = { workspace = true } chrono = { workspace = true } url = { workspace = true } +serde = { workspace = true } # local crates id = { path = "../id" } diff --git a/crates/apps/src/database.rs b/crates/apps/src/database.rs index a832d12..7e76697 100644 --- a/crates/apps/src/database.rs +++ b/crates/apps/src/database.rs @@ -22,6 +22,17 @@ impl From for App { } impl App { + pub async fn get_all( + conn: impl SqliteExecutor<'_>, + filter_get_archived: Option, + ) -> Result, Error> { + Ok(DatabaseApps::get_all(conn, filter_get_archived) + .await? + .into_iter() + .map(Self::from) + .collect::>()) + } + pub async fn insert( conn: impl SqliteExecutor<'_>, id: &AppID, diff --git a/crates/apps/src/lib.rs b/crates/apps/src/lib.rs index 0d06332..b4d4278 100644 --- a/crates/apps/src/lib.rs +++ b/crates/apps/src/lib.rs @@ -4,11 +4,12 @@ mod get_valid; use chrono::{DateTime, Utc}; use id::AppID; +use serde::Serialize; pub use crate::error::Error; pub use get_valid::Error as GetValidError; -#[derive(Debug)] +#[derive(Serialize, Debug, Clone)] pub struct App { id: AppID, created_at: DateTime, diff --git a/crates/database/queries/apps/get_all.sql b/crates/database/queries/apps/get_all.sql new file mode 100644 index 0000000..ce62abe --- /dev/null +++ b/crates/database/queries/apps/get_all.sql @@ -0,0 +1,11 @@ +select id, + created_at as "created_at: DateTime", + updated_at as "updated_at: DateTime", + label, + redirect_uri, + secret, + is_confidential as "is_confidential: bool", + is_archived as "is_archived: bool" +from apps + +order by created_at desc diff --git a/crates/database/queries/apps/get_all_active.sql b/crates/database/queries/apps/get_all_active.sql new file mode 100644 index 0000000..7d07786 --- /dev/null +++ b/crates/database/queries/apps/get_all_active.sql @@ -0,0 +1,12 @@ +select id, + created_at as "created_at: DateTime", + updated_at as "updated_at: DateTime", + label, + redirect_uri, + secret, + is_confidential as "is_confidential: bool", + is_archived as "is_archived: bool" +from apps + +where is_archived is 0 +order by created_at desc \ No newline at end of file diff --git a/crates/database/queries/apps/get_all_archived.sql b/crates/database/queries/apps/get_all_archived.sql new file mode 100644 index 0000000..340c1ee --- /dev/null +++ b/crates/database/queries/apps/get_all_archived.sql @@ -0,0 +1,12 @@ +select id, + created_at as "created_at: DateTime", + updated_at as "updated_at: DateTime", + label, + redirect_uri, + secret, + is_confidential as "is_confidential: bool", + is_archived as "is_archived: bool" +from apps + +where is_archived is 1 +order by created_at desc \ No newline at end of file diff --git a/crates/database/sqlx-data.json b/crates/database/sqlx-data.json index 40bc2bc..d2c3deb 100644 --- a/crates/database/sqlx-data.json +++ b/crates/database/sqlx-data.json @@ -20,6 +20,66 @@ }, "query": "update settings\n\nset business_name = ?\n\nwhere id is 0\n" }, + "13c26aaf556d259535afb95f5c53b4c594f373084c73000bc7865a280df74fdd": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "created_at: DateTime", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "updated_at: DateTime", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "label", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "redirect_uri", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "secret", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "is_confidential: bool", + "ordinal": 6, + "type_info": "Int64" + }, + { + "name": "is_archived: bool", + "ordinal": 7, + "type_info": "Int64" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Right": 0 + } + }, + "query": "select id,\n created_at as \"created_at: DateTime\",\n updated_at as \"updated_at: DateTime\",\n label,\n redirect_uri,\n secret,\n is_confidential as \"is_confidential: bool\",\n is_archived as \"is_archived: bool\"\nfrom apps\n\nwhere is_archived is 0\norder by created_at desc" + }, "3c8e31ffa5cbfd4dded8a272777cb320fb51fd2e53ed25054d24e9801df0c358": { "describe": { "columns": [], @@ -728,6 +788,66 @@ }, "query": "select id,\n created_at as \"created_at: DateTime\",\n revoked_at as \"revoked_at: DateTime\",\n private_der,\n public_der\n\nfrom keys\nwhere revoked_at is null\norder by created_at desc\n" }, + "d8517e5faa5292da25d21a85fcb2f676dce0f67275466566d5bbedf0f4b7f4f5": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "created_at: DateTime", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "updated_at: DateTime", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "label", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "redirect_uri", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "secret", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "is_confidential: bool", + "ordinal": 6, + "type_info": "Int64" + }, + { + "name": "is_archived: bool", + "ordinal": 7, + "type_info": "Int64" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Right": 0 + } + }, + "query": "select id,\n created_at as \"created_at: DateTime\",\n updated_at as \"updated_at: DateTime\",\n label,\n redirect_uri,\n secret,\n is_confidential as \"is_confidential: bool\",\n is_archived as \"is_archived: bool\"\nfrom apps\n\nwhere is_archived is 1\norder by created_at desc" + }, "e22ba816faac0c17ca9f2c31fd1b4a5f13a09cece9ec78e0b6e018950c91facb": { "describe": { "columns": [ @@ -1093,5 +1213,65 @@ } }, "query": "select u.id,\n u.created_at as \"created_at: DateTime\",\n u.updated_at as \"updated_at: DateTime\",\n u.is_admin as \"is_admin: bool\",\n u.username,\n u.name,\n u.email,\n u.password,\n u.password_recover,\n u.paper_key,\n u.is_archived as \"is_archived: bool\"\nfrom users u\n\n inner join authorization_codes ac on u.id = ac.user\n\nwhere ac.code is ?" + }, + "fb35faa6eb7349f783d0053509225216693532c7233a3bf61674b64c2fb3dad7": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "created_at: DateTime", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "updated_at: DateTime", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "label", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "redirect_uri", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "secret", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "is_confidential: bool", + "ordinal": 6, + "type_info": "Int64" + }, + { + "name": "is_archived: bool", + "ordinal": 7, + "type_info": "Int64" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Right": 0 + } + }, + "query": "select id,\n created_at as \"created_at: DateTime\",\n updated_at as \"updated_at: DateTime\",\n label,\n redirect_uri,\n secret,\n is_confidential as \"is_confidential: bool\",\n is_archived as \"is_archived: bool\"\nfrom apps\n\norder by created_at desc\n" } } \ No newline at end of file diff --git a/crates/database/src/tables/apps.rs b/crates/database/src/tables/apps.rs index a76ac5b..4e461d2 100644 --- a/crates/database/src/tables/apps.rs +++ b/crates/database/src/tables/apps.rs @@ -16,6 +16,35 @@ pub struct Apps { } impl Apps { + pub async fn get_all( + conn: impl SqliteExecutor<'_>, + filter_get_archived: Option, + ) -> Result, Error> { + match filter_get_archived { + Some(true) => { + // Get all archived apps + sqlx::query_file_as!(Self, "queries/apps/get_all_archived.sql") + .fetch_all(conn) + .await + .map_err(handle_error) + } + Some(false) => { + // Get all active apps + sqlx::query_file_as!(Self, "queries/apps/get_all_active.sql") + .fetch_all(conn) + .await + .map_err(handle_error) + } + None => { + // Get all apps + sqlx::query_file_as!(Self, "queries/apps/get_all.sql") + .fetch_all(conn) + .await + .map_err(handle_error) + } + } + } + pub async fn insert( conn: impl SqliteExecutor<'_>, id: &str,