style: 🎨 cargo fmt
This commit is contained in:
parent
1e8caffb97
commit
8088efd920
1 changed files with 132 additions and 128 deletions
112
src/db.rs
112
src/db.rs
|
@ -30,12 +30,15 @@ pub async fn establish_connection() -> Surreal<Client> {
|
|||
db
|
||||
}
|
||||
|
||||
pub async fn search_channel_by_username(username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn search_channel_by_username(
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
let searched: Option<Channel> = db.select(("channel", username)).await.unwrap();
|
||||
searched
|
||||
}
|
||||
async fn search_channel_by_id(id: &Id, db: &Surreal<Client>) -> Option<Channel> {
|
||||
let searced:Option<Channel> = db.select(("channel", id.clone())).await.unwrap();
|
||||
let searced: Option<Channel> = db.select(("channel", id.clone())).await.unwrap();
|
||||
searced
|
||||
}
|
||||
|
||||
|
@ -66,10 +69,10 @@ pub async fn create_channel(username: &String, db: &Surreal<Client>) -> Option<V
|
|||
pub async fn delete_channel(username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(channel) => {
|
||||
let deleted:Result<Option<Channel>, surrealdb::Error> = db.delete(("channel", channel.username)).await;
|
||||
let deleted: Result<Option<Channel>, surrealdb::Error> =
|
||||
db.delete(("channel", channel.username)).await;
|
||||
match deleted {
|
||||
Ok(channel) => {
|
||||
match channel {
|
||||
Ok(channel) => match channel {
|
||||
Some(channel) => {
|
||||
remove_follower_artifacts(channel.clone(), db).await;
|
||||
remove_banned_artifacts(channel, db).await
|
||||
|
@ -78,8 +81,7 @@ pub async fn delete_channel(username: &String, db: &Surreal<Client>) -> Option<C
|
|||
eprintln!("Error: Channel Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(err_val) => {
|
||||
eprintln!("Error: Delete | {}", err_val);
|
||||
None
|
||||
|
@ -118,7 +120,7 @@ async fn update_channel(channel: Channel, db: &Surreal<Client>) -> Option<Channe
|
|||
}
|
||||
}
|
||||
}
|
||||
async fn add_id_to_vector(id:Id, mut data:Vec<Id>) -> Option<Vec<Id>> {
|
||||
async fn add_id_to_vector(id: Id, mut data: Vec<Id>) -> Option<Vec<Id>> {
|
||||
data.sort();
|
||||
match data.binary_search(&id) {
|
||||
Ok(_) => {
|
||||
|
@ -131,7 +133,7 @@ async fn add_id_to_vector(id:Id, mut data:Vec<Id>) -> Option<Vec<Id>> {
|
|||
}
|
||||
}
|
||||
}
|
||||
async fn remove_id_from_vector(id:Id, mut data:Vec<Id>) -> Option<Vec<Id>> {
|
||||
async fn remove_id_from_vector(id: Id, mut data: Vec<Id>) -> Option<Vec<Id>> {
|
||||
data.sort();
|
||||
match data.binary_search(&id) {
|
||||
Ok(_) => {
|
||||
|
@ -149,8 +151,8 @@ async fn remove_follower_artifacts(mut channel: Channel, db: &Surreal<Client>) -
|
|||
match search_channel_by_id(&id, db).await {
|
||||
Some(follower_channel) => {
|
||||
match remove_id_from_vector(id.clone(), follower_channel.followed_list).await {
|
||||
Some(_) => {},
|
||||
None => {},
|
||||
Some(_) => {}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
|
@ -166,8 +168,8 @@ async fn remove_banned_artifacts(mut channel: Channel, db: &Surreal<Client>) ->
|
|||
match search_channel_by_id(&id, db).await {
|
||||
Some(banned_channel) => {
|
||||
match remove_id_from_vector(id.clone(), banned_channel.banned_from_list).await {
|
||||
Some(_) => {},
|
||||
None => {},
|
||||
Some(_) => {}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
|
@ -202,8 +204,7 @@ pub async fn add_follower(
|
|||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => match search_channel_by_username(follower, db).await {
|
||||
Some(follower) => {
|
||||
match add_id_to_vector(follower.id, channel.follower_list).await {
|
||||
Some(follower) => match add_id_to_vector(follower.id, channel.follower_list).await {
|
||||
Some(follower_list) => {
|
||||
channel.follower_list = follower_list;
|
||||
update_channel(channel, db).await
|
||||
|
@ -212,8 +213,7 @@ pub async fn add_follower(
|
|||
eprintln!("Error: Add Follower Id");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Follower Not Exists");
|
||||
None
|
||||
|
@ -255,11 +255,14 @@ pub async fn remove_follower(
|
|||
}
|
||||
}
|
||||
}
|
||||
pub async fn add_banned(banned: &String, username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn add_banned(
|
||||
banned: &String,
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => match search_channel_by_username(banned, db).await {
|
||||
Some(banned) => {
|
||||
match add_id_to_vector(banned.id, channel.banned_list).await {
|
||||
Some(banned) => match add_id_to_vector(banned.id, channel.banned_list).await {
|
||||
Some(banned_list) => {
|
||||
channel.banned_list = banned_list;
|
||||
update_channel(channel, db).await
|
||||
|
@ -268,8 +271,7 @@ pub async fn add_banned(banned: &String, username: &String, db: &Surreal<Client>
|
|||
eprintln!("Error: Add Banned Id");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Banned Not Exists");
|
||||
None
|
||||
|
@ -287,10 +289,8 @@ pub async fn remove_banned(
|
|||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => {
|
||||
match search_channel_by_username(banned, db).await {
|
||||
Some(banned) => {
|
||||
match remove_id_from_vector(banned.id, channel.banned_list).await {
|
||||
Some(mut channel) => match search_channel_by_username(banned, db).await {
|
||||
Some(banned) => match remove_id_from_vector(banned.id, channel.banned_list).await {
|
||||
Some(banned_list) => {
|
||||
channel.banned_list = banned_list;
|
||||
update_channel(channel, db).await
|
||||
|
@ -299,26 +299,26 @@ pub async fn remove_banned(
|
|||
eprintln!("Error: Remove Banned Id");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Banned Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Remove Banned");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn add_followed(followed:&String, username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn add_followed(
|
||||
followed: &String,
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => {
|
||||
match search_channel_by_username(followed, db).await {
|
||||
Some(followed) => {
|
||||
match add_id_to_vector(followed.id, channel.followed_list).await {
|
||||
Some(mut channel) => match search_channel_by_username(followed, db).await {
|
||||
Some(followed) => match add_id_to_vector(followed.id, channel.followed_list).await {
|
||||
Some(followed_list) => {
|
||||
channel.followed_list = followed_list;
|
||||
update_channel(channel, db).await
|
||||
|
@ -327,24 +327,25 @@ pub async fn add_followed(followed:&String, username: &String, db: &Surreal<Clie
|
|||
eprintln!("Error: Add Followed Id");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Followed Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Add Followed");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn remove_followed(followed:&String, username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn remove_followed(
|
||||
followed: &String,
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => {
|
||||
match search_channel_by_username(followed, db).await {
|
||||
Some(mut channel) => match search_channel_by_username(followed, db).await {
|
||||
Some(followed) => {
|
||||
match remove_id_from_vector(followed.id, channel.followed_list).await {
|
||||
Some(followed_list) => {
|
||||
|
@ -361,18 +362,20 @@ pub async fn remove_followed(followed:&String, username: &String, db: &Surreal<C
|
|||
eprintln!("Error: Followed Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Remove Followed");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn add_banned_from(banned_from: &String, username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn add_banned_from(
|
||||
banned_from: &String,
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => {
|
||||
match search_channel_by_username(banned_from, db).await {
|
||||
Some(mut channel) => match search_channel_by_username(banned_from, db).await {
|
||||
Some(banned_from) => {
|
||||
match add_id_to_vector(banned_from.id, channel.banned_from_list).await {
|
||||
Some(banned_from_list) => {
|
||||
|
@ -389,18 +392,20 @@ pub async fn add_banned_from(banned_from: &String, username: &String, db: &Surre
|
|||
eprintln!("Error: Followed Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Add Banned From");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn remove_banned_from(banned_from: &String, username: &String, db: &Surreal<Client>) -> Option<Channel> {
|
||||
pub async fn remove_banned_from(
|
||||
banned_from: &String,
|
||||
username: &String,
|
||||
db: &Surreal<Client>,
|
||||
) -> Option<Channel> {
|
||||
match search_channel_by_username(username, db).await {
|
||||
Some(mut channel) => {
|
||||
match search_channel_by_username(banned_from, db).await {
|
||||
Some(mut channel) => match search_channel_by_username(banned_from, db).await {
|
||||
Some(banned_from) => {
|
||||
match remove_id_from_vector(banned_from.id, channel.banned_from_list).await {
|
||||
Some(banned_from_list) => {
|
||||
|
@ -417,8 +422,7 @@ pub async fn remove_banned_from(banned_from: &String, username: &String, db: &Su
|
|||
eprintln!("Error: Banned Not Exists");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
eprintln!("Error: Remove Banned From");
|
||||
None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue