routes: pastes: Finish implementing fetching of pastes

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-06-26 00:53:39 +05:30
parent 8e44b02235
commit f16e906ff4
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
6 changed files with 78 additions and 7 deletions

42
Cargo.lock generated
View File

@ -545,7 +545,7 @@ dependencies = [
"traitobject",
"typeable",
"unicase",
"url",
"url 1.7.2",
]
[[package]]
@ -559,6 +559,17 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "idna"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "1.3.2"
@ -646,6 +657,7 @@ dependencies = [
"slog-async",
"slog-term",
"uuid",
"validator",
]
[[package]]
@ -1493,11 +1505,22 @@ version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
dependencies = [
"idna",
"idna 0.1.5",
"matches",
"percent-encoding 1.0.1",
]
[[package]]
name = "url"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb"
dependencies = [
"idna 0.2.0",
"matches",
"percent-encoding 2.1.0",
]
[[package]]
name = "uuid"
version = "0.8.1"
@ -1508,6 +1531,21 @@ dependencies = [
"serde",
]
[[package]]
name = "validator"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e60fadf92c22236de4028ceb0b8af50ed3430d41ad43d7a7d63b6bd1a8f47c38"
dependencies = [
"idna 0.2.0",
"lazy_static",
"regex",
"serde",
"serde_derive",
"serde_json",
"url 2.1.1",
]
[[package]]
name = "vcpkg"
version = "0.2.10"

View File

@ -19,6 +19,7 @@ jsonwebtoken = "7.1.2"
slog = "2.5.2"
slog-term = "2.6.0"
anyhow = "1.0"
validator = "0.10.1"
slog-async = "2.5.0"
bcrypt = "0.8"
chrono = "0.4.11"

View File

@ -5,10 +5,11 @@ use rocket::response::status::Custom;
use rocket_contrib::json::Json;
use serde_json::Value;
use crate::core::paste::{entity::Paste, service::create_paste};
use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_paste};
use crate::core::users::{service::create_or_fetch_user};
use crate::utils::{db, phonetic_key};
use crate::utils::phonetic_key::get_random_id;
use diesel::result::Error;
#[post("/", data = "<paste>")]
fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
@ -54,6 +55,27 @@ fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<J
}
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/paste", routes![create])
#[get("/<id>")]
fn fetch(id: String, conn: db::DbConn) -> Custom<Json<Value>> {
let paste = match fetch_paste(id, &conn) {
Ok(paste) => paste,
Err(err) => {
return match err.downcast_ref::<Error>() {
Some(Error::NotFound) => Custom(Status::NotFound, Json(json!({
"err": err.to_string(),
"msg": "Unable to find a paste with that ID"
}))),
_ => Custom(Status::InternalServerError, Json(json!({
"err": err.to_string(),
"msg": "Something went wrong, try again"
})))
}
}
};
Custom(Status::Found, Json(json!(paste)))
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/paste", routes![create, fetch])
}

View File

@ -1,6 +1,6 @@
use anyhow::Result;
use diesel::pg::PgConnection;
use diesel::RunQueryDsl;
use diesel::prelude::*;
use crate::schema::pastes;
@ -12,3 +12,8 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
.execute(conn)?;
Ok(rows)
}
pub fn fetch_paste(id: String, conn: &PgConnection) -> Result<Paste> {
let paste = pastes::table.find(id).get_result::<Paste>(conn)?;
Ok(paste)
}

View File

@ -4,6 +4,11 @@ use diesel::pg::PgConnection;
use super::entity::Paste;
use super::postgres;
pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result<usize> {
paste.is_url = validator::validate_url(paste.content.clone());
postgres::create_paste(paste, conn)
}
pub fn fetch_paste(id: String, conn: &PgConnection) -> Result<Paste> {
postgres::fetch_paste(id, conn)
}

View File

@ -1,6 +1,6 @@
table! {
pastes (id) {
id -> Varchar,
id -> Nullable<Varchar>,
belongs_to -> Nullable<Varchar>,
is_url -> Bool,
content -> Text,