something

This commit is contained in:
EvilMuffinHa 2021-09-08 21:41:34 -04:00
parent eab9749c72
commit 583b572b09
9 changed files with 236 additions and 30 deletions

View File

@ -6,7 +6,7 @@ use proc_macro2::*;
use std::fmt::Debug; use std::fmt::Debug;
#[derive(Debug)] #[derive(Debug)]
struct Row(Ident, Ident, Ident, Ident); struct Row(Ident, TokenStream, TokenStream, TokenStream);
fn api_route_inner(item: TokenStream) -> TokenStream { fn api_route_inner(item: TokenStream) -> TokenStream {
let iterator = item.into_iter().clone().collect::<Vec<TokenTree>>(); let iterator = item.into_iter().clone().collect::<Vec<TokenTree>>();
@ -72,11 +72,8 @@ fn api_route_inner(item: TokenStream) -> TokenStream {
false false
} }
}) })
.map(|x| match x.to_vec().get(0).unwrap().clone() { .map(|x| x.into_iter().map(|x| x.clone()).collect::<TokenStream>())
TokenTree::Ident(i) => i, .collect::<Vec<TokenStream>>();
n => panic!("Incorrect syntax at group => {:?}", n),
})
.collect::<Vec<Ident>>();
rows.push(Row( rows.push(Row(
name.clone(), name.clone(),
vals.get(0).unwrap().clone(), vals.get(0).unwrap().clone(),
@ -92,18 +89,18 @@ fn api_route_inner(item: TokenStream) -> TokenStream {
let schema_value = rows let schema_value = rows
.iter() .iter()
.map(|x| TokenTree::Ident(x.1.clone())) .map(|x| x.1.clone())
.collect::<Vec<TokenTree>>(); .collect::<Vec<TokenStream>>();
let get_value = rows let get_value = rows
.iter() .iter()
.map(|x| TokenTree::Ident(x.2.clone())) .map(|x| x.2.clone())
.collect::<Vec<TokenTree>>(); .collect::<Vec<TokenStream>>();
let put_value = rows let put_value = rows
.iter() .iter()
.map(|x| TokenTree::Ident(x.3.clone())) .map(|x| x.3.clone())
.collect::<Vec<TokenTree>>(); .collect::<Vec<TokenStream>>();
let impl_value = rows let impl_value = rows
.iter() .iter()
@ -116,7 +113,7 @@ fn api_route_inner(item: TokenStream) -> TokenStream {
} else { } else {
let s = x.0.clone(); let s = x.0.clone();
quote! { quote! {
#s: *self.#s, #s: self.#s.clone(),
} }
} }
}) })
@ -282,13 +279,15 @@ fn api_route_inner(item: TokenStream) -> TokenStream {
} }
#[get(#name_dir)] #[get(#name_dir)]
pub fn ui(_token: Token, pg: State<Mutex<PgConnection>>) -> Result<Template, Status> { pub fn eui(_token: Token, pg: State<Mutex<PgConnection>>) -> Result<Template, Status> {
let mut hash: HashMap<&str, HashMap<i32, Get>> = HashMap::new();
let ctx = get_all(&*(pg.lock().unwrap())) let ctx = get_all(&*(pg.lock().unwrap()))
.map_err(|_| Status::InternalServerError)? .map_err(|_| Status::InternalServerError)?
.iter() .iter()
.map(|x| (x.id, x.clone())) .map(|x| (x.id, x.clone()))
.collect::<HashMap<i32, Get>>(); .collect::<HashMap<i32, Get>>();
Ok(Template::render(#name_literal, &ctx)) hash.insert("ctx", ctx);
Ok(Template::render(#name_literal, &hash))
} }
}; };

View File

@ -13,3 +13,20 @@ CREATE TABLE events (
location VARCHAR NOT NULL, location VARCHAR NOT NULL,
event_date DATE 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
);

View File

@ -23,9 +23,10 @@ impl<'a> FromParam<'a> for Lang<'a> {
} }
pub mod defs { pub mod defs {
use chrono::naive::NaiveDate; use chrono::naive::{NaiveDate, NaiveTime};
use rocket::{http::RawStr, request::FromFormValue}; use rocket::{http::RawStr, request::FromFormValue};
use std::ops::Deref; use std::ops::Deref;
use rocket::response::NamedFile;
#[derive(Debug)] #[derive(Debug)]
pub struct DateForm(NaiveDate); 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<TimeForm, ()> {
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<String>);
impl Deref for EmailList {
type Target = Vec<String>;
fn deref(&self) -> &Vec<String> {
&self.0
}
}
impl<'v> FromFormValue<'v> for EmailList {
type Error = ();
fn from_form_value(value: &'v RawStr) -> Result<EmailList, ()> {
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::<Vec<String>>().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<Image, ()> {
/*
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! { api_route! {
@ -65,14 +152,73 @@ api_route! {
} }
} }
/*
api_route! { api_route! {
teachers { teachers {
name: (Text, String, String), name: (Text, String, String),
emails: (Array<Text>, Vec<String>, Vec<String>), emails: (Array<Text>, Vec<String>, 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),
}
}

View File

@ -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], 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( .mount(
"/ui", "/ui",
routes![ routes![
data::events::ui, 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::add, 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::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::upd 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,
], ],
) )
} }

View File

@ -1,3 +1,2 @@
body { body {
background-color: lightblue;
} }

View File

@ -1,12 +1,15 @@
<!-- vim: set ft=html: --> {{! vim: set ft=html: }}
<html> <html>
<head> <head>
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css">
</head> </head>
<body> <body>
<p>{{6.title}}</p> {{#each ctx}}
<p>{{id}} {{lang}} {{title}} {{location}} {{text}} {{event_date}}</p>
{{/each}}
<br> <br>
<p>add</p>
<form action="events/add" method="post"> <form action="events/add" method="post">
<input type="text", id="lang", name="lang"> <input type="text", id="lang", name="lang">
<input type="text", id="title", name="title"> <input type="text", id="title", name="title">
@ -15,11 +18,25 @@
<input type="text", id="event_date", name="event_date"> <input type="text", id="event_date", name="event_date">
<input type="submit", value="Submit"> <input type="submit", value="Submit">
</form> </form>
<p>del</p>
<form action="events/del" method="post"> <form action="events/del" method="post">
<input type="number", id="id", name="id"> <input type="number", id="id", name="id">
<input type="submit", value="Submit"> <input type="submit", value="Submit">
</form> </form>
<p>upd</p>
<form action="events/upd" method="post">
<input type="number", id="id", name="id">
<input type="text", id="lang", name="lang">
<input type="text", id="title", name="title">
<input type="text", id="location", name="location">
<input type="text", id="text", name="text">
<input type="text", id="event_date", name="event_date">
<input type="submit", value="Submit">
</form>
</body> </body>
</head> </head>
</html> </html>

View File

@ -5,5 +5,6 @@
</head> </head>
<body> <body>
<a href="/ui/events">Events</a> <a href="/ui/events">Events</a>
<a href="/ui/new">New</a>
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
<!-- vim: set ft=html: --> {{! vim: set ft=html: }}
<html> <html>
<head> <head>

View File

@ -0,0 +1,18 @@
{{! vim: set ft=html: }}
<html>
<head>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<p>add</p>
<form action="new/add" method="post">
<input type="text", id="lang", name="lang">
<input type="file" name="image", id="image", accept="image/png">
<input type="text", id="name", name="name">
<input type="text", id="new_date", name="new_date">
<input type="submit", value="Submit">
</form>
</body>
</head>
</html>