update user.rs file and integration in main file
This commit is contained in:
63
backend/src/user.rs
Normal file
63
backend/src/user.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use sqlite::Connection;
|
||||
|
||||
pub struct User {
|
||||
name: String,
|
||||
level: u32,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn open(name: String, level: u32) -> User {
|
||||
User {name, level}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> String{return self.name.clone()}
|
||||
pub fn get_level(&self) -> u32{return self.level}
|
||||
}
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user