create single route with dynamic entity param
This commit is contained in:
parent
60797bd9eb
commit
50098f93dd
@ -1,4 +1,4 @@
|
||||
use actix_web::{error, web, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{error, web, Responder, get, post, route, Error, HttpRequest, HttpResponse};
|
||||
use lazy_static::lazy_static;
|
||||
use sea_orm::{ DatabaseConnection, ModelTrait, ConnectionTrait};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -6,8 +6,12 @@ use std::collections::HashMap;
|
||||
use actix_web::http::header;
|
||||
use tera::{Context, Tera};
|
||||
use std::any::Any;
|
||||
|
||||
use std::convert::From;
|
||||
use async_trait::async_trait;
|
||||
use sea_orm::ActiveValue::Set;
|
||||
use sea_orm::{ConnectOptions };
|
||||
use sea_orm::{entity::*, query::*};
|
||||
use sea_orm::EntityTrait;
|
||||
|
||||
pub use actix_admin_macros::DeriveActixAdminModel;
|
||||
|
||||
@ -67,30 +71,34 @@ pub struct ActixAdminViewModel {
|
||||
// ActixAdminController
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ActixAdmin {
|
||||
pub entity_names: Vec<String>
|
||||
pub entity_names: Vec<String>,
|
||||
pub view_models: HashMap<String, ActixAdminViewModel>
|
||||
}
|
||||
|
||||
impl ActixAdmin {
|
||||
pub fn new() -> Self {
|
||||
let actix_admin = ActixAdmin {
|
||||
entity_names: Vec::new()
|
||||
entity_names: Vec::new(),
|
||||
view_models: HashMap::new()
|
||||
};
|
||||
actix_admin
|
||||
}
|
||||
|
||||
pub fn add_entity<T: AppDataTrait + 'static>(mut self, view_model: &ActixAdminViewModel) -> Self {
|
||||
pub fn add_entity(mut self, view_model: &ActixAdminViewModel) -> Self {
|
||||
self.entity_names.push(view_model.entity_name.to_string());
|
||||
let view_model_cloned = view_model.clone();
|
||||
let key = view_model.entity_name.to_string();
|
||||
self.view_models.insert(key, view_model_cloned);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn create_scope<T: AppDataTrait + 'static>(&self) -> actix_web::Scope {
|
||||
pub fn create_scope<T: AppDataTrait + 'static >(&self) -> actix_web::Scope {
|
||||
let scope = web::scope("/admin").route("/", web::get().to(index::<T>));
|
||||
scope
|
||||
}
|
||||
}
|
||||
|
||||
async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> {
|
||||
|
||||
pub async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> {
|
||||
let entity_names = &data.get_actix_admin().entity_names;
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("entity_names", &entity_names);
|
||||
@ -101,6 +109,18 @@ async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Erro
|
||||
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> {
|
||||
let entity_name: String = path.into_inner();
|
||||
let actix_admin = data.get_actix_admin();
|
||||
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
|
||||
let entity_names = &data.get_actix_admin().entity_names;
|
||||
|
||||
let db = data.get_db();
|
||||
//let entities: Vec<&str> = Vec::new(); // E::list_db(db, 1, 5);
|
||||
// TODO: Get ViewModel from ActixAdmin to honor individual settings
|
||||
list_model(req, &data, view_model.clone(), entity_names)
|
||||
}
|
||||
|
||||
pub fn list_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel, entity_names: &Vec<String>) -> Result<HttpResponse, Error> {
|
||||
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
|
||||
|
||||
@ -123,6 +143,19 @@ pub fn list_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_m
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(body))
|
||||
}
|
||||
|
||||
pub async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, mut body: web::Payload, text: String, entity_name: web::Path<(String)>) -> impl Responder {
|
||||
let db = &data.get_db();
|
||||
let entity_name: String = entity_name.into_inner();
|
||||
println!("{}", &entity_name);
|
||||
let entity_names = &data.get_actix_admin().entity_names;
|
||||
// TODO: Get ViewModel from ActixAdmin to honor individual settings
|
||||
let actix_admin = data.get_actix_admin();
|
||||
|
||||
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
|
||||
|
||||
create_get_model(req, &data, view_model.clone(), entity_names)
|
||||
}
|
||||
|
||||
pub fn create_get_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel, entity_names: &Vec<String>) -> Result<HttpResponse, Error> {
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("entity_names", &entity_names);
|
||||
@ -135,6 +168,27 @@ pub fn create_get_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>,
|
||||
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> {
|
||||
let db = &data.get_db();
|
||||
let entity_name: String = entity_name.into_inner();
|
||||
let actix_admin = data.get_actix_admin();
|
||||
|
||||
let entity_names = &actix_admin.entity_names;
|
||||
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
|
||||
|
||||
println!("{}", &entity_name);
|
||||
println!("{}", &text);
|
||||
//println!("{}", &body.);
|
||||
// let new_model = ActiveModel {
|
||||
// title: Set("test".to_string()),
|
||||
// text: Set("test".to_string()),
|
||||
// ..Default::default()
|
||||
// };
|
||||
// let insert_operation = M::insert(new_model).exec(data.get_db()).await;
|
||||
|
||||
create_post_model(req, &data, view_model.clone())
|
||||
}
|
||||
|
||||
pub fn create_post_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Found()
|
||||
.append_header((header::LOCATION, format!("/admin/{}/list", view_model.entity_name)))
|
||||
|
15
src/main.rs
15
src/main.rs
@ -2,7 +2,7 @@ extern crate serde_derive;
|
||||
|
||||
use actix_admin::{
|
||||
ActixAdmin, ActixAdminViewModel, ActixAdminViewModelTrait,
|
||||
AppDataTrait as ActixAdminAppDataTrait,
|
||||
AppDataTrait as ActixAdminAppDataTrait, ActixAdminModelTrait
|
||||
};
|
||||
use actix_session::{CookieSession, Session};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, middleware};
|
||||
@ -15,6 +15,7 @@ use sea_orm::EntityTrait;
|
||||
use std::env;
|
||||
use std::time::Duration;
|
||||
use tera::{Context, Tera};
|
||||
use std::sync::Arc;
|
||||
|
||||
mod entity;
|
||||
use entity::{Comment, Post, comment, post};
|
||||
@ -65,12 +66,12 @@ fn setup_actix_admin(
|
||||
actix_admin
|
||||
.create_scope::<AppState>()
|
||||
.service(
|
||||
web::scope(&format!("/{}", post_view_model.entity_name))
|
||||
.route("/list", web::get().to(Post::list::<AppState>))
|
||||
.route("/create", web::get().to(Post::create_get::<AppState>))
|
||||
.route("/create", web::post().to(Post::create_post::<AppState, post::Model>))
|
||||
|
||||
web::scope("/{entity_name}")
|
||||
.route("/list", web::get().to(actix_admin::list::<AppState>))
|
||||
.route("/create", web::get().to(actix_admin::create_get::<AppState>))
|
||||
.route("/create", web::post().to(actix_admin::create_post::<AppState>))
|
||||
)
|
||||
|
||||
// .service(
|
||||
// web::scope(&format!("/{}", comment_view_model.entity_name))
|
||||
// .route("/list", web::get().to(Comment::list::<AppState>))
|
||||
@ -116,7 +117,7 @@ async fn main() {
|
||||
let post_view_model = ActixAdminViewModel::from(Post);
|
||||
//let comment_view_model = ActixAdminViewModel::from(Comment);
|
||||
let actix_admin = ActixAdmin::new()
|
||||
.add_entity::<AppState>(&post_view_model)
|
||||
.add_entity(&post_view_model)
|
||||
//.add_entity::<AppState>(&comment_view_model)
|
||||
;
|
||||
let app_state = AppState {
|
||||
|
Loading…
Reference in New Issue
Block a user