Skip to content

Commit 5157857

Browse files
committed
feat: update example with new api
1 parent d483c4a commit 5157857

File tree

8 files changed

+92
-82
lines changed

8 files changed

+92
-82
lines changed

examples/ecom/Cargo.lock

Lines changed: 32 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ecom/awto/database/build.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
use std::env;
1+
use std::{env, error};
22

3-
use awto::schema::Schema;
3+
use awto::schema::Role;
44
use awto_compile::database::compile_database;
55

66
#[tokio::main]
7-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
async fn main() -> Result<(), Box<dyn error::Error>> {
88
dotenv::dotenv().ok();
9+
910
let pg_schema = env::var("DATABASE_SCHEMA").unwrap_or_else(|_| "public".to_string());
1011
let uri = env::var("DATABASE_URL").expect("missing env DATABASE_URL");
1112

12-
compile_database(&uri, schema::Schema::database_schemas()).await?;
13+
compile_database(&uri, schema::MODELS.to_vec()).await?;
1314

1415
sea_orm_build::generate_models(
1516
&pg_schema,
1617
&uri,
17-
&schema::Schema::database_schemas()
18-
.iter()
19-
.map(|schema| schema.table_name.as_str())
20-
.collect::<Vec<_>>(),
18+
&schema::MODELS.iter().fold(Vec::new(), |mut acc, model| {
19+
for role in &model.roles {
20+
if let Role::DatabaseTable(table) = role {
21+
acc.push(table.name.as_str())
22+
}
23+
}
24+
25+
acc
26+
}),
2127
)
2228
.await?;
2329

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
// This file is automatically @generated by awto-cli v0.1.0
1+
// This file is automatically @generated by awto-cli v0.1.1
22

33
pub use sea_orm;
44

55
include!(concat!(env!("OUT_DIR"), "/app.rs"));
6-
7-
/// Product database model
8-
pub mod product {
9-
sea_orm::include_model!("product");
10-
}

examples/ecom/awto/protobuf/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use awto::{schema::Schema, service::Service};
1+
use awto::service::Service;
22
use awto_compile::protobuf::compile_protobuf;
33

44
fn main() -> Result<(), Box<dyn std::error::Error>> {
55
compile_protobuf(
6-
schema::Schema::protobuf_schemas(),
6+
schema::MODELS.to_vec(),
77
service::Service::protobuf_services(),
88
)
99
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// This file is automatically @generated by awto-cli v0.1.0
1+
// This file is automatically @generated by awto-cli v0.1.1
22

33
include!(concat!(env!("OUT_DIR"), "/app.rs"));

examples/ecom/schema/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ edition = "2018"
55

66
[dependencies]
77
awto = { path = "../../../awto" }
8-
chrono = "0.4"
9-
uuid = { version = "0.8", features = ["v4"] }

examples/ecom/schema/src/lib.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
use awto::prelude::*;
2-
use chrono::{DateTime, FixedOffset};
3-
use uuid::Uuid;
42

5-
register_schemas!(Product);
3+
schema! {
4+
#[database_table]
5+
#[protobuf_message]
6+
pub struct Product {
7+
pub id: Uuid,
8+
pub created_at: DateTime<FixedOffset>,
9+
pub updated_at: DateTime<FixedOffset>,
10+
pub name: String,
11+
#[awto(default = 0)]
12+
pub price: i64,
13+
#[awto(max_len = 120)]
14+
pub description: Option<String>,
15+
pub category: Option<String>,
16+
}
617

7-
#[derive(Model)]
8-
pub struct Product {
9-
pub id: Uuid,
10-
pub created_at: DateTime<FixedOffset>,
11-
pub updated_at: DateTime<FixedOffset>,
12-
pub name: String,
13-
#[awto(default = 0)]
14-
pub price: i64,
15-
#[awto(max_len = 120)]
16-
pub description: Option<String>,
17-
pub category: Option<String>,
18-
}
18+
#[protobuf_message]
19+
pub struct Empty {}
1920

20-
#[derive(ProtobufModel)]
21-
pub struct Empty {}
21+
#[protobuf_message]
22+
pub struct ProductId {
23+
pub id: Uuid,
24+
}
2225

23-
#[derive(ProtobufModel)]
24-
pub struct ProductId {
25-
pub id: Uuid,
26-
}
26+
#[protobuf_message]
27+
#[database_sub_table(Product)]
28+
pub struct NewProduct {
29+
pub name: String,
30+
pub price: Option<i64>,
31+
pub description: Option<String>,
32+
pub category: Option<String>,
33+
}
2734

28-
#[derive(ProtobufModel)]
29-
pub struct NewProduct {
30-
pub name: String,
31-
pub price: Option<i64>,
32-
pub description: Option<String>,
33-
pub category: Option<String>,
35+
#[protobuf_message]
36+
pub struct ProductList {
37+
pub products: Vec<Product>,
38+
}
3439
}
3540

36-
#[derive(ProtobufModel)]
37-
pub struct ProductList {
38-
pub products: Vec<Product>,
41+
#[cfg(test)]
42+
mod test {
43+
use super::*;
44+
45+
#[test]
46+
fn models() {
47+
println!("{:#?}", &*MODELS);
48+
}
3949
}

examples/ecom/service/src/product.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use awto::macros::protobuf_service;
22
use database::{
33
product,
4-
sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveValue},
4+
sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel},
55
};
66
use schema::*;
77
use tonic::Status;
@@ -13,13 +13,7 @@ pub struct ProductService {
1313
#[protobuf_service]
1414
impl ProductService {
1515
pub async fn create_product(&self, request: NewProduct) -> Result<ProductId, Status> {
16-
let active_model = product::ActiveModel {
17-
name: request.name.into_active_value(),
18-
price: request.price.unwrap_or_default().into_active_value(),
19-
description: request.description.into_active_value(),
20-
category: request.category.into_active_value(),
21-
..Default::default()
22-
};
16+
let active_model = request.into_active_model();
2317

2418
let result = active_model
2519
.insert(&self.conn)

0 commit comments

Comments
 (0)