create some functions for communication with DB
This commit is contained in:
parent
9e238bb8bb
commit
0c7c7b736b
18
src/db/messages.rs
Normal file
18
src/db/messages.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use super::types::Message;
|
||||||
|
use rusqlite::{params, Connection, Result};
|
||||||
|
|
||||||
|
pub fn select_message_by_id(id: u64, conn: &Connection) -> Result<Message> {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_all_user_message(id: u64, conn: &Connection) -> Result<Vec<Message>> {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_n_last_messages(user_id: u64, count: u32, conn: &Connection) -> Result<Vec<Message>> {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_message(message: Message, conn: &Connection) -> Result<()>{
|
||||||
|
unimplemented!();
|
||||||
|
}
|
|
@ -1,59 +1,47 @@
|
||||||
use rusqlite::{params, Connection, Result};
|
use rusqlite::{params, Connection, Result};
|
||||||
pub mod messaging;
|
pub mod types;
|
||||||
|
pub mod users;
|
||||||
|
pub mod messages;
|
||||||
|
|
||||||
#[derive(Debug)]
|
use types::DB_PATH;
|
||||||
pub struct Person {
|
|
||||||
id: i32,
|
|
||||||
name: String,
|
|
||||||
key: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_db(conn: &Connection) -> Result<()> {
|
fn create_db(conn: &Connection) -> Result<()> {
|
||||||
match conn.execute(
|
match conn.execute(
|
||||||
"CREATE TABLE users (
|
"CREATE TABLE users (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT UNIQUE NOT NULL,
|
||||||
key BLOB
|
sign_key BLOB NOT NULL,
|
||||||
|
insert_key BLOB NOT NULL,
|
||||||
|
messages_count INTEGER
|
||||||
)",
|
)",
|
||||||
params![],
|
params![],
|
||||||
) {
|
) {
|
||||||
Ok(_) => {log::info!("USER table created successfully!")},
|
Ok(_) => log::info!("USER table created successfully!"),
|
||||||
Err(e) => log::error!("failed to create USER table {:?}", e),
|
Err(e) => log::error!("failed to create USER table {:?}", e),
|
||||||
|
}
|
||||||
|
match conn.execute(
|
||||||
|
"CREATE TABLE messages (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
user_id TEXT NOT NULL,
|
||||||
|
message TEXT NOT NULL
|
||||||
|
)",
|
||||||
|
params![],
|
||||||
|
) {
|
||||||
|
Ok(_) => log::info!("MESSAGES table created successfully!"),
|
||||||
|
Err(e) => log::error!("failed to create USER table {:?}", e),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_db() -> Result<Connection> {
|
||||||
|
if !std::path::Path::new(DB_PATH).exists() {
|
||||||
|
let conn = Connection::open(DB_PATH)?;
|
||||||
|
println!("{}", conn.is_autocommit());
|
||||||
|
match create_db(&conn) {
|
||||||
|
Ok(_) => {log::info!("Successfully created DB!"); Ok(conn)},
|
||||||
|
Err(e) => {log::error!("Failed to create DB: {:?}",e ); Err(e)},
|
||||||
}
|
}
|
||||||
Ok(())
|
} else {
|
||||||
}
|
Connection::open(DB_PATH)
|
||||||
|
|
||||||
|
|
||||||
pub fn start_db() -> Result<()> {
|
|
||||||
let conn = Connection::open("hole.db")?;
|
|
||||||
println!("{}", conn.is_autocommit());
|
|
||||||
match create_db(&conn) {
|
|
||||||
Ok(_) => log::info!("Successfully created DB!"),
|
|
||||||
Err(e) => log::error!("Failed to create DB: {:?}", e)
|
|
||||||
}
|
}
|
||||||
let me = Person {
|
|
||||||
id: 0,
|
|
||||||
name: "Steven".to_string(),
|
|
||||||
key: "SSK@OolaRmEpOc1q0JF9iypUHZTlNNIqstOnScyb15SUr6k,MgxYrnex5LfvW-pRwMINs~d4nE2mYKjW1AE1U9vIPUM,AQECAAE".to_string(),
|
|
||||||
};
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO users (name, key) VALUES (?1, ?2)",
|
|
||||||
params![me.name, me.key],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let mut stmt = conn.prepare("SELECT id, name, key FROM users")?;
|
|
||||||
let person_iter = stmt.query_map(params![], |row| {
|
|
||||||
Ok(Person {
|
|
||||||
id: row.get(0)?,
|
|
||||||
name: row.get(1)?,
|
|
||||||
key: row.get(2)?,
|
|
||||||
})
|
|
||||||
})?;
|
|
||||||
|
|
||||||
|
|
||||||
for person in person_iter {
|
|
||||||
println!("Found person {:?}", person.unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
25
src/db/types.rs
Normal file
25
src/db/types.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use fcpv2::types::{traits::FcpRequest, SSK};
|
||||||
|
|
||||||
|
|
||||||
|
pub const DB_PATH: &str = "hole.db";
|
||||||
|
|
||||||
|
pub type SignKey = SSK;
|
||||||
|
pub type InsertKey = String;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub sign_key: SignKey,
|
||||||
|
pub insert_key: InsertKey,
|
||||||
|
pub messages_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Message {
|
||||||
|
pub id: u32,
|
||||||
|
pub date: DateTime<Local>,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub message: String,
|
||||||
|
}
|
58
src/db/users.rs
Normal file
58
src/db/users.rs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
use super::types::User;
|
||||||
|
use fcpv2::types::{SSK, traits::FcpParser};
|
||||||
|
|
||||||
|
use rusqlite::{params, Connection, Result, NO_PARAMS};
|
||||||
|
|
||||||
|
pub fn get_user_by_id(id: u32, conn: &Connection) -> Result<User> {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_user_by_name(String: u32, conn: &Connection) -> Result<User> {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_all_users(conn: &Connection) -> Result<Vec<User>> {
|
||||||
|
let mut selected = conn.prepare("SELECT * FROM users")?;
|
||||||
|
log::info!("add user to USERS successfully!");
|
||||||
|
let user_iter = selected.query_map(params![], |row| {
|
||||||
|
Ok(User {
|
||||||
|
id: row.get(0)?,
|
||||||
|
name: row.get(1)?,
|
||||||
|
sign_key: row.get(2)?,
|
||||||
|
insert_key: row.get(3)?,
|
||||||
|
messages_count: row.get(4)?,
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
let mut users: Vec<User> = Vec::new();
|
||||||
|
for user in user_iter{
|
||||||
|
log::info!("User: {:?}", (&user));
|
||||||
|
users.push(user?);
|
||||||
|
}
|
||||||
|
Ok(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_user(user: User, conn: &Connection) -> Result<()> {
|
||||||
|
match conn.execute(
|
||||||
|
"INSERT INTO users (
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
sign_key,
|
||||||
|
insert_key,
|
||||||
|
messages_count
|
||||||
|
) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||||
|
params![
|
||||||
|
user.id,
|
||||||
|
user.name,
|
||||||
|
user.sign_key,
|
||||||
|
user.insert_key,
|
||||||
|
user.messages_count
|
||||||
|
],
|
||||||
|
) {
|
||||||
|
Ok(_) => log::info!("add user to USERS successfully!"),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("failed to add user {:?}", e);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue