summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2020-02-07 19:00:15 -0500
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2020-02-07 19:00:15 -0500
commit8d4d29a3c25063d71c561f30ccc1c31ab85d2bc7 (patch)
tree11e435ef7eccee70be0c3ffd370e02a89da61c6d
parentd69c7e12c721898f597e8a7c78f42db6fc02e07b (diff)
Add test template for "run"
-rw-r--r--src/db.rs11
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs4
-rw-r--r--src/models.rs26
-rw-r--r--src/server.rs19
-rw-r--r--src/template.rs80
6 files changed, 100 insertions, 41 deletions
diff --git a/src/db.rs b/src/db.rs
index cec7d56..198ce03 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,6 +1,7 @@
use crate::diesel::BoolExpressionMethods;
use crate::error::Error;
use crate::models;
+use crate::template;
use bcrypt;
use chrono::DateTime;
use chrono::Utc;
@@ -248,6 +249,16 @@ pub fn get_entries(conn: &PgConnection, username: &str) -> Result<Vec<models::En
Ok(r)
}
+pub fn get_template(
+ conn: &PgConnection,
+ entry_type: &str,
+) -> Result<template::TemplateSpec, Error> {
+ match entry_type {
+ "run" => Ok(template::running_template()),
+ _ => Err(Error::NotFound),
+ }
+}
+
// pub fn get_entries_with_data(
// conn: &PgConnection,
// username: &str,
diff --git a/src/lib.rs b/src/lib.rs
index 80cef09..2036560 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -24,6 +24,7 @@ pub mod models;
mod schema;
pub mod server;
mod strava;
+pub mod template;
pub struct Params {
pub base_url: String,
diff --git a/src/main.rs b/src/main.rs
index cf0d308..ec0e660 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -135,9 +135,7 @@ fn main() {
.value_of("base_url")
.unwrap_or("http://localhost:8000");
- let static_path = matches
- .value_of("static_path")
- .unwrap_or("./static");
+ let static_path = matches.value_of("static_path").unwrap_or("./static");
let db_url = matches.value_of("database_url").unwrap();
let conn = PgConnection::establish(db_url).unwrap();
diff --git a/src/models.rs b/src/models.rs
index 624a1ce..83153fd 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -170,29 +170,3 @@ pub struct Entry {
pub timestamp: Option<DateTime<Utc>>,
pub payload: Value,
}
-
-#[derive(Debug, Serialize, Deserialize, Clone)]
-pub enum Unit {
- TimestampRfc3339,
- Meters,
- Seconds,
- Unknown,
-}
-
-#[derive(Debug, Serialize, Deserialize, Clone)]
-pub struct Column {
- field: String,
- display_name: Option<String>,
- unit: Unit,
-}
-
-#[derive(Debug, Serialize, Deserialize, Clone)]
-pub enum TemplateSpec {
- Table(Vec<Column>),
-}
-
-#[derive(Debug, Serialize, Deserialize, Clone)]
-pub struct Template {
- pub entry_type: String,
- pub spec: TemplateSpec,
-}
diff --git a/src/server.rs b/src/server.rs
index a203af9..3d264a9 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -13,12 +13,12 @@ use rocket::request::FromRequest;
use rocket::request::Request;
use rocket::response::Redirect;
use rocket::State;
+use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use serde_json::map::Map;
use serde_json::to_value;
use serde_json::Value as Json;
use std::collections::HashMap;
-use rocket_contrib::serve::StaticFiles;
use crate::db;
use crate::error::Error;
@@ -78,17 +78,15 @@ fn profile(conn: Db, username: String) -> Result<Template, Error> {
context.insert("title".to_string(), json!(username));
let entries = db::get_entries(&*conn, &username)?;
- let headings =
- entries.first()
+ let headings = entries
+ .first()
.and_then(|e| e.payload.as_object())
.map(|e| e.keys().collect::<Vec<_>>());
context.insert("headings".to_string(), json!(headings));
context.insert(
"entries".to_string(),
- json!(entries
- .into_iter()
- .map(|e| e.payload)
- .collect::<Vec<_>>()));
+ json!(entries.into_iter().map(|e| e.payload).collect::<Vec<_>>()),
+ );
Ok(Template::render("profile", context))
}
@@ -196,8 +194,7 @@ fn link_strava(params: State<Params>) -> Redirect {
))
}
-pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str,
- static_path: &str) {
+pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str, static_path: &str) {
let mut database_config = HashMap::new();
let mut databases = HashMap::new();
database_config.insert("url", Value::from(db_url));
@@ -239,9 +236,7 @@ pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str,
link_strava_callback
],
)
- .mount(
- "/static",
- StaticFiles::from(static_path))
+ .mount("/static", StaticFiles::from(static_path))
.attach(Template::fairing())
.attach(Db::fairing())
.launch();
diff --git a/src/template.rs b/src/template.rs
new file mode 100644
index 0000000..21defa3
--- /dev/null
+++ b/src/template.rs
@@ -0,0 +1,80 @@
+use serde::Deserialize;
+use serde::Serialize;
+
+pub fn running_template() -> TemplateSpec {
+ TemplateSpec::Table(vec![
+ Column {
+ display_name: Some("Date".to_string()),
+ field: FieldSpec::App(
+ Function::DisplayUnit(Unit::Timestamp, "date".to_string()),
+ vec![FieldSpec::Field("start_time".to_string())],
+ ),
+ },
+ Column {
+ display_name: Some("Time".to_string()),
+ field: FieldSpec::App(
+ Function::DisplayUnit(Unit::Seconds, "".to_string()),
+ vec![FieldSpec::Field("moving_time".to_string())],
+ ),
+ },
+ Column {
+ display_name: Some("Distance".to_string()),
+ field: FieldSpec::App(
+ Function::DisplayUnit(Unit::Meters, "miles".to_string()),
+ vec![FieldSpec::Field("distance".to_string())],
+ ),
+ },
+ Column {
+ display_name: Some("Pace".to_string()),
+ field: FieldSpec::App(
+ Function::DisplayUnit(Unit::Pace, "".to_string()),
+ vec![FieldSpec::App(
+ Function::Div,
+ vec![
+ FieldSpec::Field("moving_time".to_string()),
+ FieldSpec::Field("distance".to_string()),
+ ],
+ )],
+ ),
+ },
+ ])
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub enum Unit {
+ Timestamp, // As Rfc3339
+ Meters,
+ Seconds,
+ Speed, // As m/s
+ Pace, // As s/m
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub enum Function {
+ TimestampToDate,
+ DisplayUnit(Unit, String),
+ Div,
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub enum FieldSpec {
+ Field(String),
+ App(Function, Vec<FieldSpec>),
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub struct Column {
+ display_name: Option<String>,
+ field: FieldSpec,
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub enum TemplateSpec {
+ Table(Vec<Column>),
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub struct Template {
+ pub entry_type: String,
+ pub spec: TemplateSpec,
+}