implement load_user function

This commit is contained in:
Horhik 2021-03-29 11:44:26 +03:00
parent be2705640c
commit e582194b25
6 changed files with 66 additions and 23 deletions

17
Cargo.lock generated
View file

@ -303,6 +303,7 @@ dependencies = [
"libc", "libc",
"num-integer", "num-integer",
"num-traits", "num-traits",
"serde",
"time", "time",
"winapi", "winapi",
] ]
@ -412,6 +413,7 @@ version = "0.0.2"
dependencies = [ dependencies = [
"regex", "regex",
"rusqlite", "rusqlite",
"serde",
] ]
[[package]] [[package]]
@ -1301,15 +1303,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.116" version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5" checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
dependencies = [
"serde_derive",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.116" version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8" checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1394,9 +1399,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.58" version = "1.0.65"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" checksum = "f3a1d708c221c5a612956ef9f75b37e454e88d1f7b899fbd3a18d4252012d663"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -22,7 +22,7 @@ serde = "1.0.116"
serde_derive = "1.0.116" serde_derive = "1.0.116"
async-trait = "0.1.42" async-trait = "0.1.42"
simple_logger = "1.11.0" simple_logger = "1.11.0"
chrono = "0.4.19" chrono = {version = "0.4.19", features = ["serde"]}
[dependencies.fcpv2] [dependencies.fcpv2]
path = "../FCPv2" path = "../FCPv2"

View file

@ -1,9 +1,12 @@
use super::request::*; use super::request::*;
use super::response::User;
use super::response::UserList;
use crate::chat::types::PackedMessage;
use crate::chat::types::SP; use crate::chat::types::SP;
use crate::db::{messages, users};
use async_std::io::Result; use async_std::io::Result;
use rusqlite::Connection; use rusqlite::Connection;
use crate::db::{messages, users}; use serde_json::json;
use crate::chat::types::PackedMessage;
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
@ -14,9 +17,16 @@ pub fn stop_app(request: StopAppReq, conn: &Connection, server_sender: &SP) -> R
} }
pub fn load_users(request: LoadUsersReq, conn: &Connection, server_sender: &SP) -> Result<()> { pub fn load_users(request: LoadUsersReq, conn: &Connection, server_sender: &SP) -> Result<()> {
let users = users::load_all_users(conn); let jsoned_users: Vec<_> = users::load_all_users(conn)
unimplemented!(); .unwrap()
//sending *JSON* .into_iter()
.map(|x| x.to_jsonable())
.collect();
let users: String = serde_json::to_string(&UserList {
users: jsoned_users,
}).unwrap();
let _ =server_sender.send(PackedMessage::ToClient(users)).unwrap();
Ok(())
} }
pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> { pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> {
unimplemented!() unimplemented!()

View file

@ -1,5 +1,7 @@
use crate::db::types::User as SqliteUser;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use crate::db::types::SignKey;
pub type InsertKey = String;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
enum ResponseType{ enum ResponseType{
Error, Error,
@ -25,3 +27,19 @@ struct AppError{
struct ActionStatus{ struct ActionStatus{
} }
#[derive(Debug, Deserialize, Serialize)]
pub struct User {
pub id: u32,
pub name: String,
pub sign_key: SignKey,
pub insert_key: InsertKey,
pub messages_count: u32,
}
#[derive(Serialize, Deserialize)]
pub struct UserList{
pub users: Vec<User>
}

View file

@ -37,7 +37,3 @@ pub async fn request_selector(json: String, server_sender: &SP, conn: &Connectio
)) ))
} }
#[test]
fn is_making_correct_jsons() {
let json = from_str::<StopApp>("{\"req_type\":\"StartAppReq\"}").unwrap();
}

View file

@ -1,13 +1,15 @@
use chrono::prelude::*; use chrono::prelude::*;
use fcpv2::types::{traits::FcpRequest, SSK}; use fcpv2::types::{traits::FcpRequest, SSK};
use serde_derive::{Serialize, Deserialize};
use crate::api::response::User as JsonableUser;
pub const DB_PATH: &str = "hole.db"; pub const DB_PATH: &str = "hole.db";
pub type SignKey = String; pub type SignKey = String;
pub type InsertKey = SSK; pub type InsertKey = SSK;
#[derive(Debug)] #[derive(Debug, Deserialize, Serialize)]
pub struct User { pub struct User {
pub id: u32, pub id: u32,
pub name: String, pub name: String,
@ -16,7 +18,19 @@ pub struct User {
pub messages_count: u32, pub messages_count: u32,
} }
#[derive(Debug)] impl User{
pub fn to_jsonable(self) -> JsonableUser{
JsonableUser{
id: self.id,
name: self.name,
sign_key: self.sign_key,
insert_key: SSK::convert(&self.insert_key),
messages_count: self.messages_count
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Message { pub struct Message {
pub id: u32, pub id: u32,
pub date: NaiveDateTime, pub date: NaiveDateTime,