use axum::{
response::{Html, IntoResponse, Json, Redirect, Response},
};
use serde::Serialize;
use serde_json;
use crate::session_manager::RustBasicSessionStore;
pub struct ResponseHelper;
impl ResponseHelper {
#[allow(dead_code)]
pub fn view(html_content: String) -> Response {
Html(html_content).into_response()
}
pub fn json<T: Serialize>(data: T) -> Response {
Json(data).into_response()
}
#[allow(dead_code)]
pub fn redirect(url: &str) -> Response {
Redirect::to(url).into_response()
}
#[allow(dead_code)]
pub fn success(message: &str) -> Response {
Json(serde_json::json!({
"status": "success",
"message": message
})).into_response()
}
#[allow(dead_code)]
pub fn not_found() -> Response {
Json(serde_json::json!({
"status": "error",
"message": "Resource not found"
})).into_response()
}
#[allow(dead_code)]
pub fn error(message: &str) -> Response {
Json(serde_json::json!({
"status": "error",
"message": message
})).into_response()
}
#[allow(dead_code)]
pub fn internal_server_error() -> Response {
Json(serde_json::json!({
"status": "error",
"message": "Internal server error"
})).into_response()
}
pub fn redirect_with_success(
url: &str,
message: &str,
session: axum_session::Session<RustBasicSessionStore>
) -> Response {
session.set("flash_success", message);
Redirect::to(url).into_response()
}
pub fn redirect_with_error(
url: &str,
message: &str,
session: axum_session::Session<RustBasicSessionStore>
) -> Response {
session.set("flash_error", message);
Redirect::to(url).into_response()
}
}