diff --git a/backend/migrations/2022-04-17-175115_user-system/down.sql b/backend/migrations/2022-04-17-175115_user-system/down.sql index f006311..78614ae 100644 --- a/backend/migrations/2022-04-17-175115_user-system/down.sql +++ b/backend/migrations/2022-04-17-175115_user-system/down.sql @@ -1,4 +1,4 @@ -- This file should undo anything in `up.sql` -drop table user; -drop table pwd; \ No newline at end of file +drop table pwd; +drop table user; \ No newline at end of file diff --git a/backend/migrations/2022-04-17-175115_user-system/up.sql b/backend/migrations/2022-04-17-175115_user-system/up.sql index d0ba8b7..4faf5e0 100644 --- a/backend/migrations/2022-04-17-175115_user-system/up.sql +++ b/backend/migrations/2022-04-17-175115_user-system/up.sql @@ -6,6 +6,7 @@ CREATE TABLE user ( ); CREATE TABLE pwd ( - id INTEGER NOT NULL PRIMARY KEY, - password TEXT NOT NULL + user_id INTEGER NOT NULL PRIMARY KEY, + password TEXT NOT NULL, + FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE ); \ No newline at end of file diff --git a/backend/src/api.rs b/backend/src/api.rs index 4be800c..3558a8d 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -8,23 +8,14 @@ use jsonwebtoken::DecodingKey; use jsonwebtoken::Validation; use jsonwebtoken::{EncodingKey, Header}; use rocket::http::Status; -use rocket::outcome::Outcome::{Failure, Success}; use rocket::request::Outcome; -use rocket::request::{self, FromRequest, Request}; +use rocket::request::{FromRequest, Request}; use rocket::serde::json::{json, Json, Value}; use rocket::State; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use validator::ValidateArgs; -pub struct Referer(String); - -#[derive(Debug)] -pub enum ReferrerError { - Missing, - MoreThanOne, -} - #[derive(Debug, Responder)] pub enum ApiResponseVariant { Status(Status), @@ -33,20 +24,6 @@ pub enum ApiResponseVariant { // Flash(Flash) } -#[rocket::async_trait] -impl<'r> FromRequest<'r> for Referer { - type Error = ReferrerError; - - async fn from_request(req: &'r Request<'_>) -> request::Outcome { - let referers: Vec<_> = req.headers().get("Referer").collect(); - match referers.len() { - 0 => Failure((Status::BadRequest, ReferrerError::Missing)), - 1 => Success(Referer(referers[0].to_string())), - _ => Failure((Status::BadRequest, ReferrerError::MoreThanOne)), - } - } -} - #[derive(Serialize, Deserialize, Debug)] struct ApiResponse { result: Cow<'static, str>, diff --git a/backend/src/main.rs b/backend/src/main.rs index f46caf8..6e10b8d 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -45,8 +45,15 @@ fn rocket() -> _ { .attach(Template::fairing()) .attach(AdHoc::on_ignite("Run Migrations", schema::run_migrations)) .attach(AdHoc::config::()) - .attach(site::make_cors()) - .mount("/", routes![site::index, site::files]) + .mount( + "/", + routes![ + site::index, + site::gamenights, + site::add_game_night, + site::register + ], + ) .mount( "/api", routes![ diff --git a/backend/src/schema.rs b/backend/src/schema.rs index 69e411e..28c4efb 100644 --- a/backend/src/schema.rs +++ b/backend/src/schema.rs @@ -104,7 +104,7 @@ pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), Databas Err(error) => return Err(DatabaseError::Hash(error)), }; - match conn + let user_insert_result = conn .run(move |c| { c.transaction(|| { diesel::insert_into(user::table) @@ -133,8 +133,9 @@ pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), Databas .execute(c) }) }) - .await - { + .await; + + match user_insert_result { Err(e) => Err(DatabaseError::Query(e.to_string())), _ => Ok(()), } diff --git a/backend/src/site.rs b/backend/src/site.rs index a5cbaa2..425cc14 100644 --- a/backend/src/site.rs +++ b/backend/src/site.rs @@ -1,42 +1,90 @@ -use rocket::fs::NamedFile; -use rocket::http::Method; -use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions}; -use std::io; -use std::path::{Path, PathBuf}; +use crate::schema; +use rocket::request::FlashMessage; +use rocket::response::Redirect; +use rocket_dyn_templates::Template; +use serde::{Deserialize, Serialize}; +use std::borrow::Cow; -pub fn make_cors() -> Cors { - let allowed_origins = AllowedOrigins::some_exact(&[ - // 4. - //CHANGE THESE TO MATCH YOUR PORTS - "http://localhost:3000", - "http://127.0.0.1:3000", - "http://localhost:8000", - "http://0.0.0.0:8000", - ]); - CorsOptions { - // 5. - allowed_origins, - allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(), // 1. - allowed_headers: AllowedHeaders::some(&[ - "Authorization", - "Accept", - "Access-Control-Allow-Origin", // 6. - ]), - allow_credentials: true, - ..Default::default() - } - .to_cors() - .expect("error while building CORS") +#[derive(Serialize, Deserialize, Debug)] +struct FlashData { + has_data: bool, + kind: Cow<'static, str>, + message: Cow<'static, str>, } -#[get("/")] -pub async fn files(file: PathBuf) -> Option { - NamedFile::open(Path::new("../frontend/build/").join(file)) - .await - .ok() +impl FlashData { + const EMPTY: Self = Self { + has_data: false, + message: Cow::Borrowed(""), + kind: Cow::Borrowed(""), + }; +} + +#[derive(Serialize, Deserialize, Debug)] +struct GameNightsData { + gamenights: Vec, + flash: FlashData, +} + +#[get("/gamenights")] +pub async fn gamenights(conn: schema::DbConn) -> Template { + let gamenights = schema::get_all_gamenights(conn).await; + + let data = GameNightsData { + gamenights: gamenights, + flash: FlashData::EMPTY, + }; + + Template::render("gamenights", &data) } #[get("/")] -pub async fn index() -> io::Result { - NamedFile::open("../frontend/build/index.html").await +pub async fn index() -> Redirect { + Redirect::to(uri!(gamenights)) +} + +#[derive(Serialize, Deserialize, Debug)] +struct GameNightAddData { + post_url: String, + flash: FlashData, +} + +#[get("/gamenight/add")] +pub async fn add_game_night(flash: Option>) -> Template { + let flash_data = match flash { + None => FlashData::EMPTY, + Some(flash) => FlashData { + has_data: true, + message: Cow::Owned(flash.message().to_string()), + kind: Cow::Owned(flash.kind().to_string()), + }, + }; + + let data = GameNightAddData { + post_url: "/api/gamenight".to_string(), + flash: flash_data, + }; + + Template::render("gamenight_add", &data) +} + +#[derive(Serialize, Deserialize, Debug)] +struct RegisterData { + flash: FlashData, +} + +#[get("/register")] +pub async fn register(flash: Option>) -> Template { + let flash_data = match flash { + None => FlashData::EMPTY, + Some(flash) => FlashData { + has_data: true, + message: Cow::Owned(flash.message().to_string()), + kind: Cow::Owned(flash.kind().to_string()), + }, + }; + + let data = RegisterData { flash: flash_data }; + + Template::render("register", &data) } diff --git a/backend/templates/register.html.hbs b/backend/templates/register.html.hbs index 25e6644..53a9bd3 100644 --- a/backend/templates/register.html.hbs +++ b/backend/templates/register.html.hbs @@ -16,4 +16,4 @@ -1 \ No newline at end of file + \ No newline at end of file diff --git a/frontend/public/index.html b/frontend/public/index.html index 0ebc098..aa069f2 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -1,4 +1,4 @@ -ex + diff --git a/backend/readme.md b/readme.md similarity index 100% rename from backend/readme.md rename to readme.md