add links
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod link;
|
||||
mod value;
|
||||
pub use link::Link;
|
||||
pub use value::Value;
|
||||
pub use value::OPTIONS;
|
||||
|
||||
Reference in New Issue
Block a user