[wip] paste: Add route to create pastes

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-06-25 18:12:36 +05:30
parent 0556a03396
commit 0e5abac774
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
11 changed files with 96 additions and 16 deletions

11
Cargo.lock generated
View File

@ -614,6 +614,7 @@ dependencies = [
"slog", "slog",
"slog-async", "slog-async",
"slog-term", "slog-term",
"uuid",
] ]
[[package]] [[package]]
@ -1466,6 +1467,16 @@ dependencies = [
"percent-encoding 1.0.1", "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]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.10" version = "0.2.10"

View File

@ -20,3 +20,4 @@ slog = "2.5.2"
slog-term = "2.6.0" slog-term = "2.6.0"
slog-async = "2.5.0" slog-async = "2.5.0"
chrono = "0.4.11" chrono = "0.4.11"
uuid = { version = "0.8", features = ["serde", "v4"] }

View File

@ -3,13 +3,10 @@ use std::result::Result;
use chrono::prelude::*; use chrono::prelude::*;
use jsonwebtoken::{Header, Algorithm, EncodingKey, Validation, DecodingKey}; use jsonwebtoken::{Header, Algorithm, EncodingKey, Validation, DecodingKey};
use rocket::request::{FromRequest, Outcome}; use rocket::request::{FromRequest, Outcome};
use rocket::http::{Status, ContentType}; use rocket::{Request};
use rocket::{Request, response, Response};
use crate::utils::errors::Error; use crate::utils::errors::Error;
use crate::utils::errors::ErrorCode; use crate::utils::errors::ErrorCode;
use rocket::response::Responder;
use std::io::Cursor;
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct Claims { pub struct Claims {

View File

@ -1,12 +1,13 @@
use rocket::Rocket; use rocket::Rocket;
pub mod health; pub mod health;
pub mod paste;
pub mod responder;
pub fn fuel(rocket: Rocket) -> Rocket { pub fn fuel(rocket: Rocket) -> Rocket {
let mut rocket = rocket; let mut rocket = rocket;
rocket = health::fuel(rocket); rocket = health::fuel(rocket);
rocket = paste::fuel(rocket);
rocket rocket
} }

View File

@ -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 = "<paste>")]
fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
// 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])
}

View File

View File

@ -1,10 +1,12 @@
use diesel::{RunQueryDsl};
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::result::Error; use diesel::result::Error;
use super::entity::Paste;
use crate::schema::pastes;
use diesel::{RunQueryDsl, QueryResult};
pub fn create(paste: &Paste, conn: &PgConnection) -> Result<usize, Error> { use crate::schema::pastes;
use super::entity::Paste;
pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize, Error> {
diesel::insert_into(pastes::table) diesel::insert_into(pastes::table)
.values(paste) .values(paste)
.execute(conn) .execute(conn)

View File

@ -3,8 +3,8 @@ use crate::schema::pastes;
#[table_name="pastes"] #[table_name="pastes"]
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] #[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
pub struct Paste { pub struct Paste {
id: String, pub id: Option<String>,
belongs_to: String, pub belongs_to: String,
is_url: bool, pub is_url: bool,
content: String pub content: String
} }

View File

@ -2,7 +2,6 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
#[macro_use]
extern crate rocket_contrib; extern crate rocket_contrib;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
@ -11,6 +10,8 @@ extern crate diesel_migrations;
#[macro_use] #[macro_use]
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
extern crate serde_json;
#[macro_use]
extern crate slog; extern crate slog;
pub mod api; pub mod api;

View File

@ -1,2 +1,3 @@
pub mod errors; pub mod errors;
pub mod db; pub mod db;
pub mod phonetic_key;

20
src/utils/phonetic_key.rs Normal file
View File

@ -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()
}