hostbasket/actix_mvc_app/src/controllers/home.rs
2025-04-19 19:59:28 +02:00

102 lines
3.7 KiB
Rust

use actix_web::{web, HttpResponse, Responder, Result};
use tera::Tera;
use crate::models::User;
/// Controller for handling home-related routes
pub struct HomeController;
impl HomeController {
/// Handles the markdown editor page route
pub async fn editor(tmpl: web::Data<Tera>) -> Result<impl Responder> {
let mut ctx = tera::Context::new();
ctx.insert("active_page", "editor");
let rendered = tmpl.render("editor.html", &ctx)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
/// Handles the home page route
pub async fn index(tmpl: web::Data<Tera>) -> Result<impl Responder> {
let mut ctx = tera::Context::new();
ctx.insert("active_page", "home");
// Example of using models in controllers
let example_user = User::new("John Doe".to_string(), "john@example.com".to_string());
ctx.insert("user", &example_user);
let rendered = tmpl.render("index.html", &ctx)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
/// Handles the about page route
pub async fn about(tmpl: web::Data<Tera>) -> Result<impl Responder> {
let mut ctx = tera::Context::new();
ctx.insert("active_page", "about");
let rendered = tmpl.render("about.html", &ctx)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
/// Handles the contact page route
pub async fn contact(tmpl: web::Data<Tera>) -> Result<impl Responder> {
let mut ctx = tera::Context::new();
ctx.insert("active_page", "contact");
let rendered = tmpl.render("contact.html", &ctx)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
/// Handles form submissions from the contact page
pub async fn submit_contact(
form: web::Form<ContactForm>,
tmpl: web::Data<Tera>
) -> Result<impl Responder> {
// In a real application, you would process the form data here
// For example, save it to a database or send an email
println!("Contact form submission: {:?}", form);
// For this example, we'll just redirect back to the contact page
// with a success message
let mut ctx = tera::Context::new();
ctx.insert("active_page", "contact");
ctx.insert("success_message", "Your message has been sent successfully!");
let rendered = tmpl.render("contact.html", &ctx)
.map_err(|e| {
eprintln!("Template rendering error: {}", e);
actix_web::error::ErrorInternalServerError("Template rendering error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered))
}
}
/// Represents the data submitted in the contact form
#[derive(Debug, serde::Deserialize)]
pub struct ContactForm {
pub name: String,
pub email: String,
pub subject: String,
pub message: String,
}