diff --git a/migrations/20241104213550_links.down.sql b/migrations/20241104213550_links.down.sql new file mode 100644 index 0000000..237866e --- /dev/null +++ b/migrations/20241104213550_links.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `links`; diff --git a/migrations/20241104213550_links.up.sql b/migrations/20241104213550_links.up.sql new file mode 100644 index 0000000..25afc87 --- /dev/null +++ b/migrations/20241104213550_links.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS links ( + id BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, + name TEXT NOT NULL, + link TEXT NOT NULL, + position BIGINT NOT NULL DEFAULT 1, + created_at DATETIME NOT NULL +); diff --git a/src/models/link.rs b/src/models/link.rs index 4a02919..13cf7ba 100644 --- a/src/models/link.rs +++ b/src/models/link.rs @@ -2,7 +2,40 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Link { - id: u16, - link: String, - name: String, + id: u64, + pub link: String, + pub name: String, + position: i64, + created_at: String, +} + +impl Link { + #[cfg(feature = "ssr")] + pub async fn insert( + name: String, + link: String, + ) -> Result { + sqlx::query!( + "INSERT INTO links (name, link, position, created_at) VALUES (?, ?, (SELECT COALESCE(MAX(position) + 1, 1) FROM links lin), ?)", + name, + link, + chrono::Local::now().naive_local(), + ) + .execute(crate::database::get_db()) + .await + } + + #[cfg(feature = "ssr")] + pub async fn get_all() -> Result, sqlx::Error> { + sqlx::query!("SELECT id, name, link, position, created_at FROM links ORDER BY position") + .map(|x| Self { + id: x.id, + name: x.name, + link: x.link, + position: x.position, + created_at: x.created_at.format("%d/%m/%Y %H:%M").to_string(), + }) + .fetch_all(crate::database::get_db()) + .await + } } diff --git a/src/models/mod.rs b/src/models/mod.rs index f292281..8bbe6d3 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,4 +1,5 @@ mod link; mod value; +pub use link::Link; pub use value::Value; pub use value::OPTIONS; diff --git a/src/routes/link.rs b/src/routes/link.rs index aab34e2..825451a 100644 --- a/src/routes/link.rs +++ b/src/routes/link.rs @@ -1,23 +1,108 @@ use leptos::*; //use leptos_meta::*; -//use leptos_router::*; +use leptos_router::*; + +#[server(GetLinksAction, "/api", "GetJson")] +#[tracing::instrument] +pub async fn get_links() -> Result, ServerFnError> { + crate::models::Link::get_all().await.map_err(|x| { + let err = format!("Error while posting a link: {x:?}"); + tracing::error!("{err}"); + ServerFnError::ServerError("Could not post a link, try again later".into()) + }) +} + +#[server(LinkAction, "/api")] +pub async fn add_value(name: String, link: String) -> Result<(), ServerFnError> { + crate::models::Link::insert(name, link) + .await + .map(|_| ()) + .map_err(|x| { + let err = format!("Error while posting a comment: {x:?}"); + tracing::error!("{err}"); + ServerFnError::ServerError("Could not post a comment, try again later".into()) + }) +} #[component] pub fn Links() -> impl IntoView { + let (show, set_show) = create_signal(false); + let link_action = create_server_action::(); + let result = link_action.version(); + let reset_form = create_rw_signal(""); + let links = create_resource( + move || (result.get()), + move |_| async move { + reset_form.set(""); + set_show.set(false); + get_links().await + }, + ); + + let form = move || { + show.get().then(|| { + view! {
+

"Ajout d'un lien"

+ +
+ + +
+ +
+ + +
+
+ +
+
+
} + }) + }; + view! {
    - - - + "Loading Comments from the article"

    }> + "Something went wrong."

    } + }> + {move || links.get().map(move |x| x.map(move |c| { + view! { + } + }/> + } + }))} +
    +
+ +
+ + {form} +
} } #[component] -fn Link(link: String, name: String) -> impl IntoView { +fn Link(link: RwSignal) -> impl IntoView { view! {
  • - {name} + + {move || link.with(|x| x.name.to_string())} +
  • } } diff --git a/src/routes/value.rs b/src/routes/value.rs index 17229cc..5daded0 100644 --- a/src/routes/value.rs +++ b/src/routes/value.rs @@ -19,8 +19,14 @@ pub async fn add_value(device: String, value: String) -> Result<(), ServerFnErro #[component] pub fn FormValues() -> impl IntoView { let value_action = create_server_action::(); - //let value = value_action.value(); - //let has_error = move || value.with(|val| matches!(val, Some(Err(_)))); + let result = value_action.version(); + let reset_value = create_rw_signal(""); + let _ = create_resource( + move || (result.get()), + move |_| async move { + reset_value.set(""); + }, + ); view! {
    @@ -37,8 +43,10 @@ pub fn FormValues() -> impl IntoView {
    - +