user database operations
This commit is contained in:
parent
085f8baee0
commit
36c72cee4a
11 changed files with 110 additions and 5 deletions
|
@ -2,6 +2,9 @@ pub mod interaction;
|
|||
pub mod post;
|
||||
pub mod user;
|
||||
|
||||
pub type SurrealUserReturn = Result<Option<User>, surrealdb::Error>;
|
||||
pub type SurrealCountReturn = Result<Option<u128>, surrealdb::Error>;
|
||||
|
||||
use std::{sync::LazyLock, time::Duration};
|
||||
|
||||
use surrealdb::{
|
||||
|
@ -11,7 +14,7 @@ use surrealdb::{
|
|||
};
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::DatabaseConfig;
|
||||
use crate::{feature::user::User, DatabaseConfig};
|
||||
|
||||
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
|
||||
|
||||
|
@ -27,6 +30,7 @@ pub async fn establish_connection() {
|
|||
.unwrap();
|
||||
DB.use_ns(database_config.namespace).await.unwrap();
|
||||
DB.use_db(database_config.database).await.unwrap();
|
||||
DB.query("DEFINE INDEX email ON TABLE user FIELDS email UNIQUE;").await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn is_alive() -> bool {
|
||||
|
|
|
@ -2,3 +2,5 @@ async fn create_interaction() {}
|
|||
async fn read_interaction() {}
|
||||
async fn update_interaction() {}
|
||||
async fn delete_interaction() {}
|
||||
async fn count_interactions_of_a_user() {}
|
||||
async fn count_interactions_of_a_post() {}
|
0
src/database/message.rs
Normal file
0
src/database/message.rs
Normal file
|
@ -2,3 +2,4 @@ async fn create_post() {}
|
|||
async fn read_post() {}
|
||||
async fn update_post() {}
|
||||
async fn delete_post() {}
|
||||
async fn count_posts_of_a_user() {}
|
||||
|
|
|
@ -1,4 +1,29 @@
|
|||
async fn create_user() {}
|
||||
async fn read_user() {}
|
||||
async fn update_user() {}
|
||||
async fn delete_user() {}
|
||||
|
||||
use crate::feature::user::User;
|
||||
|
||||
use super::{SurrealCountReturn, SurrealUserReturn, DB};
|
||||
|
||||
|
||||
pub async fn create_user(user:User) -> SurrealUserReturn {
|
||||
DB.create("user").content(user).await
|
||||
}
|
||||
|
||||
pub async fn read_user(email: &String) -> SurrealUserReturn{
|
||||
DB.select(("user", email)).await
|
||||
}
|
||||
|
||||
pub async fn update_user(target_user_email: &String, user: User) -> SurrealUserReturn{
|
||||
DB.update(("user", target_user_email)).content(user).await
|
||||
}
|
||||
pub async fn delete_user(email: &String) -> SurrealUserReturn {
|
||||
DB.delete(("user", email)).await
|
||||
}
|
||||
pub async fn count_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user GROUP BY count;").await?.take("count")
|
||||
}
|
||||
pub async fn count_male_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user WHERE gender = true GROUP BY count;").await?.take("count")
|
||||
}
|
||||
pub async fn count_female_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user WHERE gender = false GROUP BY count;").await?.take("count")
|
||||
}
|
4
src/feature.rs
Normal file
4
src/feature.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub mod user;
|
||||
pub mod message;
|
||||
pub mod post;
|
||||
pub mod interaction;
|
16
src/feature/interaction.rs
Normal file
16
src/feature/interaction.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
enum InteractionType {
|
||||
Like,
|
||||
Dislike,
|
||||
Heart,
|
||||
Confetti,
|
||||
Plus,
|
||||
Minus,
|
||||
Rocket,
|
||||
Smile,
|
||||
Laugh,
|
||||
}
|
||||
struct Interaction {
|
||||
post_id:String,
|
||||
user_id:String,
|
||||
interaction_type: InteractionType,
|
||||
}
|
15
src/feature/message.rs
Normal file
15
src/feature/message.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
pub struct Message {
|
||||
pub sender_email: String,
|
||||
pub receiver_email: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self{
|
||||
Message {
|
||||
sender_email,
|
||||
receiver_email,
|
||||
message,
|
||||
}
|
||||
}
|
||||
}
|
0
src/feature/post.rs
Normal file
0
src/feature/post.rs
Normal file
37
src/feature/user.rs
Normal file
37
src/feature/user.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Contact {
|
||||
pub email: String,
|
||||
pub phone: String,
|
||||
pub website: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum Role {
|
||||
User,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub name: Vec<String>,
|
||||
pub surname: Vec<String>,
|
||||
pub gender: bool,
|
||||
pub birth_date: NaiveDate,
|
||||
pub email: String,
|
||||
pub role: Role,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub async fn new(name: Vec<String>, surname: Vec<String>, gender: bool, birth_date: NaiveDate, email: String) -> Self {
|
||||
Self {
|
||||
name,
|
||||
surname,
|
||||
gender,
|
||||
birth_date,
|
||||
email,
|
||||
role: Role::User,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod feature;
|
||||
pub mod database;
|
||||
pub mod utils;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue