Use compact entity format for Rocket example
This commit is contained in:
parent
28b7542ca9
commit
77bc11e1e5
@ -10,7 +10,7 @@ use rocket::{Build, Request, Rocket};
|
|||||||
use rocket_db_pools::{sqlx, Connection, Database};
|
use rocket_db_pools::{sqlx, Connection, Database};
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
|
|
||||||
use sea_orm::entity::*;
|
use sea_orm::{entity::*, query::*};
|
||||||
|
|
||||||
mod pool;
|
mod pool;
|
||||||
use pool::RocketDbPool;
|
use pool::RocketDbPool;
|
||||||
@ -81,7 +81,9 @@ async fn list(
|
|||||||
) -> Template {
|
) -> Template {
|
||||||
let page = page.unwrap_or(0);
|
let page = page.unwrap_or(0);
|
||||||
let posts_per_page = posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE);
|
let posts_per_page = posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE);
|
||||||
let paginator = Post::find().paginate(&conn, posts_per_page);
|
let paginator = Post::find()
|
||||||
|
.order_by_asc(post::Column::Id)
|
||||||
|
.paginate(&conn, posts_per_page);
|
||||||
let num_pages = paginator.num_pages().await.ok().unwrap();
|
let num_pages = paginator.num_pages().await.ok().unwrap();
|
||||||
|
|
||||||
let posts = paginator
|
let posts = paginator
|
||||||
|
@ -1,65 +1,17 @@
|
|||||||
use rocket::serde::{Deserialize, Serialize};
|
use rocket::serde::{Deserialize, Serialize};
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity, Deserialize, Serialize)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, FromForm)]
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
pub struct Entity;
|
|
||||||
|
|
||||||
impl EntityName for Entity {
|
|
||||||
fn table_name(&self) -> &str {
|
|
||||||
"posts"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(
|
|
||||||
Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Deserialize, Serialize, FromForm,
|
|
||||||
)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
#[serde(crate = "rocket::serde")]
|
||||||
|
#[sea_orm(table_name = "posts")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[serde(skip_deserializing, skip_serializing_if = "Option::is_none")]
|
#[sea_orm(primary_key)]
|
||||||
pub id: Option<i32>,
|
pub id: i32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Column {
|
|
||||||
Id,
|
|
||||||
Title,
|
|
||||||
Text,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
|
||||||
pub enum PrimaryKey {
|
|
||||||
Id,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrimaryKeyTrait for PrimaryKey {
|
|
||||||
type ValueType = i32;
|
|
||||||
|
|
||||||
fn auto_increment() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
impl ColumnTrait for Column {
|
|
||||||
type EntityName = Entity;
|
|
||||||
|
|
||||||
fn def(&self) -> ColumnDef {
|
|
||||||
match self {
|
|
||||||
Self::Id => ColumnType::Integer.def(),
|
|
||||||
Self::Title => ColumnType::String(None).def(),
|
|
||||||
Self::Text => ColumnType::String(None).def(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RelationTrait for Relation {
|
|
||||||
fn def(&self) -> RelationDef {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
@ -5,6 +5,12 @@
|
|||||||
<div class="ten columns">
|
<div class="ten columns">
|
||||||
<form action="/{{ post.id }}" method="post">
|
<form action="/{{ post.id }}" method="post">
|
||||||
<div class="twelve columns">
|
<div class="twelve columns">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="id"
|
||||||
|
id="id"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="title"
|
placeholder="title"
|
||||||
|
@ -3,6 +3,12 @@
|
|||||||
<h4>New Post</h4>
|
<h4>New Post</h4>
|
||||||
<form action="/" method="post">
|
<form action="/" method="post">
|
||||||
<div class="twelve columns">
|
<div class="twelve columns">
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="id"
|
||||||
|
id="id"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="enter title"
|
placeholder="enter title"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user