2022-08-27 14:41:08 +02:00
|
|
|
use sea_orm::{ConnectOptions, DatabaseConnection};
|
|
|
|
use actix_admin::prelude::*;
|
2022-08-28 17:06:23 +02:00
|
|
|
use actix_web::Error;
|
|
|
|
use actix_session::Session;
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use actix_web::{web};
|
2022-08-27 14:41:08 +02:00
|
|
|
|
|
|
|
use super::{Post, Comment, create_tables};
|
|
|
|
|
|
|
|
pub async fn create_tables_and_get_connection() -> DatabaseConnection {
|
|
|
|
let opt = ConnectOptions::new("sqlite::memory:".to_owned());
|
|
|
|
|
|
|
|
let conn = sea_orm::Database::connect(opt).await.unwrap();
|
|
|
|
let _ = create_tables(&conn).await;
|
|
|
|
|
|
|
|
conn
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppState {
|
|
|
|
pub db: DatabaseConnection,
|
|
|
|
pub actix_admin: ActixAdmin,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActixAdminAppDataTrait for AppState {
|
|
|
|
fn get_db(&self) -> &DatabaseConnection {
|
|
|
|
&self.db
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_actix_admin(&self) -> &ActixAdmin {
|
|
|
|
&self.actix_admin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_actix_admin_builder() -> ActixAdminBuilder {
|
|
|
|
let post_view_model = ActixAdminViewModel::from(Post);
|
|
|
|
let comment_view_model = ActixAdminViewModel::from(Comment);
|
|
|
|
|
|
|
|
let configuration = ActixAdminConfiguration {
|
|
|
|
enable_auth: false,
|
|
|
|
user_is_logged_in: None,
|
|
|
|
login_link: None,
|
|
|
|
logout_link: None,
|
2022-12-29 19:56:20 +01:00
|
|
|
file_upload_directory: "./file_uploads"
|
2022-08-27 14:41:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut admin_builder = ActixAdminBuilder::new(configuration);
|
|
|
|
admin_builder.add_entity::<AppState, Post>(&post_view_model);
|
|
|
|
admin_builder.add_entity::<AppState, Comment>(&comment_view_model);
|
|
|
|
|
2022-08-28 17:06:23 +02:00
|
|
|
admin_builder.add_custom_handler_for_entity::<AppState, Comment>(
|
2022-09-29 16:23:49 +02:00
|
|
|
"Create Post From Plaintext",
|
2022-08-28 20:26:10 +02:00
|
|
|
"/create_post_from_plaintext",
|
2022-10-11 18:54:45 +02:00
|
|
|
web::post().to(create_post_from_plaintext::<AppState, Comment>), false);
|
2022-08-28 17:06:23 +02:00
|
|
|
|
|
|
|
admin_builder.add_custom_handler_for_entity::<AppState, Post>(
|
2022-09-29 16:23:49 +02:00
|
|
|
"Create Post From Plaintext",
|
2022-08-28 17:06:23 +02:00
|
|
|
"/create_post_from_plaintext",
|
2022-10-11 18:54:45 +02:00
|
|
|
web::post().to(create_post_from_plaintext::<AppState, Post>), false);
|
2022-08-28 17:06:23 +02:00
|
|
|
|
|
|
|
admin_builder.add_custom_handler_for_entity::<AppState, Post>(
|
2022-09-29 16:23:49 +02:00
|
|
|
"Create Post From Plaintext",
|
2022-08-28 17:06:23 +02:00
|
|
|
"/edit_post_from_plaintext/{id}",
|
2022-10-11 18:54:45 +02:00
|
|
|
web::post().to(edit_post_from_plaintext::<AppState, Post>), false);
|
2022-08-28 17:06:23 +02:00
|
|
|
|
|
|
|
admin_builder.add_custom_handler_for_entity::<AppState, Comment>(
|
2022-09-29 16:23:49 +02:00
|
|
|
"Create Post From Plaintext",
|
2022-08-28 20:26:10 +02:00
|
|
|
"/edit_post_from_plaintext/{id}",
|
2022-10-11 18:54:45 +02:00
|
|
|
web::post().to(edit_post_from_plaintext::<AppState, Comment>), false);
|
2022-08-28 17:06:23 +02:00
|
|
|
|
2022-08-27 14:41:08 +02:00
|
|
|
admin_builder
|
|
|
|
}
|
2022-08-28 17:06:23 +02:00
|
|
|
|
|
|
|
async fn create_post_from_plaintext<
|
|
|
|
T: ActixAdminAppDataTrait,
|
|
|
|
E: ActixAdminViewModelTrait,
|
|
|
|
>(
|
|
|
|
session: Session,
|
|
|
|
data: web::Data<T>,
|
|
|
|
text: String,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2022-12-29 19:56:20 +01:00
|
|
|
let actix_admin = data.get_actix_admin();
|
2022-08-28 17:06:23 +02:00
|
|
|
let model = ActixAdminModel::from(text);
|
2022-12-29 19:56:20 +01:00
|
|
|
create_or_edit_post::<T, E>(&session, &data, Ok(model), None, actix_admin).await
|
2022-08-28 17:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn edit_post_from_plaintext<
|
|
|
|
T: ActixAdminAppDataTrait,
|
|
|
|
E: ActixAdminViewModelTrait,
|
|
|
|
>(
|
|
|
|
session: Session,
|
|
|
|
data: web::Data<T>,
|
|
|
|
text: String,
|
|
|
|
id: web::Path<i32>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2022-12-29 19:56:20 +01:00
|
|
|
let actix_admin = data.get_actix_admin();
|
2022-08-28 17:06:23 +02:00
|
|
|
let model = ActixAdminModel::from(text);
|
2022-12-29 19:56:20 +01:00
|
|
|
create_or_edit_post::<T, E>(&session, &data, Ok(model), Some(id.into_inner()), actix_admin).await
|
2022-08-28 17:06:23 +02:00
|
|
|
}
|