[wip] paste: Add route to create pastes
Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
11 files changed
+96
-16
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
Whitespace-only changes.
@@ -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<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)
|
||||
.values(paste)
|
||||
.execute(conn)
|
||||
|
||||
@@ -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<String>,
|
||||
pub belongs_to: String,
|
||||
pub is_url: bool,
|
||||
pub content: String
|
||||
}
|
||||
+2
-1
@@ -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;
|
||||
|
||||
+2
-1
@@ -1,2 +1,3 @@
|
||||
pub mod errors;
|
||||
pub mod db;
|
||||
pub mod db;
|
||||
pub mod phonetic_key;
|
||||
@@ -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()
|
||||
}
|
||||
Reference in new issue
Block a user