1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use actix_session::Session;
use actix_web::{web, HttpRequest, HttpResponse};

use dbui_core::cfg::AppConfig;
use dbui_core::ctx::RequestContext;
use dbui_core::{Error, Result, ResultExt};

use crate::util::forms::ProfileForm;
use dbui_core::profile::UserProfile;

pub fn index(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| dbui_templates::home::index(&ctx))
}

pub fn health() -> HttpResponse {
  HttpResponse::Ok().finish()
}

pub fn profile(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| dbui_templates::profile::profile(&ctx))
}

pub fn profile_post(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest, form: Option<web::Form<ProfileForm>>) -> HttpResponse {
  match form {
    Some(f) => crate::redir(&session, &cfg, &req, |ctx| {
      let profile = UserProfile::new(
        String::from(f.username()),
        f.theme(),
        f.navbar_color().into(),
        f.link_color().into()
      );
      cfg.files().write_json(profile, "profile")?;
      ctx.router().route_simple("profile")
    }),
    None => crate::redir(&session, &cfg, &req, |ctx| ctx.router().route_simple("profile"))
  }
}

pub fn settings(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| dbui_templates::settings::settings(&ctx))
}

pub fn settings_post(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| dbui_templates::settings::settings(&ctx))
}

pub fn testbed(session: Session, cfg: web::Data<AppConfig>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| dbui_templates::testbed::list::list(&ctx))
}

pub fn testbed_key(session: Session, cfg: web::Data<AppConfig>, key: web::Path<String>, req: HttpRequest) -> HttpResponse {
  crate::act(&session, &cfg, &req, |ctx| {
    let k: &str = &key;
    match k {
      "dump" => dbui_templates::testbed::dump::dump(&ctx),
      "gallery" => dbui_templates::testbed::gallery(&ctx),
      "list" => dbui_templates::testbed::list::list(&ctx),
      "scroll" => dbui_templates::testbed::scroll(&ctx),
      "sql.list" => dbui_templates::testbed::sql_list(&ctx),
      _ if key.starts_with("sql.") => run_sql(&ctx, key.trim_start_matches("sql.")),
      _ => Err(Error::from(format!("Cannot find testbed matching [{}]", key)))
    }
  })
}

fn run_sql(ctx: &RequestContext, key: &str) -> Result<maud::Markup> {
  let p = ctx.app().projects().read().expect("Can't lock project service!").get("x")?;
  let params = p.connection_params();
  let pool = dbui_database::conn::ConnectionPool::new(params)?;
  let mut conn = pool.open()?;
  let sql = match key {
    "databases" => Ok(dbui_database::named::LIST_DATABASES),
    "tables" => Ok(dbui_database::named::LIST_TABLES),
    _ => Err(Error::from(format!("No query available with key [{}]", key)))
  }?;
  let stmt = conn.prepare(&sql).chain_err(|| "Cannot parse query [project.test]")?;
  let rows = conn.query(&stmt, &[]).chain_err(|| "Cannot run query [project.test]")?;
  let rs = dbui_database::results::from_rows(sql, rows)?;
  dbui_templates::sql::results(&ctx, &sql, &rs)
}