From 33cbfeadc57c38d4feab7db5811a7e88b08454b1 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Sun, 12 Apr 2026 15:05:19 +0200 Subject: [PATCH] add alternative link --- ...00000_add_alternate_link_to_links.down.sql | 1 + ...2000000_add_alternate_link_to_links.up.sql | 1 + src/models/link.rs | 41 +++++++++------ src/routes/link.rs | 51 +++++++++++++++---- 4 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 migrations/20260412000000_add_alternate_link_to_links.down.sql create mode 100644 migrations/20260412000000_add_alternate_link_to_links.up.sql diff --git a/migrations/20260412000000_add_alternate_link_to_links.down.sql b/migrations/20260412000000_add_alternate_link_to_links.down.sql new file mode 100644 index 0000000..14f66b3 --- /dev/null +++ b/migrations/20260412000000_add_alternate_link_to_links.down.sql @@ -0,0 +1 @@ +ALTER TABLE links DROP COLUMN alternate_link; diff --git a/migrations/20260412000000_add_alternate_link_to_links.up.sql b/migrations/20260412000000_add_alternate_link_to_links.up.sql new file mode 100644 index 0000000..18e42e0 --- /dev/null +++ b/migrations/20260412000000_add_alternate_link_to_links.up.sql @@ -0,0 +1 @@ +ALTER TABLE links ADD COLUMN alternate_link TEXT NOT NULL DEFAULT ''; diff --git a/src/models/link.rs b/src/models/link.rs index 452c19f..d3d6825 100644 --- a/src/models/link.rs +++ b/src/models/link.rs @@ -6,6 +6,7 @@ pub struct Link { pub link: String, pub name: String, pub icon: String, + pub alternate_link: String, position: i64, created_at: String, } @@ -16,12 +17,14 @@ impl Link { name: String, link: String, icon: String, + alternate_link: String, ) -> Result { sqlx::query!( - "INSERT INTO links (name, link, icon, position, created_at) VALUES (?, ?, ?, (SELECT COALESCE(MAX(position) + 1, 1) FROM links lin), ?)", + "INSERT INTO links (name, link, icon, alternate_link, position, created_at) VALUES (?, ?, ?, ?, (SELECT COALESCE(MAX(position) + 1, 1) FROM links lin), ?)", name, link, icon, + alternate_link, chrono::Local::now().naive_local(), ) .execute(crate::database::get_db()) @@ -30,17 +33,20 @@ impl Link { #[cfg(feature = "ssr")] pub async fn get_all() -> Result, sqlx::Error> { - sqlx::query!("SELECT id, name, link, icon, position, created_at FROM links ORDER BY position") - .map(|x| Self { - id: x.id, - name: x.name, - link: x.link, - icon: x.icon, - position: x.position, - created_at: x.created_at.format("%d/%m/%Y %H:%M").to_string(), - }) - .fetch_all(crate::database::get_db()) - .await + sqlx::query!( + "SELECT id, name, link, icon, alternate_link, position, created_at FROM links ORDER BY position" + ) + .map(|x| Self { + id: x.id, + name: x.name, + link: x.link, + icon: x.icon, + alternate_link: x.alternate_link, + position: x.position, + created_at: x.created_at.format("%d/%m/%Y %H:%M").to_string(), + }) + .fetch_all(crate::database::get_db()) + .await } #[cfg(feature = "ssr")] @@ -49,7 +55,7 @@ impl Link { direction: String, ) -> Result { let link = sqlx::query!( - "SELECT id, name, link, icon, position, created_at FROM links WHERE id = ?", + "SELECT id, name, link, icon, alternate_link, position, created_at FROM links WHERE id = ?", link_id ) .map(|x| Self { @@ -57,6 +63,7 @@ impl Link { name: x.name, link: x.link, icon: x.icon, + alternate_link: x.alternate_link, position: x.position, created_at: x.created_at.format("%d/%m/%Y %H:%M").to_string(), }) @@ -100,16 +107,18 @@ impl Link { name: String, link: String, icon: String, + alternate_link: String, ) -> Result { sqlx::query!( - "UPDATE links SET name = ?, link = ?, icon = ? WHERE id = ?", + "UPDATE links SET name = ?, link = ?, icon = ?, alternate_link = ? WHERE id = ?", name, link, icon, + alternate_link, id ) - .execute(crate::database::get_db()) - .await + .execute(crate::database::get_db()) + .await } #[cfg(feature = "ssr")] diff --git a/src/routes/link.rs b/src/routes/link.rs index 4b7defc..bab111c 100644 --- a/src/routes/link.rs +++ b/src/routes/link.rs @@ -13,8 +13,8 @@ pub async fn get_links() -> Result, ServerFnError> { } #[server(LinkAction, "/api")] -pub async fn add_value(name: String, link: String, icon: String) -> Result<(), ServerFnError> { - crate::models::Link::insert(name, link, icon) +pub async fn add_value(name: String, link: String, icon: String, alternate_link: String) -> Result<(), ServerFnError> { + crate::models::Link::insert(name, link, icon, alternate_link) .await .map(|_| ()) .map_err(|x| { @@ -29,6 +29,7 @@ pub fn Links() -> impl IntoView { let (show_form, set_show_form) = create_signal(false); let (edit_link, set_edit_link) = create_signal::>(None); let (edit, set_edit) = create_signal(false); + let (use_alternate, set_use_alternate) = create_signal(false); let link_action = create_server_action::(); let update_action = create_server_action::(); @@ -59,7 +60,7 @@ pub fn Links() -> impl IntoView { children=move |(_, link)| { let link = create_rw_signal(link); view!{ - + } }/> } @@ -69,6 +70,13 @@ pub fn Links() -> impl IntoView {
+
} -} \ No newline at end of file +}