Web API Integration

We can wrap more of the backend's functionalities into our Rocket application.

Check Rocket's official documentation for how to use their interfaces.

Below are some examples: (Don't forget to mount all new handlers in rocket()!)

Fetch one Bakery by id

fn main() {
async fn bakery_by_id(db: &State<DatabaseConnection>, id: i32) -> Result<String, ErrorResponder> {
    let db = db as &DatabaseConnection;

    let bakery = Bakery::find_by_id(id).one(db).await.map_err(Into::into)?;

    Ok(if let Some(bakery) = bakery {
    } else {
        return Err(format!("No bakery with id {id} is found.").into());

Add a new Bakery

Query parameters are used for input here for simplicity. Alternatively, use Body Data.

fn main() {
use entities::*;

async fn new_bakery(
    db: &State<DatabaseConnection>,
    name: &str,
    profit_margin: Option<f64>,
) -> Result<(), ErrorResponder> {
    let db = db as &DatabaseConnection;

    let new_bakery = bakery::ActiveModel {
        name: ActiveValue::Set(name.to_owned()),
        profit_margin: ActiveValue::Set(profit_margin.unwrap_or_default()),