* Simplified badge structure
+ Adding driver code for backend update methods This is basically all the dblib code required for the differing /badge/update/* api handlers to use
This commit is contained in:
parent
e754b18687
commit
1884580bf8
@ -50,24 +50,34 @@ async fn get(pool: &Pool, id: u64) -> SqlResult<Option<Badge>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(pool: &Pool, badge: Badge) -> SqlResult<Option<Badge>> {
|
async fn badge_update(pool: &Pool, id: u64, query: &'static str, values: mysql_async::Params) -> SqlResult<bool> {
|
||||||
|
if let Some(_) = get(pool, id).await? {
|
||||||
|
let mut conn = pool.get_conn().await?;
|
||||||
|
conn.exec_drop(query, values).await?;
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_perms(pool: &Pool, id: u64, perms: u64) -> SqlResult<bool> {
|
||||||
// Ok(None) if that badge was not registered anywhere
|
// Ok(None) if that badge was not registered anywhere
|
||||||
// Ok(Some(Badge)) if that badge was registered
|
// Ok(Some(Badge)) if that badge was registered
|
||||||
if let Ok(None) = get(pool, badge.id).await {
|
let q = "UPDATE perms SET permissions = :perms WHERE id = :id";
|
||||||
return Ok(None);
|
let p = params!{"id" => id, "perms" => perms};
|
||||||
}
|
return badge_update(pool, id, q, p).await;
|
||||||
|
}
|
||||||
let q = "UPDATE badges SET name = :name, color = :color, permissions = :perms
|
|
||||||
WHERE id = :id";
|
pub async fn update_name(pool: &Pool, id: u64, name: &str) -> SqlResult<bool> {
|
||||||
let p = params! {
|
let q = "UPDATE badges SET name = :name WHERE id = :id";
|
||||||
"id" => badge.id,
|
let p = params!{"id" => id, "name" => name};
|
||||||
"name" => badge.name.clone(),
|
return badge_update(pool, id, q, p).await;
|
||||||
"color" => badge.color,
|
}
|
||||||
"perms" => badge.perms
|
|
||||||
};
|
pub async fn update_color(pool: &Pool, id: u64, color: u32) -> SqlResult<bool> {
|
||||||
let mut conn = pool.get_conn().await?;
|
let q = "UPDATE badges SET color = :color WHERE id = :id";
|
||||||
conn.exec_drop(q, p).await?;
|
let p = params!{"id" => id, "color" => color};
|
||||||
Ok(Some(badge))
|
return badge_update(pool, id, q, p).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list(pool: &Pool) -> SqlResult<Vec<Badge>> {
|
pub async fn list(pool: &Pool) -> SqlResult<Vec<Badge>> {
|
||||||
|
@ -6,6 +6,7 @@ pub mod invites;
|
|||||||
pub mod channels;
|
pub mod channels;
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod neighbors;
|
pub mod neighbors;
|
||||||
|
pub mod badges;
|
||||||
pub mod jwt;
|
pub mod jwt;
|
||||||
|
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
@ -60,7 +61,7 @@ pub struct UserMessage {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct Channel {
|
pub struct Channel {
|
||||||
pub id: UBigInt,
|
pub id: UBigInt,
|
||||||
pub name: VarChar,
|
pub name: VarChar,
|
||||||
@ -107,7 +108,7 @@ pub struct Neighbor {
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Badge {
|
pub struct Badge {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub id: u32,
|
pub id: u64,
|
||||||
pub color: u32,
|
pub color: u32,
|
||||||
pub perms: Option<u32>, // abridged version of permissiosn
|
pub perms: u64,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user