Skip to content

Commit

Permalink
fix: 🩹 a little further adaptation for restful
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahinli committed May 30, 2024
1 parent 8c04db0 commit cfae099
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ debug/
target/
.vscode/
certificates/
configs/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
5 changes: 5 additions & 0 deletions configs/databaseconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
address=0.0.0.0:5000
username=root
password=root
namespace=acapair
database=acapair
18 changes: 6 additions & 12 deletions src/db/db_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn search_channel_by_username(
for element in searched {
match element {
Some(channel) => {
if channel.username == username.to_string() {
if channel.username == *username {
return Some(channel);
}
}
Expand All @@ -73,7 +73,7 @@ pub async fn create_channel(username: &String, db: &Surreal<Client>) -> Vec<Opti
match search_channel_by_username(username, db).await {
Some(_) => {
eprintln!("Already Exists");
return vec![];
vec![]
}
None => {
db.query("DEFINE INDEX usernameINDEX ON TABLE channel COLUMNS username UNIQUE")
Expand Down Expand Up @@ -485,13 +485,10 @@ pub async fn is_follower_by_username(
Some(follower_channel) => match search_channel_by_username(followed, db).await {
Some(mut followed_channel) => {
followed_channel.follower_list.sort();
match followed_channel
followed_channel
.follower_list
.binary_search(&follower_channel.id.unwrap().id)
{
Ok(_) => true,
Err(_) => false,
}
.is_ok()
}
None => {
eprintln!("Error: Can't Check Is Follower | Followed Not Exists");
Expand All @@ -510,13 +507,10 @@ pub async fn is_banned_by_username(victim: &String, judge: &String, db: &Surreal
Some(victim_channel) => match search_channel_by_username(judge, db).await {
Some(mut judge_channel) => {
judge_channel.banned_list.sort();
match judge_channel
judge_channel
.banned_list
.binary_search(&victim_channel.id.unwrap().id)
{
Ok(_) => true,
Err(_) => false,
}
.is_ok()
}
None => {
eprintln!("Error: Can't Check Is Banned | Judge Not Exists");
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() {
};

let app = routing::routing(axum::extract::State(state)).await;
let addr = SocketAddr::from(take_args().parse::<SocketAddr>().unwrap());
let addr = take_args().parse::<SocketAddr>().unwrap();

axum_server::bind_rustls(addr, tls_config)
.serve(app.into_make_service())
Expand Down
32 changes: 19 additions & 13 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
routing::{delete, get, patch, post},
Json, Router,
};
use tower_http::cors::CorsLayer;
Expand All @@ -12,18 +12,18 @@ use crate::{db::db_operations, utils::database_config, AppState};
pub async fn routing(State(state): State<AppState>) -> 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("/:username", post(create_channel))
.route("/:username", delete(delete_channel))
.route("/:username", get(search_username))
.route("/id/:id", get(search_id))
.route(
"/change-username/:username/:updated_username",
get(change_username),
"/username/:username/:updated_username",
patch(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))
.route("/follow/:follower/:followed", patch(follow))
.route("/unfollow/:follower/:followed", patch(unfollow))
.route("/ban/:victim/:judge", patch(ban))
.route("/unban/:victim/:judge", patch(unban))
.route("/is-follower/:follower/:followed", get(is_follower))
.route("/is-banned/:victim/:judge", get(is_banned))
.layer(CorsLayer::permissive())
Expand All @@ -42,7 +42,10 @@ async fn alive() -> impl IntoResponse {
(StatusCode::OK, Json(alive_json))
}

async fn create(Path(username): Path<String>, State(state): State<AppState>) -> impl IntoResponse {
async fn create_channel(
Path(username): Path<String>,
State(state): State<AppState>,
) -> impl IntoResponse {
match db_operations::create(&username, &state.db).await {
Some(channel) => {
let create = serde_json::json!({
Expand All @@ -53,7 +56,10 @@ async fn create(Path(username): Path<String>, State(state): State<AppState>) ->
None => (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))),
}
}
async fn delete(Path(username): Path<String>, State(state): State<AppState>) -> impl IntoResponse {
async fn delete_channel(
Path(username): Path<String>,
State(state): State<AppState>,
) -> impl IntoResponse {
match db_operations::delete(&username, &state.db).await {
Some(channel) => {
let delete = serde_json::json!({
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub async fn database_config() -> DataBaseConfig {
.await
.unwrap();

let configs_parsed: Vec<&str> = config_unparsed.split_terminator("\n").collect();
let configs_parsed: Vec<&str> = config_unparsed.split_terminator('\n').collect();
let mut configs_cleaned: Vec<&str> = vec![];

for element in configs_parsed {
let dirty: Vec<&str> = element.split(": ").collect();
let dirty: Vec<&str> = element.split('=').collect();
configs_cleaned.push(dirty[1]);
}

Expand Down

0 comments on commit cfae099

Please sign in to comment.