features/curl #6
@ -1,18 +1,28 @@
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use diesel::result::Error;
 | 
			
		||||
use rocket::{http::{Cookies, Status}, response::status, Rocket};
 | 
			
		||||
use rocket::response::status::Custom;
 | 
			
		||||
use rocket::{
 | 
			
		||||
    http::{Cookies, Status},
 | 
			
		||||
    response::status,
 | 
			
		||||
    Rocket,
 | 
			
		||||
};
 | 
			
		||||
use rocket_contrib::json::Json;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
 | 
			
		||||
use crate::{api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, utils::domain::get_domain};
 | 
			
		||||
use crate::api::guards::db::DbConn;
 | 
			
		||||
use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}};
 | 
			
		||||
use crate::core::paste::{
 | 
			
		||||
    entity::Paste,
 | 
			
		||||
    service::{create_paste, fetch_paste, update_paste},
 | 
			
		||||
};
 | 
			
		||||
use crate::core::users::entity::User;
 | 
			
		||||
use crate::core::users::service::{create_or_fetch_user, fetch_user};
 | 
			
		||||
use crate::utils::phonetic_key;
 | 
			
		||||
use crate::utils::users::get_session_id;
 | 
			
		||||
use crate::core::users::entity::User;
 | 
			
		||||
use crate::{
 | 
			
		||||
    api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity},
 | 
			
		||||
    utils::domain::get_domain,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#[post("/", data = "<paste>")]
 | 
			
		||||
fn create(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
 | 
			
		||||
@ -53,12 +63,10 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
 | 
			
		||||
    let r_user = fetch_user(user_id, &conn);
 | 
			
		||||
    let user = match r_user {
 | 
			
		||||
        Ok(user) => user,
 | 
			
		||||
        Err(e) => {
 | 
			
		||||
            match e.downcast_ref::<Error>() {
 | 
			
		||||
                Some(Error::NotFound) => User::new(),
 | 
			
		||||
                _ => return internal_server_error(),
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Err(e) => match e.downcast_ref::<Error>() {
 | 
			
		||||
            Some(Error::NotFound) => User::new(),
 | 
			
		||||
            _ => return internal_server_error(),
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let paste = match fetch_paste(id, &conn) {
 | 
			
		||||
@ -73,17 +81,20 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
 | 
			
		||||
 | 
			
		||||
    let belongs_to = paste.belongs_to.as_ref().unwrap();
 | 
			
		||||
 | 
			
		||||
    return if user.id == *belongs_to {
 | 
			
		||||
        Custom(Status::Ok, Json(json!({
 | 
			
		||||
            "id": paste.id,
 | 
			
		||||
            "belongs_to": *belongs_to,
 | 
			
		||||
            "is_url": paste.is_url.unwrap(),
 | 
			
		||||
            "content": paste.content,
 | 
			
		||||
            "is_owner": true
 | 
			
		||||
        })))
 | 
			
		||||
    if user.id == *belongs_to {
 | 
			
		||||
        Custom(
 | 
			
		||||
            Status::Ok,
 | 
			
		||||
            Json(json!({
 | 
			
		||||
                "id": paste.id,
 | 
			
		||||
                "belongs_to": *belongs_to,
 | 
			
		||||
                "is_url": paste.is_url.unwrap(),
 | 
			
		||||
                "content": paste.content,
 | 
			
		||||
                "is_owner": true
 | 
			
		||||
            })),
 | 
			
		||||
        )
 | 
			
		||||
    } else {
 | 
			
		||||
        Custom(Status::Ok, Json(json!(paste)))
 | 
			
		||||
    };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[patch("/", data = "<paste>")]
 | 
			
		||||
@ -107,7 +118,7 @@ fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
 | 
			
		||||
 | 
			
		||||
    new_paste.belongs_to = match fetch_paste(new_paste.id.as_ref().unwrap().clone(), &conn) {
 | 
			
		||||
        Ok(paste) => paste.belongs_to,
 | 
			
		||||
        Err(_) => return internal_server_error()
 | 
			
		||||
        Err(_) => return internal_server_error(),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if new_paste.belongs_to.is_some() {
 | 
			
		||||
@ -116,9 +127,9 @@ fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
 | 
			
		||||
                Ok(_) => status::Custom(
 | 
			
		||||
                    Status::Created,
 | 
			
		||||
                    Json(json!({
 | 
			
		||||
                "msg": "Successfully created paste",
 | 
			
		||||
                "paste_id": new_paste.id
 | 
			
		||||
            })),
 | 
			
		||||
                        "msg": "Successfully created paste",
 | 
			
		||||
                        "paste_id": new_paste.id
 | 
			
		||||
                    })),
 | 
			
		||||
                ),
 | 
			
		||||
                Err(_) => internal_server_error(),
 | 
			
		||||
            }
 | 
			
		||||
@ -149,6 +160,6 @@ fn anonymous(input: String, conn: DbConn) -> String {
 | 
			
		||||
 | 
			
		||||
pub fn fuel(rocket: Rocket) -> Rocket {
 | 
			
		||||
    rocket
 | 
			
		||||
    .mount("/api/paste", routes![create, fetch, update])
 | 
			
		||||
    .mount("/", routes![anonymous])
 | 
			
		||||
        .mount("/api/paste", routes![create, fetch, update])
 | 
			
		||||
        .mount("/", routes![anonymous])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,7 @@
 | 
			
		||||
use anyhow::Result;
 | 
			
		||||
use rocket::{
 | 
			
		||||
    http::Status,
 | 
			
		||||
    response::status,
 | 
			
		||||
    Rocket,
 | 
			
		||||
};
 | 
			
		||||
use rocket::http::Cookies;
 | 
			
		||||
use rocket::response::status::Custom;
 | 
			
		||||
use rocket::{http::Status, response::status, Rocket};
 | 
			
		||||
use rocket_contrib::json::Json;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
use crate::schema::pastes;
 | 
			
		||||
 | 
			
		||||
#[table_name = "pastes"]
 | 
			
		||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
 | 
			
		||||
#[table_name = "pastes"]
 | 
			
		||||
pub struct Paste {
 | 
			
		||||
    pub id: Option<String>,
 | 
			
		||||
    pub belongs_to: Option<String>,
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,9 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
 | 
			
		||||
 | 
			
		||||
pub fn update_paste(paste: &Paste, conn: &PgConnection) -> Result<Paste> {
 | 
			
		||||
    use crate::schema::pastes::dsl::*;
 | 
			
		||||
    let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap()))).set(paste).get_result(conn)?;
 | 
			
		||||
    let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap())))
 | 
			
		||||
        .set(paste)
 | 
			
		||||
        .get_result(conn)?;
 | 
			
		||||
    Ok(updated_user)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
use crate::schema::users;
 | 
			
		||||
 | 
			
		||||
#[derive(Default, AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
 | 
			
		||||
#[table_name = "users"]
 | 
			
		||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
 | 
			
		||||
pub struct User {
 | 
			
		||||
    pub id: String,
 | 
			
		||||
    pub username: Option<String>,
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result<User> {
 | 
			
		||||
        Err(err) => match err.downcast_ref::<Error>() {
 | 
			
		||||
            Some(Error::NotFound) => {
 | 
			
		||||
                let new_user = User {
 | 
			
		||||
                    id: id.clone(),
 | 
			
		||||
                    id,
 | 
			
		||||
                    username: None,
 | 
			
		||||
                    password: None,
 | 
			
		||||
                    activated: Some(false),
 | 
			
		||||
 | 
			
		||||
@ -15,8 +15,6 @@ extern crate serde_json;
 | 
			
		||||
extern crate slog;
 | 
			
		||||
 | 
			
		||||
use slog::{Drain, Logger};
 | 
			
		||||
use slog_async;
 | 
			
		||||
use slog_term;
 | 
			
		||||
 | 
			
		||||
pub mod api;
 | 
			
		||||
pub mod core;
 | 
			
		||||
@ -40,7 +38,7 @@ fn main() {
 | 
			
		||||
    let drain = slog_async::Async::new(drain).build().fuse();
 | 
			
		||||
    let logger = slog::Logger::root(drain, o!());
 | 
			
		||||
 | 
			
		||||
    // run_migrations(&logger);
 | 
			
		||||
    run_migrations(&logger);
 | 
			
		||||
 | 
			
		||||
    let mut rocket = rocket::ignite();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,10 @@ pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
 | 
			
		||||
 | 
			
		||||
pub fn pool() -> Pool {
 | 
			
		||||
    let manager = ConnectionManager::<PgConnection>::new(database_url());
 | 
			
		||||
    let pool_size = env::var("KATBIN_POOL_SIZE").unwrap().parse::<u32>().expect("pool size");
 | 
			
		||||
    let pool_size = env::var("KATBIN_POOL_SIZE")
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .parse::<u32>()
 | 
			
		||||
        .expect("pool size");
 | 
			
		||||
    Pool::builder().max_size(pool_size).build(manager).unwrap()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,8 @@
 | 
			
		||||
use std::env;
 | 
			
		||||
 | 
			
		||||
pub fn get_domain() -> String {
 | 
			
		||||
    env::var("KATBIN_DOMAIN_NAME").unwrap().parse::<String>().expect("domain name")
 | 
			
		||||
}
 | 
			
		||||
    env::var("KATBIN_DOMAIN_NAME")
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .parse::<String>()
 | 
			
		||||
        .expect("domain name")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -64,7 +64,7 @@ impl Error {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn set_msg(mut self) -> Self {
 | 
			
		||||
        self.msg = match self.code.clone() {
 | 
			
		||||
        self.msg = match self.code {
 | 
			
		||||
            ErrorCode::InvalidCredentials => "invalid credentials were provided".to_string(),
 | 
			
		||||
            ErrorCode::MultipleAuthToken => {
 | 
			
		||||
                "multiple authorization tokens were provided".to_string()
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
pub mod db;
 | 
			
		||||
pub mod domain;
 | 
			
		||||
pub mod errors;
 | 
			
		||||
pub mod phonetic_key;
 | 
			
		||||
pub mod users;
 | 
			
		||||
pub mod domain;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user