Add quote block
parent
c669c306e6
commit
0ce8ddd96e
@ -1,30 +1,96 @@
|
||||
use log::warn;
|
||||
use sea_orm::{DatabaseConnection, EntityTrait};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::entities::image;
|
||||
use crate::entities::{prelude::*, *};
|
||||
|
||||
pub enum BlockTypes {
|
||||
HR,
|
||||
HEADER {
|
||||
text: String,
|
||||
size: i8,
|
||||
},
|
||||
PARAGRAPH {
|
||||
text: String,
|
||||
},
|
||||
MARKDOWN {
|
||||
content: String,
|
||||
},
|
||||
HTML {
|
||||
content: String,
|
||||
},
|
||||
IMAGE {
|
||||
content: Option<image::Model>
|
||||
},
|
||||
UNSUPPORTED
|
||||
HEADER { id: String, text: String, size: i8 },
|
||||
PARAGRAPH { text: String },
|
||||
MARKDOWN { content: String },
|
||||
HTML { content: String },
|
||||
IMAGE { content: Option<image::Model> },
|
||||
QUOTE { quote: String, caption: String },
|
||||
UNSUPPORTED,
|
||||
}
|
||||
|
||||
// Database JSON types
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Header {
|
||||
pub id: String,
|
||||
pub text: String,
|
||||
pub size: i8,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Quote {
|
||||
pub quote: String,
|
||||
pub caption: String,
|
||||
}
|
||||
|
||||
// Content block handling
|
||||
|
||||
pub struct GenericBlock {
|
||||
pub id: String,
|
||||
pub owner: String,
|
||||
pub order: i32,
|
||||
pub block_type: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
pub async fn generate_content_blocks(
|
||||
db_conn: &DatabaseConnection,
|
||||
blocks: Vec<GenericBlock>,
|
||||
) -> Vec<BlockTypes> {
|
||||
let mut content_blocks: Vec<BlockTypes> = vec![];
|
||||
|
||||
for block in blocks {
|
||||
content_blocks.push(match block.block_type.as_str() {
|
||||
"HR" => BlockTypes::HR,
|
||||
"PARAGRAPH" => BlockTypes::PARAGRAPH {
|
||||
text: block.content,
|
||||
},
|
||||
"MARKDOWN" => BlockTypes::MARKDOWN {
|
||||
content: block.content,
|
||||
},
|
||||
"HEADER" => {
|
||||
let deserde: Header = serde_json::from_str(block.content.as_str())
|
||||
.expect("Incorrect HEADER block formatting");
|
||||
|
||||
BlockTypes::HEADER {
|
||||
id: deserde.id,
|
||||
text: deserde.text,
|
||||
size: deserde.size,
|
||||
}
|
||||
}
|
||||
"HTML" => BlockTypes::HTML {
|
||||
content: block.content,
|
||||
},
|
||||
"IMAGE" => BlockTypes::IMAGE {
|
||||
content: {
|
||||
Image::find_by_id(block.content)
|
||||
.one(db_conn)
|
||||
.await
|
||||
.expect("Failed to get image! HALTING")
|
||||
},
|
||||
},
|
||||
"QUOTE" => {
|
||||
let deserde: Quote = serde_json::from_str(block.content.as_str())
|
||||
.expect("Incorrect QUOTE block formatting");
|
||||
|
||||
BlockTypes::QUOTE {
|
||||
quote: deserde.quote,
|
||||
caption: deserde.caption,
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
warn!("Unsupported block type! ({})", block.block_type.as_str());
|
||||
BlockTypes::UNSUPPORTED
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
content_blocks
|
||||
}
|
||||
|
Loading…
Reference in New Issue