fix: 🩹 a little further adaptation for restful

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-05-30 23:27:43 +03:00
parent 8c04db05b8
commit cfae099413
6 changed files with 33 additions and 29 deletions

1
.gitignore vendored
View file

@ -4,7 +4,6 @@ debug/
target/ target/
.vscode/ .vscode/
certificates/ certificates/
configs/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # 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 # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

5
configs/databaseconfig Normal file
View file

@ -0,0 +1,5 @@
address=0.0.0.0:5000
username=root
password=root
namespace=acapair
database=acapair

View file

@ -57,7 +57,7 @@ pub async fn search_channel_by_username(
for element in searched { for element in searched {
match element { match element {
Some(channel) => { Some(channel) => {
if channel.username == username.to_string() { if channel.username == *username {
return Some(channel); return Some(channel);
} }
} }
@ -73,7 +73,7 @@ pub async fn create_channel(username: &String, db: &Surreal<Client>) -> Vec<Opti
match search_channel_by_username(username, db).await { match search_channel_by_username(username, db).await {
Some(_) => { Some(_) => {
eprintln!("Already Exists"); eprintln!("Already Exists");
return vec![]; vec![]
} }
None => { None => {
db.query("DEFINE INDEX usernameINDEX ON TABLE channel COLUMNS username UNIQUE") db.query("DEFINE INDEX usernameINDEX ON TABLE channel COLUMNS username UNIQUE")
@ -485,13 +485,10 @@ pub async fn is_follower_by_username(
Some(follower_channel) => match search_channel_by_username(followed, db).await { Some(follower_channel) => match search_channel_by_username(followed, db).await {
Some(mut followed_channel) => { Some(mut followed_channel) => {
followed_channel.follower_list.sort(); followed_channel.follower_list.sort();
match followed_channel followed_channel
.follower_list .follower_list
.binary_search(&follower_channel.id.unwrap().id) .binary_search(&follower_channel.id.unwrap().id)
{ .is_ok()
Ok(_) => true,
Err(_) => false,
}
} }
None => { None => {
eprintln!("Error: Can't Check Is Follower | Followed Not Exists"); eprintln!("Error: Can't Check Is Follower | Followed Not Exists");
@ -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(victim_channel) => match search_channel_by_username(judge, db).await {
Some(mut judge_channel) => { Some(mut judge_channel) => {
judge_channel.banned_list.sort(); judge_channel.banned_list.sort();
match judge_channel judge_channel
.banned_list .banned_list
.binary_search(&victim_channel.id.unwrap().id) .binary_search(&victim_channel.id.unwrap().id)
{ .is_ok()
Ok(_) => true,
Err(_) => false,
}
} }
None => { None => {
eprintln!("Error: Can't Check Is Banned | Judge Not Exists"); eprintln!("Error: Can't Check Is Banned | Judge Not Exists");

View file

@ -28,7 +28,7 @@ async fn main() {
}; };
let app = routing::routing(axum::extract::State(state)).await; 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) axum_server::bind_rustls(addr, tls_config)
.serve(app.into_make_service()) .serve(app.into_make_service())

View file

@ -2,7 +2,7 @@ use axum::{
extract::{Path, State}, extract::{Path, State},
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::get, routing::{delete, get, patch, post},
Json, Router, Json, Router,
}; };
use tower_http::cors::CorsLayer; use tower_http::cors::CorsLayer;
@ -12,18 +12,18 @@ use crate::{db::db_operations, utils::database_config, AppState};
pub async fn routing(State(state): State<AppState>) -> Router { pub async fn routing(State(state): State<AppState>) -> Router {
Router::new() Router::new()
.route("/", get(alive)) .route("/", get(alive))
.route("/create/:username", get(create)) .route("/:username", post(create_channel))
.route("/delete/:username", get(delete)) .route("/:username", delete(delete_channel))
.route("/search-username/:username", get(search_username)) .route("/:username", get(search_username))
.route("/search-id/:id", get(search_id)) .route("/id/:id", get(search_id))
.route( .route(
"/change-username/:username/:updated_username", "/username/:username/:updated_username",
get(change_username), patch(change_username),
) )
.route("/follow/:follower/:followed", get(follow)) .route("/follow/:follower/:followed", patch(follow))
.route("/unfollow/:follower/:followed", get(unfollow)) .route("/unfollow/:follower/:followed", patch(unfollow))
.route("/ban/:victim/:judge", get(ban)) .route("/ban/:victim/:judge", patch(ban))
.route("/unban/:victim/:judge", get(unban)) .route("/unban/:victim/:judge", patch(unban))
.route("/is-follower/:follower/:followed", get(is_follower)) .route("/is-follower/:follower/:followed", get(is_follower))
.route("/is-banned/:victim/:judge", get(is_banned)) .route("/is-banned/:victim/:judge", get(is_banned))
.layer(CorsLayer::permissive()) .layer(CorsLayer::permissive())
@ -42,7 +42,10 @@ async fn alive() -> impl IntoResponse {
(StatusCode::OK, Json(alive_json)) (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 { match db_operations::create(&username, &state.db).await {
Some(channel) => { Some(channel) => {
let create = serde_json::json!({ let create = serde_json::json!({
@ -53,7 +56,10 @@ async fn create(Path(username): Path<String>, State(state): State<AppState>) ->
None => (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))), 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 { match db_operations::delete(&username, &state.db).await {
Some(channel) => { Some(channel) => {
let delete = serde_json::json!({ let delete = serde_json::json!({

View file

@ -17,11 +17,11 @@ pub async fn database_config() -> DataBaseConfig {
.await .await
.unwrap(); .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![]; let mut configs_cleaned: Vec<&str> = vec![];
for element in configs_parsed { for element in configs_parsed {
let dirty: Vec<&str> = element.split(": ").collect(); let dirty: Vec<&str> = element.split('=').collect();
configs_cleaned.push(dirty[1]); configs_cleaned.push(dirty[1]);
} }