working list impl

This commit is contained in:
manuel 2022-05-22 22:00:33 +02:00
parent dc60135cb9
commit a22195d5d8
6 changed files with 20 additions and 15 deletions

View File

@ -37,13 +37,12 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
#[async_trait(?Send)] #[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity { impl ActixAdminViewModelTrait for Entity {
async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> { async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
let model = ActixAdminViewModel::from(Entity);
let entities = Entity::list_model(db, 1, 5).await; let entities = Entity::list_model(db, 1, 5).await;
entities entities
} }
async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel { async fn create_entity(db: &DatabaseConnection, mut model: ActixAdminModel) -> ActixAdminModel {
let new_model = ActiveModel { let new_model = ActiveModel {
title: Set("test".to_string()), title: Set("test".to_string()),
text: Set("test".to_string()), text: Set("test".to_string()),
@ -51,7 +50,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
}; };
let insert_operation = Entity::insert(new_model).exec(db).await; let insert_operation = Entity::insert(new_model).exec(db).await;
ActixAdminModel{ values: HashMap::new() } model
} }
} }

View File

@ -51,8 +51,8 @@ pub struct ActixAdminModel {
// ActixAdminViewModel // ActixAdminViewModel
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait ActixAdminViewModelTrait { pub trait ActixAdminViewModelTrait {
async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel>; async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel>;
async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel; async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
@ -102,7 +102,7 @@ pub async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse,
Ok(HttpResponse::Ok().content_type("text/html").body(body)) Ok(HttpResponse::Ok().content_type("text/html").body(body))
} }
pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: web::Path<String>) -> Result<HttpResponse, Error> { pub async fn list<T: AppDataTrait, E: ActixAdminViewModelTrait>(req: HttpRequest, data: web::Data<T>, path: web::Path<String>) -> Result<HttpResponse, Error> {
let entity_name: String = path.into_inner(); let entity_name: String = path.into_inner();
let actix_admin = data.get_actix_admin(); let actix_admin = data.get_actix_admin();
let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap(); let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap();
@ -114,7 +114,7 @@ pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: w
let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE); let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
let db = data.get_db(); let db = data.get_db();
let entities: Vec<ActixAdminModel> = Vec::new(); // entity.list(db, page, entities_per_page).await; let entities: Vec<ActixAdminModel> = E::list(db, page, entities_per_page).await;
let mut ctx = Context::new(); let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names); ctx.insert("entity_names", &entity_names);
@ -130,7 +130,7 @@ pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: w
Ok(HttpResponse::Ok().content_type("text/html").body(body)) Ok(HttpResponse::Ok().content_type("text/html").body(body))
} }
pub async fn create_get<T: AppDataTrait>(_req: HttpRequest, data: web::Data<T>, _body: web::Payload, _text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> { pub async fn create_get<T: AppDataTrait, E: ActixAdminViewModelTrait>(_req: HttpRequest, data: web::Data<T>, _body: web::Payload, _text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> {
let _db = &data.get_db(); let _db = &data.get_db();
let entity_name: String = entity_name.into_inner(); let entity_name: String = entity_name.into_inner();
println!("{}", &entity_name); println!("{}", &entity_name);
@ -151,12 +151,14 @@ pub async fn create_get<T: AppDataTrait>(_req: HttpRequest, data: web::Data<T>,
Ok(HttpResponse::Ok().content_type("text/html").body(body)) Ok(HttpResponse::Ok().content_type("text/html").body(body))
} }
pub async fn create_post<T: AppDataTrait>(_req: HttpRequest, data: web::Data<T>, text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> { pub async fn create_post<T: AppDataTrait, E: ActixAdminViewModelTrait>(_req: HttpRequest, data: web::Data<T>, text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> {
let _db = &data.get_db(); let db = &data.get_db();
let entity_name: String = entity_name.into_inner(); let entity_name: String = entity_name.into_inner();
let actix_admin = data.get_actix_admin(); let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap(); let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let mut model = ActixAdminModel{ values: HashMap::new() };
model = E::create_entity(db, model).await;
println!("{}", &entity_name); println!("{}", &entity_name);
println!("{}", &text); println!("{}", &text);

View File

@ -8,8 +8,12 @@
<th>{{ model_field[0] }}</th> <th>{{ model_field[0] }}</th>
{%- endfor %} {%- endfor %}
</tr> </tr>
{% for entity in entities -%}
<tr> <tr>
<td></td> {% for model_field in view_model.fields -%}
<td>{{ entity.values | get(key=model_field[0]) }}</td>
{%- endfor %}
</tr> </tr>
{%- endfor %}
</table> </table>
{% endblock content %} {% endblock content %}

Binary file not shown.

Binary file not shown.

View File

@ -64,9 +64,9 @@ fn setup_actix_admin(
.create_scope::<AppState>() .create_scope::<AppState>()
.service( .service(
web::scope("/{entity_name}") web::scope("/{entity_name}")
.route("/list", web::get().to(actix_admin::list::<AppState>)) .route("/list", web::get().to(actix_admin::list::<AppState, Post>))
.route("/create", web::get().to(actix_admin::create_get::<AppState>)) .route("/create", web::get().to(actix_admin::create_get::<AppState, Post>))
.route("/create", web::post().to(actix_admin::create_post::<AppState>)) .route("/create", web::post().to(actix_admin::create_post::<AppState, Post>))
) )
} }