Commit a65d6b5d authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Display group list (stub)

parent 4fdb5438
......@@ -31,6 +31,11 @@ pub trait MedalConnection {
fn get_submission_to_validate(&self, tasklocation: String, subtask: Option<String>) -> u32;
fn find_next_submission_to_validate(&self, userid: u32, taskgroupid: u32);
fn add_group(&self, group: &mut Group);
fn get_groups(&self, session_id: u32) -> Vec<Group>;
fn get_groups_complete(&self, session_id: u32) -> Vec<Group>;
fn get_group_complete(&self, group_id: u32) -> Option<Group>;
}
......
......@@ -313,6 +313,28 @@ impl MedalConnection for Connection {
self.execute("UPDATE submission SET needs_validation = 1 WHERE id = ?1", &[&id]).unwrap();
}
}
fn add_group(&self, group: &mut Group) {
group.save(self);
}
fn get_groups(&self, session_id: u32) -> Vec<Group> {
let mut stmt = self.prepare("SELECT id, name, groupcode, tag FROM usergroup WHERE admin = ?1").unwrap();
let rows = stmt.query_map(&[&session_id], |row| {
Group {
id: Some(row.get(0)),
name: row.get(1),
groupcode: row.get(2),
tag: row.get(3),
admin: session_id,
members: Vec::new(),
}
}).unwrap().filter_map(|row| {row.ok()}).collect();
rows
}
fn get_groups_complete(&self, session_id: u32) -> Vec<Group> {unimplemented!();}
fn get_group_complete(&self, group_id: u32) -> Option<Group> {unimplemented!();}
}
......@@ -424,7 +446,7 @@ impl MedalObject<Connection> for Submission {
unimplemented!(),
None => {
conn.execute("INSERT INTO submission (task, session_user, grade, validated, nonvalidated_grade, subtask_identifier, value, date, needs_validation) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)", &[&self.task, &self.session_user, &self.grade, &self.validated, &self.nonvalidated_grade, &self.subtask_identifier, &self.value, &self.date, &self.needs_validation]).unwrap();
// TODO, update ID
self.setId(conn.query_row("SELECT last_insert_rowid()", &[], |row| {row.get(0)}).unwrap())
}
}
}
......@@ -437,7 +459,7 @@ impl MedalObject<Connection> for Group {
unimplemented!(),
None => {
conn.execute("INSERT INTO usergroup (name, groupcode, tag, admin) VALUES (?1, ?2, ?3, ?4)", &[&self.name, &self.groupcode, &self.tag, &self.admin]).unwrap();
// TODO, update ID
self.setId(conn.query_row("SELECT last_insert_rowid()", &[], |row| {row.get(0)}).unwrap());
}
}
}
......
......@@ -31,7 +31,6 @@ pub struct ContestInfo {
pub fn show_contests<T: MedalConnection>(conn: &T) -> (String, json_val::Map<String, json_val::Value>) {
let mut data = json_val::Map::new();
let v : Vec<ContestInfo> = conn.get_contest_list().iter().map(|c| { ContestInfo {
id: c.id.unwrap(),
......@@ -185,6 +184,49 @@ pub fn show_task<T: MedalConnection>(conn: &T, task_id: u32, session_token: Stri
// TOKEN -> {"token_type" : "Bearer","expires" : 3600,"refresh_token" : "R3a716e23-b320-4dab-a529-4c19e6b7ffc5","access_token" : "A6f681904-ded6-4e8b-840e-ac79ca1ffc07"}
// DATA -> {"lastName" : "Czechowski","gender" : "?","userType" : "a","userID" : "12622","dateOfBirth" : "2001-01-01","firstName" : "Robert","eMail" : "czechowski@bwinf.de","schoolId" : -1}
#[derive(Serialize, Deserialize)]
pub struct GroupInfo {
pub name: String,
pub tag: String,
pub code: String,
}
pub fn show_groups<T: MedalConnection>(conn: &T, session_token: String) -> (String, json_val::Map<String, json_val::Value>) {
let session = conn.get_session(session_token).unwrap(); // TODO handle error
// let groupvec = conn.get_group(session_token);
let mut data = json_val::Map::new();
let v : Vec<GroupInfo> = conn.get_groups(session.id).iter().map(|g| { GroupInfo {
name: g.name.clone(),
tag: g.tag.clone(),
code: g.groupcode.clone(),
}}).collect();
data.insert("group".to_string(), to_json(&v));
data.insert("csrftoken".to_string(), to_json(&session.csrf_token));
("groups".to_string(), data)
}
pub fn show_group<T: MedalConnection>(conn: &T, group_id: u32, session_token: String) -> Result<(String, json_val::Map<String, json_val::Value>),(String, json_val::Map<String, json_val::Value>)> {
let session = conn.get_session(session_token).unwrap(); // TODO handle error
let group = conn.get_group_complete(group_id).unwrap(); // TODO handle error
let mut data = json_val::Map::new();
if group.admin != session.id {
return Err(("error".to_owned(), data));
}
data.insert("groupname".to_string(), to_json(&group.name));
data.insert("grouptag".to_string(), to_json(&group.tag));
Ok(("group".to_string(), data))
}
pub fn modify_group<T: MedalConnection>(conn: &T, group_id: u32, session_token: String) -> Result<(),(String, json_val::Map<String, json_val::Value>)> {
unimplemented!()
}
pub fn add_group<T: MedalConnection>(conn: &T, session_token: String, csrf_token: String, name: String, tag: String) -> Result<u32, (String, json_val::Map<String, json_val::Value>)> {
let session = conn.get_session(session_token).unwrap(); // TODO handle error
......@@ -194,7 +236,7 @@ pub fn add_group<T: MedalConnection>(conn: &T, session_token: String, csrf_token
return Err(("error".to_owned(), data));
}
let group = Group {
let mut group = Group {
id: None,
name: name,
groupcode: "blub".to_string(),
......@@ -203,7 +245,7 @@ pub fn add_group<T: MedalConnection>(conn: &T, session_token: String, csrf_token
members: Vec::new()
};
group.save(conn);
conn.add_group(&mut group);
Ok(group.id.unwrap())
}
......
......@@ -296,16 +296,18 @@ fn task(req: &mut Request) -> IronResult<Response> {
}
fn groups(req: &mut Request) -> IronResult<Response> {
let session_token = SessionRequestExt::session(req).get::<SessionToken>().unwrap().unwrap();
let (template, data) = {
// hier ggf. Daten aus dem Request holen
let mutex = req.get::<Write<SharedDatabaseConnection>>().unwrap();
let conn = mutex.lock().unwrap();
// Antwort erstellen und zurücksenden
//functions::show_contests(&*conn)
let mut data = json_val::Map::new();
functions::show_groups(&*conn, session_token.token)
/*let mut data = json_val::Map::new();
data.insert("reason".to_string(), to_json(&"Not implemented".to_string()));
("groups", data)
("groups", data)*/
};
let mut resp = Response::new();
......@@ -314,25 +316,62 @@ fn groups(req: &mut Request) -> IronResult<Response> {
}
fn group(req: &mut Request) -> IronResult<Response> {
let (template, data) = {
let session_token = SessionRequestExt::session(req).get::<SessionToken>().unwrap().unwrap();
let group_id = req.extensions.get::<Router>().unwrap().find("groupid").unwrap_or("").parse::<u32>().unwrap_or(0);
let groupresult = {
// hier ggf. Daten aus dem Request holen
let mutex = req.get::<Write<SharedDatabaseConnection>>().unwrap();
let conn = mutex.lock().unwrap();
// Antwort erstellen und zurücksenden
//functions::show_contests(&*conn)
let mut data = json_val::Map::new();
functions::show_group(&*conn, group_id, session_token.token)
/*let mut data = json_val::Map::new();
data.insert("reason".to_string(), to_json(&"Not implemented".to_string()));
("group", data)
("group", data)*/
};
let mut resp = Response::new();
resp.set_mut(Template::new(&template, data)).set_mut(status::Ok);
Ok(resp)
match groupresult {
// Change successful
Ok((template, data)) => {
let mut resp = Response::new();
resp.set_mut(Template::new(&template, data)).set_mut(status::Ok);
Ok(resp)
},
// Change failed
Err((template, data)) => {
let mut resp = Response::new();
resp.set_mut(Template::new(&template, data)).set_mut(status::Forbidden);
Ok(resp)
}
}
}
fn group_post(req: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Found, Redirect(url_for!(req, "group")))))
let session_token = SessionRequestExt::session(req).get::<SessionToken>().unwrap().unwrap();
let group_id = req.extensions.get::<Router>().unwrap().find("groupid").unwrap_or("").parse::<u32>().unwrap_or(0);
let changegroupresult = {
// hier ggf. Daten aus dem Request holen
let mutex = req.get::<Write<SharedDatabaseConnection>>().unwrap();
let conn = mutex.lock().unwrap();
// Antwort erstellen und zurücksenden
functions::modify_group(&*conn, group_id, session_token.token)
};
match changegroupresult {
// Change successful
Ok(()) => {
Ok(Response::with((status::Found, Redirect(url_for!(req, "group", "groupid" => format!("{}",group_id))))))
},
// Change failed
Err((template, data)) => {
let mut resp = Response::new();
resp.set_mut(Template::new(&template, data)).set_mut(status::Forbidden);
Ok(resp)
}
}
}
fn new_group(req: &mut Request) -> IronResult<Response> {
......@@ -419,6 +458,8 @@ pub fn get_handlebars_engine() -> impl AfterMiddleware {
if let Err(r) = hbse.reload() {
panic!("{}", r);
}
hbse
}
......
......@@ -4,9 +4,12 @@
<h2>Gruppe anlegen</h2>
<p>
<form action="/group/new" method="post">
Gruppename: <input>
Marker: <input placeholder="optional">
<form action="/group/" method="post">
Gruppename:
<input name="name">
Marker:
<input name="tag" placeholder="optional">
<input type="hidden" name="csrf" value="{{csrftoken}}">
<input type="submit" value="Neue Gruppe anlegen">
</form>
</p>
......@@ -32,5 +35,11 @@
<ul>
{{#each group}}
<li><a href="/contest/{{id}}">{{name}}, {{tag}}, {{code}}</li>
{{/each}}
</ul>
<p><a href="/">Zurück zur Startseite</a></p>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment