The Basics of Building a Blazer Application: A Step-by-Step Guide
Building an application with Blazer can seem like a daunting task, but it's actually quite straightforward once you understand the basics. In this guide, we'll walk through the process of creating a simple Blazer application from scratch.
Creating a Student Data Model
-----------------------------
First things first, let's create our student data model. We want to store information about students in the database and display them on our index page. To do this, we need to define a struct that will hold our student data. This struct should have fields for the student's name, age, and birthday.
```blazer
struct Student {
name: String,
age: i32,
birthday: String,
}
```
Next, we'll create a database connection to store our students in. We'll use the `blazer::db` module to connect to our SQLite database.
```blazer
use blazer::db;
#[blazer]
fn main() {
let conn = db::connect("students.db").unwrap();
// ...
}
```
Now, we can create a function to insert new students into the database. This function will take in the student's data and store it in the database.
```blazer
fn add_student(conn: &db::Connection, name: String, age: i32, birthday: String) {
let mut stmt = conn.prepare("INSERT INTO students (name, age, birthday) VALUES (?, ?, ?)").unwrap();
stmt.execute(&[name, age, birthday]).unwrap();
}
```
Next, we'll create a function to retrieve all students from the database. This function will return a list of all students in the database.
```blazer
fn get_students(conn: &db::Connection) -> Vec
let mut stmt = conn.prepare("SELECT * FROM students").unwrap();
let rows = stmt.query().execute().unwrap();
let mut students = Vec::new();
for row in rows {
let student = Student {
name: row.get(0),
age: row.get(1),
birthday: row.get(2),
};
students.push(student);
}
students
}
```
Now, we can use these functions to add new students and retrieve all existing students. We'll create a `blazer::Component` to render the index page.
```blazer
#[blazer]
fn index_page(conn: &db::Connection) {
let students = get_students(conn);
// Render the index page with the list of students.
}
```
Creating an Interactive Form
-----------------------------
Now, we want to create an interactive form that allows users to add new students. To do this, we need to make our component page interactive.
```blazer
#[blazer]
fn create_page(conn: &db::Connection) {
let mut stmt = conn.prepare("INSERT INTO students (name, age, birthday) VALUES (?, ?, ?)").unwrap();
// Render the form and handle user input.
}
```
We can make our component page interactive by specifying the `render mode` as "interactive server".
```blazer
#[blazer]
fn main() {
#[blazer::main(page = "index")]
fn index_page(conn: &db::Connection) {
// ...
}
#[blazer::main(page = "create", render_mode = "interactive_server")]
fn create_page(conn: &db::Connection) {
// ...
}
}
```
Adding Validation
-----------------
Now, we want to add validation to our form. We can do this by checking the user input as soon as they click out of the form.
```blazer
#[blazer]
fn create_page(conn: &db::Connection) {
let mut stmt = conn.prepare("INSERT INTO students (name, age, birthday) VALUES (?, ?, ?)").unwrap();
// Render the form and handle user input.
if !validate_form() {
return Err(BlazerError::InvalidForm);
}
// Insert new student into database.
}
```
We can validate the form by checking that the age is between 18 and 35.
```blazer
fn validate_form() -> bool {
let mut stmt = conn.prepare("SELECT * FROM students WHERE name = ?").unwrap();
stmt.execute(&[""]).unwrap();
// ...
}
```
Deleting a Student
-----------------
Now, we want to delete a student. To do this, we need to create an edit page.
```blazer
#[blazer]
fn delete_page(conn: &db::Connection) {
let id = get_id_from_url();
// Retrieve the student with the specified ID.
}
```
We can retrieve the student with the specified ID by using a database query.
```blazer
fn get_id_from_url() -> i32 {
// Get the ID from the URL.
}
fn get_student(conn: &db::Connection, id: i32) -> Student {
let mut stmt = conn.prepare("SELECT * FROM students WHERE id = ?").unwrap();
stmt.execute(&[id]).unwrap();
// ...
}
```
Now, we can delete the student by removing them from the database.
```blazer
fn delete_student(conn: &db::Connection, id: i32) {
let mut stmt = conn.prepare("DELETE FROM students WHERE id = ?").unwrap();
stmt.execute(&[id]).unwrap();
}
```
Putting it All Together
------------------------
Now that we've created all the necessary components and functions, we can put everything together.
```blazer
#[blazer]
fn main() {
#[blazer::main(page = "index")]
fn index_page(conn: &db::Connection) {
// Render the index page with the list of students.
}
#[blazer::main(page = "create", render_mode = "interactive_server")]
fn create_page(conn: &db::Connection) {
let mut stmt = conn.prepare("INSERT INTO students (name, age, birthday) VALUES (?, ?, ?)").unwrap();
// Render the form and handle user input.
if !validate_form() {
return Err(BlazerError::InvalidForm);
}
// Insert new student into database.
}
#[blazer::main(page = "delete", render_mode = "interactive_server")]
fn delete_page(conn: &db::Connection) {
let id = get_id_from_url();
// Retrieve the student with the specified ID.
}
}
```
Conclusion
----------
In this guide, we've walked through the process of creating a simple Blazer application from scratch. We've created a student data model, added validation to our form, and deleted a student. This is just a basic example, but it should give you a good starting point for building your own Blazer applications.
Note: This is a simplified guide and might not cover all possible edge cases. You may need to add more error checking and handling code depending on your specific use case.