finish send_message function

This commit is contained in:
Horhik 2021-04-05 19:39:36 +03:00
parent c822025d12
commit 9ef832a332
5 changed files with 59 additions and 7 deletions

View file

@ -1,5 +1,6 @@
use super::response::User;
use super::response::UserList;
use std::time::{SystemTime};
use super::{
request::*,
response::{AppStatus, ResponseType},
@ -17,6 +18,8 @@ use rusqlite::Connection;
use serde_json::json;
use uuid::Uuid;
use crate::db::types::Id;
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
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<()> {
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 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 =
ClientPut::new_default_direct(key, identifier, &request.message[..]).convert();
server_sender

View file

@ -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());
let sr = client_sender.clone();
let stream = TcpStream::connect(&addr).await.expect("weeror here");
let (receiver, sender) = stream.into_split();
let stream = TcpStream::connect(&addr).await.expect("Unable to connect to FCP");
let (receiver, sender) = stream.into_split();
log::info!("Connected to FCP");
let t = task::spawn(server_responce_getter(receiver, client_sender));
to_server_sender(sender, server_receiver, sr).await?;
match t.await {

View file

@ -8,6 +8,7 @@ fn ret_mes(row: &rusqlite::Row<'_>) -> Result<Message> {
user_id: row.get(1)?,
date: row.get(2)?,
message: row.get(3)?,
from_me: row.get(4)?,
})
}
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>> {
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 mut messages: Vec<Message> = Vec::new();
for message in message_iter {
@ -58,7 +59,28 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
id,
user_id,
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)",
params![message.id, message.user_id, message.date, message.message],
) {

View file

@ -24,13 +24,27 @@ fn create_db(conn: &Connection) -> Result<()> {
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
date datetime NOT NULL,
message TEXT NOT NULL
message TEXT NOT NULL,
from_me BOOL
)",
params![],
) {
Ok(_) => log::info!("MESSAGES table created successfully!"),
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(())
}

View file

@ -12,6 +12,7 @@ use uuid::Uuid;
use crate::api::response::User as JsonableUser;
pub const DB_PATH: &str = "hole.db";
pub type Time = chrono::DateTime<chrono::Local>;
pub type SignKey = String;
pub type InsertKey = SSK;
@ -66,7 +67,8 @@ impl User {
#[derive(Debug, Deserialize, Serialize)]
pub struct Message {
pub id: u32,
pub date: NaiveDateTime,
pub user_id: u32,
pub date: Time,
pub user_id: Id,
pub message: String,
pub from_me: bool,
}