diff --git a/Cargo.lock b/Cargo.lock index 96c4740..3eefe94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,6 +614,7 @@ dependencies = [ "slog", "slog-async", "slog-term", + "uuid", ] [[package]] @@ -1466,6 +1467,16 @@ dependencies = [ "percent-encoding 1.0.1", ] +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand", + "serde", +] + [[package]] name = "vcpkg" version = "0.2.10" diff --git a/Cargo.toml b/Cargo.toml index 0cfc4a0..8c549b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,4 @@ slog = "2.5.2" slog-term = "2.6.0" slog-async = "2.5.0" chrono = "0.4.11" +uuid = { version = "0.8", features = ["serde", "v4"] } diff --git a/src/api/guards/auth.rs b/src/api/guards/auth.rs index 6a0f1f7..1f3dcc5 100644 --- a/src/api/guards/auth.rs +++ b/src/api/guards/auth.rs @@ -3,13 +3,10 @@ use std::result::Result; use chrono::prelude::*; use jsonwebtoken::{Header, Algorithm, EncodingKey, Validation, DecodingKey}; use rocket::request::{FromRequest, Outcome}; -use rocket::http::{Status, ContentType}; -use rocket::{Request, response, Response}; +use rocket::{Request}; use crate::utils::errors::Error; use crate::utils::errors::ErrorCode; -use rocket::response::Responder; -use std::io::Cursor; #[derive(Serialize, Deserialize, Clone)] pub struct Claims { diff --git a/src/api/routes/mod.rs b/src/api/routes/mod.rs index 9390295..d98a57d 100644 --- a/src/api/routes/mod.rs +++ b/src/api/routes/mod.rs @@ -1,12 +1,13 @@ use rocket::Rocket; pub mod health; +pub mod paste; +pub mod responder; pub fn fuel(rocket: Rocket) -> Rocket { let mut rocket = rocket; - rocket = health::fuel(rocket); - + rocket = paste::fuel(rocket); rocket } diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index e69de29..bd45081 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -0,0 +1,46 @@ +use rocket::{Rocket, response::status, http::{Status, Cookies, Cookie}}; +use rocket_contrib::json::Json; +use crate::core::paste::{entity::Paste, diesel::create_paste}; +use serde_json::Value; +use crate::utils::{db, phonetic_key}; +use std::ops::{DerefMut}; +use rocket::response::status::Custom; +use crate::utils::phonetic_key::get_random_id; + + +#[post("/", data = "")] +fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom> { + // Check if frontend sent a session cookie + let session = match ck.get_private("session") { + Some(c) => c.value().to_string(), + None => { + let user_id = get_random_id(); + ck.add_private(Cookie::new("session", user_id.clone())); + user_id + } + }; + + let new_paste = paste.deref_mut(); + if new_paste.id.is_none() { + new_paste.id = Some(phonetic_key::get_random_id()); + } + + match create_paste(new_paste, &conn) { + Ok(_) => { + status::Custom(Status::Created, Json(json!({ + "msg": "Successfully created paste", + "paste_id": new_paste.id + }))) + } + Err(e) => { + status::Custom(Status::InternalServerError, Json(json!({ + "err": e.to_string(), + "msg": "Failed to create paste" + }))) + } + } +} + +pub fn fuel(rocket: Rocket) -> Rocket { + rocket.mount("/api/paste", routes![create]) +} \ No newline at end of file diff --git a/src/api/routes/responder.rs b/src/api/routes/responder.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/core/paste/diesel.rs b/src/core/paste/diesel.rs index 85bd16c..90744f0 100644 --- a/src/core/paste/diesel.rs +++ b/src/core/paste/diesel.rs @@ -1,10 +1,12 @@ +use diesel::{RunQueryDsl}; use diesel::pg::PgConnection; use diesel::result::Error; -use super::entity::Paste; -use crate::schema::pastes; -use diesel::{RunQueryDsl, QueryResult}; -pub fn create(paste: &Paste, conn: &PgConnection) -> Result { +use crate::schema::pastes; + +use super::entity::Paste; + +pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { diesel::insert_into(pastes::table) .values(paste) .execute(conn) diff --git a/src/core/paste/entity.rs b/src/core/paste/entity.rs index 85d2701..d542b2f 100644 --- a/src/core/paste/entity.rs +++ b/src/core/paste/entity.rs @@ -3,8 +3,8 @@ use crate::schema::pastes; #[table_name="pastes"] #[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] pub struct Paste { - id: String, - belongs_to: String, - is_url: bool, - content: String + pub id: Option, + pub belongs_to: String, + pub is_url: bool, + pub content: String } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7a0380b..cbf54e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ #[macro_use] extern crate rocket; -#[macro_use] extern crate rocket_contrib; #[macro_use] extern crate diesel; @@ -11,6 +10,8 @@ extern crate diesel_migrations; #[macro_use] extern crate serde; #[macro_use] +extern crate serde_json; +#[macro_use] extern crate slog; pub mod api; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 670f9fe..2a81a13 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,3 @@ pub mod errors; -pub mod db; \ No newline at end of file +pub mod db; +pub mod phonetic_key; \ No newline at end of file diff --git a/src/utils/phonetic_key.rs b/src/utils/phonetic_key.rs new file mode 100644 index 0000000..aed53da --- /dev/null +++ b/src/utils/phonetic_key.rs @@ -0,0 +1,20 @@ +use uuid::Uuid; + +// const VOWELS: Vec<&str> = vec!["a", "e", "i", "o", "u", "y"]; +// const CONSONANTS: Vec<&str> = vec!["b", "c", "d", "f", "g", "h", "l", "m", "n", "p", "r", "s", "t", "v", "w"]; +// const UNCOMMON_CON: Vec<&str> = vec!["x", "z", "q", "j", "k"]; +// const COMMON_VOW_VOW_ST: Vec<&str> = vec!["ea", "ai", "a", "yu"]; +// const COMMON_VOW_VOW: Vec<&str> = vec!["ee", "oo", "ea", "ai", "ay", "uy"]; +// const COMMON_VOW_CON: Vec<&str> = vec!["in", "an", "ing", "im", "er", "ex", "un", "est", "ux", "am", "ap"]; +// const COMMON_VOW_CON_ST: Vec<&str> = vec!["un", "im", "in", "ex"]; +// const COMMON_CON_VOW: Vec<&str> = vec!["me", "li", "le", "ly", "pe", "re", "fi", "nu", "co", "lo", "cu", "ki", "cy", "fu", "mo", "bi"]; +// const COMMON_CON_VOW_ST: Vec<&str> = vec!["me", "li", "fu", "pe", "lo", "mo"]; +// const COMMON_CON_CON_ST: Vec<&str> = vec!["gh", "th", "gr", "st", "ph", "pr", "t", "cr"]; +// const COMMON_CON_CON: Vec<&str> = vec!["ll", "pp", "gh", "th", "gr", "ng", "st", "ph", "rr", "gn", "ck", "rf", "tt", "cr"]; +// +// const CONSONANT: i8 = 1; +// const VOWEL: i8 = 0; + +pub fn get_random_id() -> String { + Uuid::new_v4().to_string() +} \ No newline at end of file