add links

This commit is contained in:
Romulus21
2024-11-06 15:55:35 +01:00
parent bd48522b66
commit c8c35bde09
6 changed files with 148 additions and 13 deletions

View File

@@ -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::mysql::MySqlQueryResult, sqlx::Error> {
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<Vec<Self>, 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
}
}

View File

@@ -1,4 +1,5 @@
mod link;
mod value;
pub use link::Link;
pub use value::Value;
pub use value::OPTIONS;