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.
96 lines
2.5 KiB
96 lines
2.5 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 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_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, 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)?,
|
|
page_name: row.get(1)?,
|
|
page_text: row.get(2)?,
|
|
})
|
|
})
|
|
.and_then(Iterator::collect)
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|
|
|
|
pub async fn update_page(
|
|
pool: &Pool,
|
|
domain: String,
|
|
page_name: String,
|
|
page_text: String,
|
|
) -> Result<usize, 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("insert or replace into pages (domain, page_name, page_text, active) values (?, ?, ?, true)")?;
|
|
stmt.execute([domain, page_name, page_text, ])
|
|
})
|
|
.await?
|
|
.map_err(error::ErrorInternalServerError)
|
|
}
|