use crate::database::Database;
use crate::model::{DatabaseError, IpBanCreate};
use databeam::DefaultReturn;
use axum::response::IntoResponse;
use axum::{
extract::{Path, State},
Json,
};
use axum_extra::extract::cookie::CookieJar;
pub async fn create_request(
jar: CookieJar,
State(database): State<Database>,
Json(props): Json<IpBanCreate>,
) -> impl IntoResponse {
let auth_user = match jar.get("__Secure-Token") {
Some(c) => match database
.get_profile_by_unhashed(c.value_trimmed().to_string())
.await
{
Ok(ua) => ua,
Err(e) => {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
},
None => {
return Json(DefaultReturn {
success: false,
message: DatabaseError::NotAllowed.to_string(),
payload: (),
});
}
};
match database.create_ipban(props, auth_user).await {
Ok(_) => Json(DefaultReturn {
success: true,
message: "Acceptable".to_string(),
payload: (),
}),
Err(e) => Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
}),
}
}
pub async fn delete_request(
jar: CookieJar,
Path(id): Path<String>,
State(database): State<Database>,
) -> impl IntoResponse {
let auth_user = match jar.get("__Secure-Token") {
Some(c) => match database
.get_profile_by_unhashed(c.value_trimmed().to_string())
.await
{
Ok(ua) => ua,
Err(e) => {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
},
None => {
return Json(DefaultReturn {
success: false,
message: DatabaseError::NotAllowed.to_string(),
payload: (),
});
}
};
match database.delete_ipban(id, auth_user).await {
Ok(_) => Json(DefaultReturn {
success: true,
message: "Acceptable".to_string(),
payload: (),
}),
Err(e) => Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
}),
}
}