finish send_message function
This commit is contained in:
parent
c822025d12
commit
9ef832a332
|
@ -1,5 +1,6 @@
|
||||||
use super::response::User;
|
use super::response::User;
|
||||||
use super::response::UserList;
|
use super::response::UserList;
|
||||||
|
use std::time::{SystemTime};
|
||||||
use super::{
|
use super::{
|
||||||
request::*,
|
request::*,
|
||||||
response::{AppStatus, ResponseType},
|
response::{AppStatus, ResponseType},
|
||||||
|
@ -17,6 +18,8 @@ use rusqlite::Connection;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::db::types::Id;
|
||||||
|
|
||||||
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
|
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
//sending *JSON*, what everything is OK
|
//sending *JSON*, what everything is OK
|
||||||
|
@ -41,8 +44,18 @@ pub fn load_users(request: LoadUsersReq, conn: &Connection, server_sender: &SP)
|
||||||
}
|
}
|
||||||
pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> {
|
pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> {
|
||||||
if let Ok(user_data) = db::users::get_user_by_id(request.user_id, conn) {
|
if let Ok(user_data) = db::users::get_user_by_id(request.user_id, conn) {
|
||||||
|
// Add message to DB
|
||||||
let key = user_data.insert_key;
|
let key = user_data.insert_key;
|
||||||
let identifier = &user_data.id.0.to_string()[..];
|
let identifier = &user_data.id.0.to_string()[..];
|
||||||
|
let message_id :u32 = user_data.messages_count;
|
||||||
|
let db_message = db::types::Message{
|
||||||
|
id: message_id,
|
||||||
|
date: chrono::offset::Local::now(),
|
||||||
|
user_id: Id(uuid::Uuid::parse_str(identifier).unwrap()),
|
||||||
|
message: request.message.clone(),
|
||||||
|
from_me: false,
|
||||||
|
};
|
||||||
|
let _ = db::messages::add_my_message(db_message, conn).unwrap();
|
||||||
let fcp_req: String =
|
let fcp_req: String =
|
||||||
ClientPut::new_default_direct(key, identifier, &request.message[..]).convert();
|
ClientPut::new_default_direct(key, identifier, &request.message[..]).convert();
|
||||||
server_sender
|
server_sender
|
||||||
|
|
|
@ -19,8 +19,9 @@ async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result
|
||||||
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
||||||
|
|
||||||
let sr = client_sender.clone();
|
let sr = client_sender.clone();
|
||||||
let stream = TcpStream::connect(&addr).await.expect("weeror here");
|
let stream = TcpStream::connect(&addr).await.expect("Unable to connect to FCP");
|
||||||
let (receiver, sender) = stream.into_split();
|
let (receiver, sender) = stream.into_split();
|
||||||
|
log::info!("Connected to FCP");
|
||||||
let t = task::spawn(server_responce_getter(receiver, client_sender));
|
let t = task::spawn(server_responce_getter(receiver, client_sender));
|
||||||
to_server_sender(sender, server_receiver, sr).await?;
|
to_server_sender(sender, server_receiver, sr).await?;
|
||||||
match t.await {
|
match t.await {
|
||||||
|
|
|
@ -8,6 +8,7 @@ fn ret_mes(row: &rusqlite::Row<'_>) -> Result<Message> {
|
||||||
user_id: row.get(1)?,
|
user_id: row.get(1)?,
|
||||||
date: row.get(2)?,
|
date: row.get(2)?,
|
||||||
message: row.get(3)?,
|
message: row.get(3)?,
|
||||||
|
from_me: row.get(4)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
type Id = crate::db::types::Id;
|
type Id = crate::db::types::Id;
|
||||||
|
@ -22,7 +23,7 @@ pub fn select_message_by_id(user_id: u32, id:u32, conn: &Connection) -> Result<M
|
||||||
|
|
||||||
pub fn select_all_user_message(id: u32, conn: &Connection) -> Result<Vec<Message>> {
|
pub fn select_all_user_message(id: u32, conn: &Connection) -> Result<Vec<Message>> {
|
||||||
let mut selected =
|
let mut selected =
|
||||||
conn.prepare("SELECT * FROM messages WHERE user_id = ?1 ORDER BY date DESC")?;
|
conn.prepare("SELECT * FROM messages, my_messages WHERE user_id = ?1 ORDER BY date DESC")?;
|
||||||
let message_iter = selected.query_map(params![id], |row| ret_mes(row))?;
|
let message_iter = selected.query_map(params![id], |row| ret_mes(row))?;
|
||||||
let mut messages: Vec<Message> = Vec::new();
|
let mut messages: Vec<Message> = Vec::new();
|
||||||
for message in message_iter {
|
for message in message_iter {
|
||||||
|
@ -58,7 +59,28 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
|
||||||
id,
|
id,
|
||||||
user_id,
|
user_id,
|
||||||
date,
|
date,
|
||||||
message
|
message,
|
||||||
|
from_me
|
||||||
|
) VALUES (?1, ?2, ?3, ?4)",
|
||||||
|
params![message.id, message.user_id, message.date, message.message],
|
||||||
|
) {
|
||||||
|
Ok(_) => log::info!("message {:} added succsessfully!", message.id),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("failed to insert message {:?}", e);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_my_message(message: Message, conn: &Connection) -> Result<()> {
|
||||||
|
match conn.execute(
|
||||||
|
"INSERT INTO messages (
|
||||||
|
id,
|
||||||
|
user_id,
|
||||||
|
date,
|
||||||
|
message,
|
||||||
|
from_me
|
||||||
) VALUES (?1, ?2, ?3, ?4)",
|
) VALUES (?1, ?2, ?3, ?4)",
|
||||||
params![message.id, message.user_id, message.date, message.message],
|
params![message.id, message.user_id, message.date, message.message],
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -24,13 +24,27 @@ fn create_db(conn: &Connection) -> Result<()> {
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
date datetime NOT NULL,
|
date datetime NOT NULL,
|
||||||
message TEXT NOT NULL
|
message TEXT NOT NULL,
|
||||||
|
from_me BOOL
|
||||||
)",
|
)",
|
||||||
params![],
|
params![],
|
||||||
) {
|
) {
|
||||||
Ok(_) => log::info!("MESSAGES table created successfully!"),
|
Ok(_) => log::info!("MESSAGES 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 my_messages (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
date datetime NOT NULL,
|
||||||
|
message TEXT NOT NULL,
|
||||||
|
from_me BOOL
|
||||||
|
)",
|
||||||
|
params![],
|
||||||
|
) {
|
||||||
|
Ok(_) => log::info!("MY_MESSAGES table created successfully!"),
|
||||||
|
Err(e) => log::error!("failed to create USER table {:?}", e),
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ use uuid::Uuid;
|
||||||
use crate::api::response::User as JsonableUser;
|
use crate::api::response::User as JsonableUser;
|
||||||
|
|
||||||
pub const DB_PATH: &str = "hole.db";
|
pub const DB_PATH: &str = "hole.db";
|
||||||
|
pub type Time = chrono::DateTime<chrono::Local>;
|
||||||
|
|
||||||
pub type SignKey = String;
|
pub type SignKey = String;
|
||||||
pub type InsertKey = SSK;
|
pub type InsertKey = SSK;
|
||||||
|
@ -66,7 +67,8 @@ impl User {
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
pub date: NaiveDateTime,
|
pub date: Time,
|
||||||
pub user_id: u32,
|
pub user_id: Id,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
pub from_me: bool,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue