Files
Projet_48h/backend/user.rs
2026-03-31 16:01:59 +02:00

70 lines
2.1 KiB
Rust

use sqlite::Connection;
// use sodiumoxide::crypto::secretbox;
pub struct User {
name: String,
level: u32,
// password: [u8],
}
impl User {
pub fn open(name: String, level: u32, password: [u8]) -> User {
User {name, level, password}
}
pub fn get_name(&self) -> String{return self.name.clone()}
pub fn get_level(&self) -> u32{return self.level}
// pub fn get_password(&self) -> [u8]{return self.password.clone()}
// pub fn check_password(&self,nonce:secretbox::Nonce,key:secretbox::Key){
// assert!("" == secretbox::open(self.get_password(), &nonce, &key).unwrap()[..]);
// }
}
pub struct DataBase {
connection: Connection,
}
impl DataBase {
pub fn open(name: String) -> DataBase {
let connection = sqlite::open(name + ".db").unwrap();
DataBase { connection }
}
fn execute(&self, arg: String){
match self.connection.execute(arg){
_ => (),
Err(e) => panic!("Error : {}",e),
}
}
pub fn create_table(&self, table: &str, var: &str){
self.execute("CREATE TABLE ".to_owned() + &table.to_string() + "(" + &var.to_string() + ");");
}
pub fn add_column(&self, table: &str, arg: &str){
self.execute("ALTER TABLE ".to_owned() + &table.to_string() + " ADD " + &arg.to_string() + ";");
}
pub fn insert(&self, table: &str, arg: &str){
self.execute("INSERT INTO ".to_owned() + &table.to_string() + " VALUES (" + &arg.to_string() + ")" + ";");
}
pub fn check(&self, table: &str, arg: &str) -> bool {
let mut end = false;
self.connection.iterate("Select name FROM ".to_owned() + &table.to_string() + " WHERE name = " + &format!("'{}'",&arg.to_string()), |pairs| {
for &(name, value) in pairs.iter() {
if name == "name" {
end = true;
}
}
return end;
});
return end;
}
pub fn update(&self, table: &str, arg: &str, where_is: &str, arg2: &str){
self.execute("UPDATE ".to_owned() + &table.to_string() + " SET " + &arg.to_string() + " WHERE " + &where_is.to_string() + " = " + &arg2.to_string());
}
}