diff --git a/cms/cms_macro/src/lib.rs b/cms/cms_macro/src/lib.rs index e81eedc..1a61c41 100644 --- a/cms/cms_macro/src/lib.rs +++ b/cms/cms_macro/src/lib.rs @@ -6,7 +6,7 @@ use proc_macro2::*; use std::fmt::Debug; #[derive(Debug)] -struct Row(Ident, Ident, Ident, Ident); +struct Row(Ident, TokenStream, TokenStream, TokenStream); fn api_route_inner(item: TokenStream) -> TokenStream { let iterator = item.into_iter().clone().collect::>(); @@ -72,11 +72,8 @@ fn api_route_inner(item: TokenStream) -> TokenStream { false } }) - .map(|x| match x.to_vec().get(0).unwrap().clone() { - TokenTree::Ident(i) => i, - n => panic!("Incorrect syntax at group => {:?}", n), - }) - .collect::>(); + .map(|x| x.into_iter().map(|x| x.clone()).collect::()) + .collect::>(); rows.push(Row( name.clone(), vals.get(0).unwrap().clone(), @@ -92,18 +89,18 @@ fn api_route_inner(item: TokenStream) -> TokenStream { let schema_value = rows .iter() - .map(|x| TokenTree::Ident(x.1.clone())) - .collect::>(); + .map(|x| x.1.clone()) + .collect::>(); let get_value = rows .iter() - .map(|x| TokenTree::Ident(x.2.clone())) - .collect::>(); + .map(|x| x.2.clone()) + .collect::>(); let put_value = rows .iter() - .map(|x| TokenTree::Ident(x.3.clone())) - .collect::>(); + .map(|x| x.3.clone()) + .collect::>(); let impl_value = rows .iter() @@ -116,7 +113,7 @@ fn api_route_inner(item: TokenStream) -> TokenStream { } else { let s = x.0.clone(); quote! { - #s: *self.#s, + #s: self.#s.clone(), } } }) @@ -282,13 +279,15 @@ fn api_route_inner(item: TokenStream) -> TokenStream { } #[get(#name_dir)] - pub fn ui(_token: Token, pg: State>) -> Result { + pub fn eui(_token: Token, pg: State>) -> Result { + let mut hash: HashMap<&str, HashMap> = HashMap::new(); let ctx = get_all(&*(pg.lock().unwrap())) .map_err(|_| Status::InternalServerError)? .iter() .map(|x| (x.id, x.clone())) .collect::>(); - Ok(Template::render(#name_literal, &ctx)) + hash.insert("ctx", ctx); + Ok(Template::render(#name_literal, &hash)) } }; diff --git a/cms/migrations/setup/up.sql b/cms/migrations/setup/up.sql index 87081c2..0100d78 100644 --- a/cms/migrations/setup/up.sql +++ b/cms/migrations/setup/up.sql @@ -13,3 +13,20 @@ CREATE TABLE events ( location VARCHAR NOT NULL, event_date DATE ); + + +CREATE TABLE teachers ( + id SERIAL PRIMARY KEY, + lang VARCHAR, + name VARCHAR NOT NULL, + emails VARCHAR[] +); + + +CREATE TABLE new ( + id SERIAL PRIMARY KEY, + lang VARCHAR, + image VARCHAR, + name VARCHAR NOT NULL, + new_date DATE +); diff --git a/cms/src/data/mod.rs b/cms/src/data/mod.rs index 1000929..ffd9df1 100644 --- a/cms/src/data/mod.rs +++ b/cms/src/data/mod.rs @@ -23,9 +23,10 @@ impl<'a> FromParam<'a> for Lang<'a> { } pub mod defs { - use chrono::naive::NaiveDate; + use chrono::naive::{NaiveDate, NaiveTime}; use rocket::{http::RawStr, request::FromFormValue}; use std::ops::Deref; + use rocket::response::NamedFile; #[derive(Debug)] pub struct DateForm(NaiveDate); @@ -54,6 +55,92 @@ pub mod defs { } } + #[derive(Debug)] + pub struct TimeForm(NaiveTime); + + impl Deref for TimeForm { + type Target = NaiveTime; + + fn deref(&self) -> &NaiveTime { + &self.0 + } + } + + impl<'v> FromFormValue<'v> for TimeForm { + type Error = (); + + fn from_form_value(value: &'v RawStr) -> Result { + let value_uri = match value.url_decode() { + Ok(n) => n, + Err(_) => return Err(()), + }; + let naivedate = NaiveTime::parse_from_str(&value_uri[..], "%I:%M %p"); + match naivedate { + Ok(n) => Ok(TimeForm(n)), + Err(_) => Err(()), + } + } + } + + + #[derive(Debug)] + pub struct EmailList(Vec); + + impl Deref for EmailList { + type Target = Vec; + + fn deref(&self) -> &Vec { + &self.0 + } + } + + impl<'v> FromFormValue<'v> for EmailList { + type Error = (); + fn from_form_value(value: &'v RawStr) -> Result { + let mut value_uri = match value.url_decode() { + Ok(n) => n, + Err(_) => return Err(()), + }; + + Ok(EmailList((&mut value_uri[..]) + .split(|x| x == ' ') + .map(String::from) + .collect::>().clone())) + } + } + + #[derive(Debug)] + pub struct Image(String); + + impl Deref for Image { + type Target = String; + + fn deref(&self) -> &String { + &self.0 + } + } + + impl<'v> FromFormValue<'v> for Image { + type Error = (); + + fn from_form_value(value: &'v RawStr) -> Result { + /* + println!("{:?}", value); + let file = NamedFile::open(value.to_string()); + println!("{:?}", file); + + Ok(Image(String::from("pepega"))) + */ + + let mut value_uri = match value.url_decode() { + Ok(n) => n, + Err(_) => return Err(()), + }; + Ok(Image(value_uri)) + + } + } + } api_route! { @@ -65,14 +152,73 @@ api_route! { } } -/* api_route! { teachers { name: (Text, String, String), - emails: (Array, Vec, Vec), + emails: (Array, Vec, EmailList), } } -*/ -//TODO: fix value parsing to read a TokenStream until the `,` to allow for containerized types in -//the macro +api_route! { + announcements { + message: (Text, String, String), + teacher: (Text, String, String), + date: (Date, NaiveDate, DateForm), + time: (Time, NaiveTime, TimeForm), + } +} + +api_route! { + clubs { + name: (Text, String, String), + meeting: (Text, String, String), + link: (Text, String, String), + sponsor: (Text, String, String), + } +} + +api_route! { + lunch_events { + title: (Text, String, String), + text: (Text, String, String), + location: (Text, String, String), + time: (Time, NaiveTime, TimeForm), + } +} + +api_route! { + ssl_ops { + title: (Text, String, String), + text: (Text, String, String), + location: (Text, String, String), + teacher: (Text, String, String), + time: (Time, NaiveTime, TimeForm), + } +} + +api_route! { + calendar { + time: (Time, NaiveTime, TimeForm), + } +} + +api_route! { + polls { + url: (Text, String, String), + } +} + +api_route! { + new { + image: (Text, String, Image), + name: (Text, String, String), + new_date: (Date, NaiveDate, DateForm), + } +} + +api_route! { + important { + image: (Text, String, Image), + text: (Text, String, String), + } +} diff --git a/cms/src/main.rs b/cms/src/main.rs index 1bc81d0..13d78ae 100644 --- a/cms/src/main.rs +++ b/cms/src/main.rs @@ -75,14 +75,23 @@ fn rocket(port: u16, address: String, env: Environment, pg: PgConnection, sa: Se "/", routes![home, home_not_logged_in, login, auth::callback, auth::oauth, static_files], ) - .mount("/api", routes![data::events::api]) + .mount("/api", routes![ + data::events::api, + data::teachers::api, + data::announcements::api, + data::clubs::api, + data::lunch_events::api, + data::ssl_ops::api, + data::calendar::api, + data::polls::api, + ]) .mount( "/ui", routes![ - data::events::ui, - data::events::add, - data::events::del, - data::events::upd + data::events::eui, data::teachers::eui, data::announcements::eui, data::clubs::eui, data::lunch_events::eui, data::ssl_ops::eui, data::calendar::eui, data::polls::eui, data::new::eui, data::important::eui, + data::events::upd, data::teachers::upd, data::announcements::upd, data::clubs::upd, data::lunch_events::upd, data::ssl_ops::upd, data::calendar::upd, data::polls::upd, data::new::upd, data::important::upd, + data::events::del, data::teachers::del, data::announcements::del, data::clubs::del, data::lunch_events::del, data::ssl_ops::del, data::calendar::del, data::polls::del, data::new::del, data::important::del, + data::events::add, data::teachers::add, data::announcements::add, data::clubs::add, data::lunch_events::add, data::ssl_ops::add, data::calendar::add, data::polls::add, data::new::add, data::important::add, ], ) } diff --git a/cms/static/style.css b/cms/static/style.css index eed8207..25fa51c 100644 --- a/cms/static/style.css +++ b/cms/static/style.css @@ -1,3 +1,2 @@ body { - background-color: lightblue; } diff --git a/cms/templates/events.html.hbs b/cms/templates/events.html.hbs index fcde65f..22040b8 100644 --- a/cms/templates/events.html.hbs +++ b/cms/templates/events.html.hbs @@ -1,12 +1,15 @@ - +{{! vim: set ft=html: }} -

{{6.title}}

+ {{#each ctx}} +

{{id}} {{lang}} {{title}} {{location}} {{text}} {{event_date}}

+ {{/each}}
+

add

@@ -15,11 +18,25 @@
+ +

del

+ +

upd

+
+ + + + + + + +
+ diff --git a/cms/templates/home.html.hbs b/cms/templates/home.html.hbs index 28559a7..07e0d80 100644 --- a/cms/templates/home.html.hbs +++ b/cms/templates/home.html.hbs @@ -5,5 +5,6 @@ Events + New diff --git a/cms/templates/login.html.hbs b/cms/templates/login.html.hbs index 3ffad37..afcada8 100644 --- a/cms/templates/login.html.hbs +++ b/cms/templates/login.html.hbs @@ -1,4 +1,4 @@ - +{{! vim: set ft=html: }} diff --git a/cms/templates/new.html.hbs b/cms/templates/new.html.hbs new file mode 100644 index 0000000..f2815a5 --- /dev/null +++ b/cms/templates/new.html.hbs @@ -0,0 +1,18 @@ +{{! vim: set ft=html: }} + + + + + + +

add

+
+ + + + + +
+ + +