You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.7 KiB
138 lines
3.7 KiB
use actix_web::{error, web, Error};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
|
pub type Connection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
|
|
|
|
#[allow(unused)]
|
|
fn uuid4() -> String {
|
|
let id = Uuid::new_v4();
|
|
id.to_string()
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Page {
|
|
pub domain: String,
|
|
pub domain_name: String,
|
|
pub page_name: String,
|
|
pub page_text: String,
|
|
}
|
|
|
|
pub struct Domain {
|
|
pub domain: String,
|
|
pub domain_name: String,
|
|
}
|
|
|
|
pub async fn get_domains(pool: &Pool) -> Result<Vec<Domain>, Error> {
|
|
let pool = pool.clone();
|
|
|
|
let conn = web::block(move || pool.get())
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
web::block(move || {
|
|
let mut stmt = conn
|
|
.prepare("SELECT distinct domain, REPLACE(domain, '_', ' ') from pages WHERE active=true order by domain")?;
|
|
|
|
stmt.query_map([], |row| {
|
|
Ok(Domain {
|
|
domain: row.get(0)?,
|
|
domain_name: row.get(1)?,
|
|
})
|
|
})
|
|
.and_then(Iterator::collect)
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|
|
|
|
pub async fn get_pages_by_domain(
|
|
pool: &Pool,
|
|
domain: String,
|
|
) -> Result<Vec<Page>, Error> {
|
|
let pool = pool.clone();
|
|
|
|
let conn = web::block(move || pool.get())
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
web::block(move || {
|
|
let mut stmt = conn
|
|
.prepare("SELECT domain, REPLACE(domain, '_', ' '), page_name from pages WHERE active=true and domain=?")?;
|
|
|
|
stmt.query_map([domain], |row| {
|
|
Ok(Page {
|
|
domain: row.get(0)?,
|
|
domain_name: row.get(1)?,
|
|
page_name: row.get(2)?,
|
|
page_text: String::from(""),
|
|
})
|
|
})
|
|
.and_then(Iterator::collect)
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|
|
|
|
pub async fn get_page_by_name(
|
|
pool: &Pool,
|
|
domain: String,
|
|
pagename: String,
|
|
) -> Result<Vec<Page>, Error> {
|
|
let pool = pool.clone();
|
|
|
|
let conn = web::block(move || pool.get())
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
web::block(move || {
|
|
let mut stmt = conn
|
|
.prepare("SELECT domain, REPLACE(domain, '_', ' '), page_name, page_text from pages WHERE active=true and domain=? and page_name=?")?;
|
|
|
|
stmt.query_map([domain, pagename], |row| {
|
|
Ok(Page {
|
|
domain: row.get(0)?,
|
|
domain_name: row.get(1)?,
|
|
page_name: row.get(2)?,
|
|
page_text: row.get(3)?,
|
|
})
|
|
})
|
|
.and_then(Iterator::collect)
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|
|
|
|
pub async fn update_page(
|
|
pool: &Pool,
|
|
domain: String,
|
|
page_name: String,
|
|
page_text: String,
|
|
parent_domain: Option<&str>,
|
|
parent_page: Option<&str>
|
|
) -> Result<usize, Error> {
|
|
let pool = pool.clone();
|
|
|
|
let conn = web::block(move || pool.get())
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
let parent_domain = match parent_domain {
|
|
Some(pd) => pd,
|
|
None => ""
|
|
}.to_owned();
|
|
|
|
let parent_page = match parent_page {
|
|
Some(pp) => pp,
|
|
None => ""
|
|
}.to_owned();
|
|
|
|
web::block(move || {
|
|
let mut stmt = conn
|
|
.prepare("insert or replace into pages (domain, page_name, page_text, active, parent_domain, parent_page) values (?, ?, ?, true, ?, ?)")?;
|
|
stmt.execute([domain.to_owned(), page_name.to_owned(), page_text.to_owned(), parent_domain, parent_page ])
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|