diff --git a/Cargo.lock b/Cargo.lock index 412c1bd..76de12e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1060,6 +1060,7 @@ dependencies = [ "askama_tide", "async-std", "chrono", + "serde", "sqlx", "tide", ] diff --git a/Cargo.toml b/Cargo.toml index 7f1ec49..88ccfbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,12 @@ edition = "2018" [dependencies] anyhow = "1.0" -argh = "0.1.3" +argh = "0.1" askama_tide = "0.10" askama = { version = "0.10", features = ["with-tide"]} async-std = { version = "1.6", features = ["attributes"] } chrono = "0.4" +serde = "1.0" sqlx = { version = "0.4.0-beta.1", features = ["mysql", "chrono", "macros"] } tide = "0.13" diff --git a/src/db.rs b/src/db.rs index c7e0e36..98c13a3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,9 +4,10 @@ use std::net::IpAddr; type QueryAs<'q, T> = sqlx::query::QueryAs<'q, sqlx::MySql, T, >::Arguments>; +type Query<'q> = sqlx::query::Query<'q, sqlx::MySql, >::Arguments>; #[derive(sqlx::FromRow, Debug)] -pub struct Entry { +pub struct Device { pub id: i32, pub macaddr: String, pub nickname: String, @@ -26,7 +27,7 @@ pub enum PrivacyLevel { DontLog = 4, } -impl<'q> Entry { +impl<'q> Device { pub fn all() -> QueryAs<'q, Self> { sqlx::query_as("SELECT * FROM mac_to_nick") } @@ -34,21 +35,37 @@ impl<'q> Entry { pub fn for_user(user: &'q str) -> QueryAs<'q, Self> { sqlx::query_as( " -SELECT +SELECT DISTINCT mtn.*, - IF(al.iplong != NULL, TRUE, FALSE) present + IF(al.iplong, TRUE, FALSE) present FROM - mac_to_nick mtn, + mac_to_nick mtn +LEFT JOIN alive_hosts al +ON + mtn.macaddr = al.macaddr + AND al.erfda > NOW() - INTERVAL 24 DAY WHERE - al.macaddr = mtn.macaddr - AND nickname = ? -GROUP BY - mtn.macaddr + nickname = ? +ORDER BY + al.erfda DESC ", ) .bind(user) } + + pub fn register(mac: &'q str, user: &'q str) -> Query<'q> { + sqlx::query( + " +INSERT +INTO mac_to_nick +(macaddr, nickname, descr, privacy, created) +VALUES +(?, ?, ?, ?, NOW()) +", + ) + .bind(mac) + } } impl PrivacyLevel { @@ -79,11 +96,14 @@ SELECT DISTINCT al.macaddr macaddr, al.iplong iplong FROM - alive_hosts al, + alive_hosts al +NATURAL LEFT JOIN mac_to_nick mtn WHERE mtn.nickname IS NULL AND al.erfda > NOW() - INTERVAL 24 DAY +ORDER BY + al.erfda DESC ", ) } diff --git a/src/main.rs b/src/main.rs index da72abb..25a48d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ async fn main() -> Result<(), io::Error> { let mut app = tide::with_state(State { pool }); app.at("/").get(routes::index); + app.at("/register").post(routes::register); app.at("/healthz").get(routes::healthz); app.at("/static").serve_dir("static/")?; diff --git a/src/routes.rs b/src/routes.rs index 42ea35f..c437883 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,12 +1,15 @@ use crate::db; use crate::templates; +use tide::prelude::*; + +const USER: &str = "foosinn"; pub async fn healthz(_request: crate::Request) -> tide::Result { Ok("ok".into()) } pub async fn index(request: crate::Request) -> tide::Result { - let my = db::Entry::for_user("foosinn") + let my = db::Device::for_user(USER) .fetch_all(&request.state().pool) .await .map_err(|err| dbg!(err))?; @@ -16,3 +19,13 @@ pub async fn index(request: crate::Request) -> tide::Result { .map_err(|err| dbg!(err))?; Ok(templates::IndexTemplate::new(my, unassinged).into()) } + +#[derive(Deserialize)] +struct RegisterForm { + macaddr: String, +} + +pub async fn register(mut request: crate::Request) -> tide::Result { + let form: RegisterForm = request.body_form().await?; + unimplemented!(); +} diff --git a/src/templates.rs b/src/templates.rs index 7123ca5..eaca270 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -4,12 +4,12 @@ use askama::Template; #[derive(Template, Default)] #[template(path = "index.html")] pub struct IndexTemplate { - my: Vec, + my: Vec, unassinged: Vec, } impl IndexTemplate { - pub fn new(my: Vec, unassinged: Vec) -> Self { + pub fn new(my: Vec, unassinged: Vec) -> Self { Self { my, unassinged } } } diff --git a/templates/index.html b/templates/index.html index d349ba3..482de38 100644 --- a/templates/index.html +++ b/templates/index.html @@ -19,8 +19,8 @@

Your Devices:

{% for device in my %} -
-
{{ device.macaddr }}
+
+
{{ device.macaddr }} {{ device.present }}
@@ -52,7 +52,7 @@

Unregistred Devices:

{% for device in unassinged %} -
+
{{ device.macaddr }}
{{ device.ip() }}