Add very simple forum view and debug insertion route

This commit is contained in:
Vivian Lim 2018-10-20 00:01:58 -07:00
parent 2ae2066440
commit f6bae4189e
6 changed files with 160 additions and 1 deletions

View File

@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
drop table forums;
drop table memberships;
drop table topics;
drop table posts;

View File

@ -0,0 +1,42 @@
-- Your SQL goes here
CREATE TABLE forums (
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL,
description VARCHAR NOT NULL,
minimum_permission_level INTEGER NOT NULL
);
CREATE TABLE memberships (
id INTEGER PRIMARY KEY NOT NULL,
user_id INTEGER NOT NULL REFERENCES users (id),
forum_id INTEGER NOT NULL REFERENCES forums (id),
permission_level INTEGER NOT NULL
-- arbitrary permission levels for now
-- -1: banned
-- 0: guest
-- 1: member
-- 2: moderator
-- 3: admin
);
CREATE TABLE topics (
id INTEGER PRIMARY KEY NOT NULL,
subject VARCHAR NOT NULL,
forum_id INTEGER NOT NULL REFERENCES forums (id),
state INTEGER NOT NULL,
creation_date DATE NOT NULL,
sort_date DATE NOT NULL,
minimum_permission_level INTEGER NOT NULL
);
CREATE TABLE posts (
id INTEGER PRIMARY KEY NOT NULL,
user_id INTEGER NOT NULL REFERENCES users (id),
topic_id INTEGER NOT NULL REFERENCES topics (id),
body TEXT NOT NULL,
creation_date DATE NOT NULL,
modified_date DATE,
modifying_user_id INTEGER REFERENCES users (id),
minimum_permission_level INTEGER NOT NULL
);

View File

@ -54,6 +54,9 @@ fn main() {
.and(static_routes)
.or(get().and(
(s().and(index()).and_then(home_page))
.or(s().and(path("forums"))
.and(index()).and_then(forum_list)
.or(s().and(path("forums")).and(path::param()).and_then(add_forum)))
.or(s().and(path("login")).and(index()).and_then(login_form))
.or(s()
.and(path("signup"))
@ -67,7 +70,27 @@ fn main() {
.and(body::form())
.and_then(do_signup)),
)).recover(customize_error);
warp::serve(routes).run(([127, 0, 0, 1], 3030));
warp::serve(routes).run(([0, 0, 0, 0], 8080));
}
/// Render a forum list.
fn forum_list(session: Session) -> Result<impl Reply, Rejection> {
use session::get_forums;
match get_forums(&session){
Ok(forums) => Response::builder().html(|o| templates::forum_list(o, &session, &forums, None, None)),
Err(err) => Err(reject::server_error().with(err))
}
}
/// seed forum list
fn add_forum(session: Session, forum_name: String) -> Result<impl Reply, Rejection> {
use session::add_forum;
use session::get_forums;
add_forum(&session, &forum_name, &forum_name);
match get_forums(&session){
Ok(forums) => Response::builder().html(|o| templates::forum_list(o, &session, &forums, None, None)),
Err(err) => Err(reject::server_error().with(err))
}
}
/// Render a login form.

View File

@ -1,3 +1,34 @@
table! {
forums (id) {
id -> Integer,
name -> Text,
description -> Text,
minimum_permission_level -> Integer,
}
}
table! {
memberships (id) {
id -> Integer,
user_id -> Integer,
forum_id -> Integer,
permission_level -> Integer,
}
}
table! {
posts (id) {
id -> Integer,
user_id -> Integer,
topic_id -> Integer,
body -> Text,
creation_date -> Date,
modified_date -> Nullable<Date>,
modifying_user_id -> Nullable<Integer>,
minimum_permission_level -> Integer,
}
}
table! {
sessions (id) {
id -> Integer,
@ -6,6 +37,18 @@ table! {
}
}
table! {
topics (id) {
id -> Integer,
subject -> Text,
forum_id -> Integer,
state -> Integer,
creation_date -> Date,
sort_date -> Date,
minimum_permission_level -> Integer,
}
}
table! {
users (id) {
id -> Integer,
@ -15,9 +58,17 @@ table! {
}
}
joinable!(memberships -> forums (forum_id));
joinable!(memberships -> users (user_id));
joinable!(posts -> topics (topic_id));
joinable!(sessions -> users (user_id));
joinable!(topics -> forums (forum_id));
allow_tables_to_appear_in_same_query!(
forums,
memberships,
posts,
sessions,
topics,
users,
);

View File

@ -30,6 +30,7 @@ impl Session {
/// Attempt to authenticate a user for this session.
///
/// If the username and password is valid, create and return a session key.
///
/// If authentication fails, simply return None.
pub fn authenticate(
&mut self,
@ -149,3 +150,28 @@ fn pg_pool(database_url: &str) -> SqlitePool {
let manager = ConnectionManager::<SqliteConnection>::new(database_url);
Pool::new(manager).expect("Sqlite connection pool could not be created")
}
/* begin forum stuff. todo: refactor into a separate file */
#[derive(Debug, Queryable)]
pub struct Forum {
pub id: i32,
pub name: String,
pub description: String,
pub minimum_permission_level: i32
}
pub fn get_forums(session: &Session) -> QueryResult<Vec<Forum>> {
use schema::forums::dsl::*;
return forums.select((id, name, description, minimum_permission_level)).load(&session.db);
}
pub fn add_forum(session: &Session, forum_name: &String, forum_description: &String) {
use schema::forums::dsl::*;
diesel::insert_into(forums)
.values((
name.eq(forum_name),
description.eq(forum_description),
minimum_permission_level.eq(0)
)).execute(&session.db).expect("whoops inserting into forums failed");
}

View File

@ -0,0 +1,12 @@
@use templates::page_base;
@use Session;
@use session::Forum;
@(session: &Session, forums: &Vec<Forum>, _next: Option<String>, _message: Option<&str>)
@:page_base(session, "forum_list", {
<ul>@for forum in forums {
<li>@forum.name: @forum.description</li>
}</ul>
</form>
})