refact: move views to each model file

This commit is contained in:
Kilerd Chan 2019-06-04 11:44:55 +08:00
parent 39bb64f195
commit b188629e7a
6 changed files with 42 additions and 35 deletions

View File

@ -29,7 +29,6 @@ mod pg_pool;
mod routers;
mod schema;
mod utils;
mod view;
embed_migrations!();

View File

@ -108,3 +108,35 @@ pub mod form {
}
}
}
pub mod view {
use crate::models::article::Article;
use pulldown_cmark::{html, Parser};
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct ArticleView<'a> {
pub article: &'a Article,
pub timestamp: i64,
pub markdown_content: String,
pub description: String,
}
impl<'a> ArticleView<'a> {
pub fn from(article: &'a Article) -> ArticleView {
let content_split: Vec<_> = article.body.split("<!--more-->").collect();
let description_parser = Parser::new(&content_split[0]);
let parser = Parser::new(&article.body);
let mut description_buf = String::new();
let mut content_buf = String::new();
html::push_html(&mut content_buf, parser);
html::push_html(&mut description_buf, description_parser);
ArticleView {
article,
timestamp: article.publish_at.timestamp(),
markdown_content: content_buf,
description: description_buf,
}
}
}
}

View File

@ -1,9 +1,12 @@
use crate::{
data::RubbleData,
models::{article::Article, setting::Setting, CRUD},
models::{
article::{view::ArticleView, Article},
setting::Setting,
CRUD,
},
pg_pool::Pool,
routers::RubbleResponder,
view::article::ArticleView,
};
use actix_web::{get, web, Either, HttpResponse, Responder};
use std::{result::Result, sync::Arc};

View File

@ -1,8 +1,11 @@
use crate::{
data::RubbleData,
models::{article::Article, setting::Setting, CRUD},
models::{
article::{view::ArticleView, Article},
setting::Setting,
CRUD,
},
pg_pool::Pool,
view::article::ArticleView,
};
use actix_web::{get, web, HttpResponse, Responder};
use rss::{Channel, ChannelBuilder, Item, ItemBuilder};

View File

@ -1,29 +0,0 @@
use crate::models::article::Article;
use pulldown_cmark::{html, Parser};
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct ArticleView<'a> {
pub article: &'a Article,
pub timestamp: i64,
pub markdown_content: String,
pub description: String,
}
impl<'a> ArticleView<'a> {
pub fn from(article: &'a Article) -> ArticleView {
let content_split: Vec<_> = article.body.split("<!--more-->").collect();
let description_parser = Parser::new(&content_split[0]);
let parser = Parser::new(&article.body);
let mut description_buf = String::new();
let mut content_buf = String::new();
html::push_html(&mut content_buf, parser);
html::push_html(&mut description_buf, description_parser);
ArticleView {
article,
timestamp: article.publish_at.timestamp(),
markdown_content: content_buf,
description: description_buf,
}
}
}

View File

@ -1 +0,0 @@
pub mod article;