forked from groche97/mini_admin_bot
209 lines
7.3 KiB
Rust
209 lines
7.3 KiB
Rust
use sqlite::{State, Error};
|
|
use crate::database;
|
|
|
|
#[cfg(test)]
|
|
fn drop_all(con: &sqlite::Connection) -> bool {
|
|
let mut result: bool;
|
|
match con
|
|
.execute(
|
|
"
|
|
DROP TABLE IF EXISTS poles;
|
|
",
|
|
)
|
|
{
|
|
Ok(_a) => result = true,
|
|
Err(e) => {log::error!("error drop pole {}",e);result = false},
|
|
};
|
|
|
|
match con
|
|
.execute(
|
|
"
|
|
DROP TABLE IF EXISTS last;
|
|
",
|
|
)
|
|
{
|
|
Ok(_a) => result && true,
|
|
Err(e) => {log::error!("error drop last {}",e);result && false},
|
|
}
|
|
}
|
|
|
|
fn create_databases(con: &sqlite::Connection) -> bool {
|
|
return create_poles_database(con) && create_last_database(con)
|
|
}
|
|
|
|
fn create_poles_database(con: &sqlite::Connection) -> bool {
|
|
match con
|
|
.execute(
|
|
"
|
|
CREATE TABLE IF NOT EXISTS poles (npole INTEGER, user TEXT, user_name TEXT, id_group TEXT, id INTEGER PRIMARY KEY AUTOINCREMENT);
|
|
",
|
|
)
|
|
{
|
|
Ok(_a) => return true,
|
|
Err(e) => {log::error!("error pole {}",e);return false},
|
|
}
|
|
}
|
|
|
|
fn create_last_database(con: &sqlite::Connection) -> bool {
|
|
match con
|
|
.execute(
|
|
"
|
|
CREATE TABLE IF NOT EXISTS last (date TEXT, id_group TEXT, user TEXT, points INTEGER, id INTEGER PRIMARY KEY AUTOINCREMENT);
|
|
",
|
|
)
|
|
{
|
|
Ok(_a) => return true,
|
|
Err(e) => {log::error!("error last {}",e);return false},
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub struct DatabasePole {
|
|
ins_connection: &'static database::DB_CONNECTION,//Mutex<Box<sqlite::Connection>>,
|
|
}
|
|
|
|
impl<'a> DatabasePole{
|
|
#[cfg(not(test))]
|
|
pub fn get_database()->Self{
|
|
let ret = Self{
|
|
ins_connection: &database::DB_CONNECTION,
|
|
};
|
|
let raw = &**ret.ins_connection.lock().unwrap();
|
|
create_databases(raw);
|
|
//log::info!("create databases {}",ret.createDatabases());
|
|
ret
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub fn get_database()->Self{
|
|
let ret = Self{
|
|
ins_connection: &DB_CONNECTION,
|
|
};
|
|
let raw = &**ret.ins_connection.lock().unwrap();
|
|
drop_all(raw);
|
|
create_databases(raw);
|
|
//log::info!("create databases {}",ret.createDatabases());
|
|
ret
|
|
}
|
|
|
|
pub fn last_pole(&self, group_id : &str) -> Result<String, Error>{
|
|
let builder = self.ins_connection.lock().unwrap();
|
|
let mut statement = builder
|
|
.prepare("SELECT date FROM last WHERE id_group = ? order by date desc")
|
|
.unwrap();
|
|
statement.bind((1, group_id)).unwrap();
|
|
statement.next().unwrap();
|
|
statement.read::<String, _>(0)
|
|
}
|
|
|
|
pub fn write_points(&self, group_id : &str, user_id: &str, user_name: &str, date: &str, points: i64){
|
|
let builder = self.ins_connection.lock().unwrap();
|
|
let mut statement = builder
|
|
.prepare("SELECT npole FROM poles WHERE user = ? AND id_group = ?")
|
|
.unwrap();
|
|
statement.bind((1, user_id)).unwrap();
|
|
statement.bind((2, group_id)).unwrap();
|
|
|
|
match statement.next().unwrap() {
|
|
State::Row => {
|
|
let mut statement2 = builder
|
|
.prepare("UPDATE poles SET npole=? WHERE user=? AND id_group=?")
|
|
.unwrap();
|
|
statement2.bind((1, statement.read::<i64, _>(0).unwrap()+points)).unwrap();
|
|
statement2.bind((2, user_id)).unwrap();
|
|
statement2.bind((3, group_id)).unwrap();
|
|
statement2.next().unwrap();
|
|
}
|
|
State::Done => {
|
|
let mut statement2 = builder
|
|
.prepare("INSERT INTO poles (npole, user, user_name, id_group) VALUES (?,?,?,?)")
|
|
.unwrap();
|
|
statement2.bind((1, points)).unwrap();
|
|
statement2.bind((2, user_id)).unwrap();
|
|
statement2.bind((3, user_name)).unwrap();
|
|
statement2.bind((4, group_id)).unwrap();
|
|
statement2.next().unwrap();
|
|
}
|
|
|
|
};
|
|
|
|
let mut statement3 = builder
|
|
.prepare("INSERT INTO last (date, id_group, user, points) VALUES (?,?,?,?)")
|
|
.unwrap();
|
|
statement3.bind((1, date)).unwrap();
|
|
statement3.bind((2, group_id)).unwrap();
|
|
statement3.bind((3, user_id)).unwrap();
|
|
statement3.bind((4, points)).unwrap();
|
|
statement3.next().unwrap();
|
|
//while let State::Row = statement3.next().unwrap() {
|
|
// log::info!("name = {}", statement.read::<String>(0).unwrap());
|
|
//}
|
|
}
|
|
|
|
pub fn check_user_pole(&self, group_id : &str, user_id: &str, date: &str) -> i64{
|
|
let builder = self.ins_connection.lock().unwrap();
|
|
let mut statement = builder
|
|
.prepare("SELECT points FROM last WHERE id_group = ? AND user = ? AND date = :date")
|
|
.unwrap();
|
|
statement.bind((1, group_id)).unwrap();
|
|
statement.bind((2, user_id)).unwrap();
|
|
statement.bind((3, date)).unwrap();
|
|
match statement.next().unwrap() {
|
|
State::Row => statement.read::<i64, _>(0).unwrap(),
|
|
State::Done => 0,
|
|
}
|
|
}
|
|
|
|
pub fn check_group_points(&self, group_id : &str, date: &str) -> i64{
|
|
let builder = self.ins_connection.lock().unwrap();
|
|
let mut statement = builder
|
|
.prepare("SELECT points FROM last WHERE id_group = ? AND date = ?")
|
|
.unwrap();
|
|
statement.bind((1, group_id)).unwrap();
|
|
statement.bind((2, date)).unwrap();
|
|
match statement.next().unwrap() {
|
|
State::Row => statement.read::<i64, _>(0).unwrap(),
|
|
State::Done => 0,
|
|
}
|
|
}
|
|
|
|
pub fn get_top_users(&self, group_id : &str) -> Vec<(i64,String)>{
|
|
let builder = self.ins_connection.lock().unwrap();
|
|
let mut statement = builder
|
|
.prepare("SELECT npole, user_name FROM poles where id_group=? order by npole desc limit 10")
|
|
.unwrap();
|
|
statement.bind((1, group_id)).unwrap();
|
|
let mut ret = Vec::new();
|
|
for i in 1 .. 10 {
|
|
match statement.next().unwrap() {
|
|
State::Row => ret.push((
|
|
statement.read::<i64, _>(0).unwrap(),
|
|
statement.read::<String, _>(1).unwrap()
|
|
)),
|
|
State::Done => return ret,
|
|
}
|
|
}
|
|
ret
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[test]
|
|
fn write_points(){
|
|
let data = DatabasePole::get_database();
|
|
let expected = 3;
|
|
data.write_points("000", "000", "2020-01-01", expected);
|
|
assert_eq!(expected, data.check_user_pole("000", "000", "2020-01-01"));
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[test]
|
|
fn last_pole(){
|
|
let data = DatabasePole::get_database();
|
|
let group_id = "000";
|
|
data.write_points(group_id, "000", "2020-01-02", 3);
|
|
assert_eq!("2020-01-02", data.last_pole(group_id).unwrap());
|
|
}
|