add move link

This commit is contained in:
Romulus21
2024-11-27 23:03:54 +01:00
parent f67c0e2e57
commit 319e901dd8
7 changed files with 969 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
id: u64,
pub id: u64,
pub link: String,
pub name: String,
position: i64,
@@ -38,4 +38,54 @@ impl Link {
.fetch_all(crate::database::get_db())
.await
}
#[cfg(feature = "ssr")]
pub async fn move_position(
link_id: String,
direction: String,
) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error> {
let link = sqlx::query!(
"SELECT id, name, link, position, created_at FROM links WHERE id = ?",
link_id
)
.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_one(crate::database::get_db())
.await;
match link {
Ok(link) => {
let position = link.position;
let flow = match direction.as_str() {
"prev" => -1,
"next" => 1,
_ => 0,
};
let _ = sqlx::query!(
"UPDATE links SET position = ? WHERE position = ?",
position - flow,
position + flow
)
.execute(crate::database::get_db())
.await;
sqlx::query!(
"UPDATE links SET position = ? WHERE id = ?",
position + flow,
link_id
)
.execute(crate::database::get_db())
.await
}
Err(_) => {
print!("Error position");
panic!("not fount")
}
}
}
}