rustbasic_core/
responses.rs1use axum::{
2 response::{Html, IntoResponse, Json, Redirect, Response},
3};
4use serde::Serialize;
5use serde_json;
6use crate::session_manager::RustBasicSessionStore;
7
8pub struct ResponseHelper;
9
10impl ResponseHelper {
11 #[allow(dead_code)]
13 pub fn view(html_content: String) -> Response {
14 Html(html_content).into_response()
15 }
16
17 pub fn json<T: Serialize>(data: T) -> Response {
19 Json(data).into_response()
20 }
21
22 #[allow(dead_code)]
24 pub fn redirect(url: &str) -> Response {
25 Redirect::to(url).into_response()
26 }
27
28 #[allow(dead_code)]
30 pub fn success(message: &str) -> Response {
31 Json(serde_json::json!({
32 "status": "success",
33 "message": message
34 })).into_response()
35 }
36
37 #[allow(dead_code)]
38 pub fn not_found() -> Response {
39 Json(serde_json::json!({
40 "status": "error",
41 "message": "Resource not found"
42 })).into_response()
43 }
44
45 #[allow(dead_code)]
46 pub fn error(message: &str) -> Response {
47 Json(serde_json::json!({
48 "status": "error",
49 "message": message
50 })).into_response()
51 }
52
53 #[allow(dead_code)]
54 pub fn internal_server_error() -> Response {
55 Json(serde_json::json!({
56 "status": "error",
57 "message": "Internal server error"
58 })).into_response()
59 }
60
61 pub fn redirect_with_success(
63 url: &str,
64 message: &str,
65 session: axum_session::Session<RustBasicSessionStore>
66 ) -> Response {
67 session.set("flash_success", message);
68 Redirect::to(url).into_response()
69 }
70
71 pub fn redirect_with_error(
73 url: &str,
74 message: &str,
75 session: axum_session::Session<RustBasicSessionStore>
76 ) -> Response {
77 session.set("flash_error", message);
78 Redirect::to(url).into_response()
79 }
80}