pub trait ModelController: Controller {
type Model: Model + Serialize + Send + Sync + for<'a> Deserialize<'a>;
// Provided methods
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn crud(self, path: &str) -> Handler
where Self: Sized + 'static { ... }
fn list<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn get<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn create<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn update<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn patch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}
Expand description
A controller that extends the RestController
to
automatically performs CRUD actions on database models.
§Example
use serde::{Serialize, Deserialize};
// The database model.
#[derive(Clone, macros::Model, Serialize, Deserialize)]
struct User {
id: Option<i64>,
email: String,
}
// The controller.
#[derive(macros::ModelController)]
struct UserController;
#[rwf::async_trait]
impl ModelController for UserController {
type Model = User;
}
Required Associated Types§
Provided Methods§
Sourcefn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Handle the request to this controller.
Sourcefn crud(self, path: &str) -> Handlerwhere
Self: Sized + 'static,
fn crud(self, path: &str) -> Handlerwhere
Self: Sized + 'static,
Returns the controller route handler. Used when mapping this controller to a path in the server.
Use crud!
instead:
ⓘ
crud!("/path" => MyController)
Sourcefn list<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn list<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
List all records for the model. Supports pagination with page
parameter. Supports number of records per page with page_size
parameter.
§Example
GET /users?page=3&page_size=40
Sourcefn get<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn get<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Fetch a model record identified by its primary key.
Sourcefn create<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn create<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Create new model record.
Sourcefn update<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn update<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Update existing model record.
Sourcefn patch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn patch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 i64,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Partially update an existing model record.