....
This commit is contained in:
88
actix_mvc_app/src/controllers/home.rs
Normal file
88
actix_mvc_app/src/controllers/home.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
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 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,
|
||||
}
|
5
actix_mvc_app/src/controllers/mod.rs
Normal file
5
actix_mvc_app/src/controllers/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// Export controllers
|
||||
pub mod home;
|
||||
|
||||
// Re-export controllers for easier imports
|
||||
pub use home::HomeController;
|
Reference in New Issue
Block a user