diff --git a/Cargo.toml b/Cargo.toml index 8a56ab0..4b93e9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -axum = "0.7.5" +axum = { version = "0.7.5", features = ["macros"] } axum-server = { version = "0.6.0", features = ["tls-rustls"] } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.115" diff --git a/src/db/db_operations.rs b/src/db/db_operations.rs index ccfe7d6..886a7b8 100644 --- a/src/db/db_operations.rs +++ b/src/db/db_operations.rs @@ -15,13 +15,15 @@ pub async fn create(username: &String, db: &Surreal) -> Option } } -pub async fn search(username: &String, db: &Surreal) -> Option { +pub async fn search_username(username: &String, db: &Surreal) -> Option { search_channel_by_username(username, db).await } +pub async fn search_id(id: &String, db: &Surreal) -> Option { + search_channel_by_id(&id.into(), db).await +} + pub async fn delete(username: &String, db: &Surreal) -> Option { - // delete channel should be last for mind sake - // first artifacts match search_channel_by_username(username, db).await { Some(channel) => match remove_all_followers(channel.clone(), db).await { Some(_) => match remove_all_followed(channel.clone(), db).await { @@ -41,18 +43,6 @@ pub async fn delete(username: &String, db: &Surreal) -> Option None } } - // match delete_channel(username, db).await { - // Some(deleted_channel) => { - // match remove_follower_artifacts(deleted_channel.clone(), db).await { - // Some(_) => match remove_banned_artifacts(deleted_channel.clone(), db).await { - // Some(_) => Some(deleted_channel), - // None => None, - // }, - // None => None, - // } - // } - // None => None, - // } } pub async fn change_username( @@ -103,3 +93,4 @@ pub async fn unban(victim: &String, judge: &String, db: &Surreal) -> Opt None => None, } } + diff --git a/src/db/db_utils.rs b/src/db/db_utils.rs index ce7fe73..8271717 100644 --- a/src/db/db_utils.rs +++ b/src/db/db_utils.rs @@ -37,7 +37,7 @@ pub async fn establish_connection() -> Option> { } } -async fn search_channel_by_id(id: &Id, db: &Surreal) -> Option { +pub async fn search_channel_by_id(id: &Id, db: &Surreal) -> Option { let searced: Option = db.select(("channel", id.clone())).await.unwrap(); searced } diff --git a/src/lib.rs b/src/lib.rs index 9e67b58..b2387a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,14 @@ use serde::{Deserialize, Serialize}; -use surrealdb::sql::{Id, Thing}; +use surrealdb::{engine::remote::ws::Client, sql::{Id, Thing}, Surreal}; pub mod db; pub mod routing; pub mod tests; #[derive(Debug, Clone)] -pub struct AppState {} +pub struct AppState { + pub db: Surreal, +} #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Channel { diff --git a/src/main.rs b/src/main.rs index d8f012e..6bea036 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use acapair_follow_ban_api::{routing, AppState}; +use acapair_follow_ban_api::{db::db_operations::connect, routing, AppState}; use axum_server::tls_rustls::RustlsConfig; use std::{env, net::SocketAddr}; @@ -19,7 +19,9 @@ async fn main() { .await .unwrap(); - let state = AppState {}; + let state = AppState { + db:connect().await.unwrap(), + }; let app = routing::routing(axum::extract::State(state)).await; let addr = SocketAddr::from(take_args().parse::().unwrap()); diff --git a/src/routing.rs b/src/routing.rs index 0a24bf1..6eb5965 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,4 +1,4 @@ -use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router}; +use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::get, Json, Router}; use tower_http::cors::CorsLayer; use crate::{db::db_operations, AppState}; @@ -6,6 +6,15 @@ use crate::{db::db_operations, AppState}; pub async fn routing(State(state): State) -> Router { Router::new() .route("/", get(alive)) + .route("/create/:username", get(create)) + .route("/delete/:username", get(delete)) + .route("/search-username/:username", get(search_username)) + .route("/search-id/:id", get(search_id)) + .route("/change-username/:username/:updated_username", get(change_username)) + .route("/follow/:follower/:followed", get(follow)) + .route("/unfollow/:follower/:followed", get(unfollow)) + .route("/ban/:victim/:judge", get(ban)) + .route("/unban/:victim/:judge", get(unban)) .layer(CorsLayer::permissive()) .with_state(state.clone()) } @@ -22,3 +31,121 @@ async fn alive() -> impl IntoResponse { println!("{}", alive_json); (StatusCode::OK, Json(alive_json)) } + +async fn create(Path(username):Path, State(state): State) -> impl IntoResponse { + match db_operations::create(&username, &state.db).await { + Some(channel) => { + let create = serde_json::json!({ + "channel":channel, + }); + (StatusCode::CREATED, Json(create)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn delete(Path(username):Path, State(state): State) -> impl IntoResponse { + match db_operations::delete(&username, &state.db).await { + Some(channel) => { + let delete = serde_json::json!({ + "channel":channel, + }); + (StatusCode::NO_CONTENT, Json(delete)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn search_username(Path(username):Path, State(state): State) -> impl IntoResponse { + match db_operations::search_username(&username, &state.db).await { + Some(channel) => { + let search_username = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(search_username)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn search_id(Path(id):Path, State(state): State) -> impl IntoResponse { + match db_operations::search_id(&id, &state.db).await { + Some(channel) => { + let search_id = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(search_id)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn change_username(Path((username, updated_username)):Path<(String, String)>, State(state): State) -> impl IntoResponse { + match db_operations::change_username(&updated_username, &username, &state.db).await { + Some(channel) => { + let change_username = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(change_username)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn follow(Path((follower, followed)):Path<(String, String)>, State(state): State) -> impl IntoResponse { + match db_operations::follow(&follower, &followed, &state.db).await { + Some(channel) => { + let follow = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(follow)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn unfollow(Path((follower, followed)):Path<(String, String)>, State(state): State) -> impl IntoResponse { + match db_operations::unfollow(&follower, &followed, &state.db).await { + Some(channel) => { + let unfollow = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(unfollow)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn ban(Path((victim, judge)):Path<(String, String)>, State(state): State) -> impl IntoResponse { + match db_operations::ban(&victim, &judge, &state.db).await { + Some(channel) => { + let ban = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(ban)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} +async fn unban(Path((victim, judge)):Path<(String, String)>, State(state): State) -> impl IntoResponse { + match db_operations::unban(&victim, &judge, &state.db).await { + Some(channel) => { + let unban = serde_json::json!({ + "channel":channel, + }); + (StatusCode::OK, Json(unban)) + } + None => { + (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))) + } + } +} \ No newline at end of file diff --git a/src/tests/db_tests.rs b/src/tests/db_tests.rs index 99b09d4..8da5875 100644 --- a/src/tests/db_tests.rs +++ b/src/tests/db_tests.rs @@ -37,12 +37,26 @@ async fn test_create() { } #[test] -async fn test_search() { - let connection = create_connection_for_tests("test_search").await; +async fn test_search_username() { + let connection = create_connection_for_tests("test_search_username").await; let name = &"Ahmet".to_string(); let created = create(name, &connection).await; - let searched = search(name, &connection).await; + let searched = search_username(name, &connection).await; + + assert_eq!(created, searched); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_search_id() { + let connection = create_connection_for_tests("test_search_username").await; + let name = &"Ahmet".to_string(); + + let created = create(name, &connection).await; + let id = &created.clone().unwrap().id.unwrap().id.to_string(); + let searched = search_id(id, &connection).await; assert_eq!(created, searched); @@ -58,7 +72,7 @@ async fn test_delete() { let deleted = delete(name, &connection).await; assert_eq!(created, deleted); - assert_eq!(search(name, &connection).await.is_none(), true); + assert_eq!(search_username(name, &connection).await.is_none(), true); let _cleaning = connection.query("DELETE channel;").await; } @@ -91,7 +105,7 @@ async fn test_follow() { let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); - let mut followed = search(name_followed, &connection).await.unwrap(); + let mut followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!( followed.follower_list.pop().unwrap(), @@ -117,7 +131,7 @@ async fn test_unfollow() { let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); - let mut followed = search(name_followed, &connection).await.unwrap(); + let mut followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!( followed.follower_list.pop().unwrap(), @@ -131,7 +145,7 @@ async fn test_unfollow() { follower = unfollow(name_follower, name_followed, &connection) .await .unwrap(); - followed = search(name_followed, &connection).await.unwrap(); + followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); @@ -149,7 +163,7 @@ async fn test_ban() { let _judge = create(name_judge, &connection).await.unwrap(); let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); - let mut judge = search(name_judge, &connection).await.unwrap(); + let mut judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); @@ -167,13 +181,13 @@ async fn test_unban() { let _judge = create(name_judge, &connection).await.unwrap(); let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); - let mut judge = search(name_judge, &connection).await.unwrap(); + let mut judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); victim = unban(name_victim, name_judge, &connection).await.unwrap(); - judge = search(name_judge, &connection).await.unwrap(); + judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(victim.banned_from_list.pop().is_none(), true); assert_eq!(judge.banned_list.pop().is_none(), true); @@ -193,7 +207,7 @@ async fn test_delete_follower() { let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); - let mut followed = search(name_followed, &connection).await.unwrap(); + let mut followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!( followed.follower_list.pop().unwrap(), @@ -205,7 +219,7 @@ async fn test_delete_follower() { ); follower = delete(name_follower, &connection).await.unwrap(); - followed = search(name_followed, &connection).await.unwrap(); + followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); @@ -225,7 +239,7 @@ async fn test_delete_followed() { let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); - let mut followed = search(name_followed, &connection).await.unwrap(); + let mut followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!( followed.follower_list.pop().unwrap(), @@ -237,7 +251,7 @@ async fn test_delete_followed() { ); followed = delete(name_followed, &connection).await.unwrap(); - follower = search(name_follower, &connection).await.unwrap(); + follower = search_username(name_follower, &connection).await.unwrap(); assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); @@ -255,13 +269,13 @@ async fn test_delete_victim() { let _judge = create(name_judge, &connection).await.unwrap(); let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); - let mut judge = search(name_judge, &connection).await.unwrap(); + let mut judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); victim = delete(name_victim, &connection).await.unwrap(); - judge = search(name_judge, &connection).await.unwrap(); + judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(judge.banned_list.pop().is_none(), true); assert_eq!(victim.banned_from_list.pop().is_none(), true); @@ -279,13 +293,13 @@ async fn test_delete_judge() { let _judge = create(name_judge, &connection).await.unwrap(); let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); - let mut judge = search(name_judge, &connection).await.unwrap(); + let mut judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); judge = delete(name_judge, &connection).await.unwrap(); - victim = search(name_victim, &connection).await.unwrap(); + victim = search_username(name_victim, &connection).await.unwrap(); assert_eq!(judge.banned_list.pop().is_none(), true); assert_eq!(victim.banned_from_list.pop().is_none(), true); @@ -305,7 +319,7 @@ async fn test_follow_already_follower() { let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); - let mut followed = search(name_followed, &connection).await.unwrap(); + let mut followed = search_username(name_followed, &connection).await.unwrap(); assert_eq!( followed.follower_list.pop().unwrap(), @@ -352,7 +366,7 @@ async fn test_ban_already_banned() { let _judge = create(name_judge, &connection).await.unwrap(); let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); - let mut judge = search(name_judge, &connection).await.unwrap(); + let mut judge = search_username(name_judge, &connection).await.unwrap(); assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); @@ -405,11 +419,21 @@ async fn test_create_already_created() { } #[test] -async fn test_search_noncreated() { - let connection = create_connection_for_tests("test_search_noncreated").await; +async fn test_search_username_noncreated() { + let connection = create_connection_for_tests("test_search_username_noncreated").await; + let name = &"Ahmet".to_string(); + + assert_eq!(search_username(name, &connection).await.is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_search_id_noncreated() { + let connection = create_connection_for_tests("test_search_id_noncreated").await; let name = &"Ahmet".to_string(); - assert_eq!(search(name, &connection).await.is_none(), true); + assert_eq!(search_id(name, &connection).await.is_none(), true); let _cleaning = connection.query("DELETE channel;").await; }