routes: pastes: Finish impl of paste creation
Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
15 files changed
+141
-22
No files matched your search
+22
-9
@@ -1,17 +1,19 @@
|
||||
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;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use rocket::{http::{Cookie, Cookies, Status}, response::status, Rocket};
|
||||
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::users::{service::create_or_fetch_user};
|
||||
use crate::utils::{db, phonetic_key};
|
||||
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") {
|
||||
let user_id = match ck.get_private("session") {
|
||||
Some(c) => c.value().to_string(),
|
||||
None => {
|
||||
let user_id = get_random_id();
|
||||
@@ -20,11 +22,22 @@ fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<J
|
||||
}
|
||||
};
|
||||
|
||||
// Create or fetch already existing user
|
||||
let user = match create_or_fetch_user(user_id, &conn) {
|
||||
Ok(user) => user,
|
||||
Err(e) => return status::Custom(Status::InternalServerError, Json(json!({
|
||||
"err": e.to_string(),
|
||||
"msg": "Failed to create or fetch user"
|
||||
})))
|
||||
};
|
||||
|
||||
let new_paste = paste.deref_mut();
|
||||
if new_paste.id.is_none() {
|
||||
new_paste.id = Some(phonetic_key::get_random_id());
|
||||
}
|
||||
|
||||
new_paste.belongs_to = Some(user.id);
|
||||
|
||||
match create_paste(new_paste, &conn) {
|
||||
Ok(_) => {
|
||||
status::Custom(Status::Created, Json(json!({
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::schema::pastes;
|
||||
|
||||
#[table_name="pastes"]
|
||||
#[table_name = "pastes"]
|
||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
||||
pub struct Paste {
|
||||
pub id: Option<String>,
|
||||
pub belongs_to: String,
|
||||
pub belongs_to: Option<String>,
|
||||
pub is_url: bool,
|
||||
pub content: String
|
||||
pub content: String,
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
pub mod entity;
|
||||
pub mod repository;
|
||||
pub mod diesel;
|
||||
pub mod postgres;
|
||||
pub mod service;
|
||||
@@ -1,13 +1,14 @@
|
||||
use diesel::{RunQueryDsl};
|
||||
use anyhow::Result;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::result::Error;
|
||||
use diesel::RunQueryDsl;
|
||||
|
||||
use crate::schema::pastes;
|
||||
|
||||
use super::entity::Paste;
|
||||
|
||||
pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize, Error> {
|
||||
diesel::insert_into(pastes::table)
|
||||
pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
|
||||
let rows = diesel::insert_into(pastes::table)
|
||||
.values(paste)
|
||||
.execute(conn)
|
||||
.execute(conn)?;
|
||||
Ok(rows)
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,9 @@
|
||||
use anyhow::Result;
|
||||
use diesel::pg::PgConnection;
|
||||
|
||||
use super::entity::Paste;
|
||||
use super::postgres;
|
||||
|
||||
pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
|
||||
postgres::create_paste(paste, conn)
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,10 @@
|
||||
use crate::schema::users;
|
||||
|
||||
#[table_name = "users"]
|
||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
||||
pub struct User {
|
||||
pub id: String,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub activated: Option<bool>,
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
pub mod diesel;
|
||||
pub mod postgres;
|
||||
pub mod entity;
|
||||
pub mod repository;
|
||||
pub mod service;
|
||||
@@ -0,0 +1,19 @@
|
||||
use diesel::prelude::*;
|
||||
use diesel::pg::PgConnection;
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::core::users::entity::User;
|
||||
use crate::schema::users;
|
||||
|
||||
pub fn create_user(user: &User, conn: &PgConnection) -> Result<usize> {
|
||||
let records = diesel::insert_into(users::table)
|
||||
.values(user)
|
||||
.on_conflict_do_nothing()
|
||||
.execute(conn)?;
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn find_user(id: String, conn: &PgConnection) -> Result<User> {
|
||||
let user = users::table.find(id).get_result::<User>(conn)?;
|
||||
Ok(user)
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,36 @@
|
||||
use anyhow::Result;
|
||||
use bcrypt::{DEFAULT_COST, hash};
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::result::Error;
|
||||
|
||||
use crate::core::users::entity::User;
|
||||
|
||||
use super::postgres;
|
||||
|
||||
pub fn create_user(user: &mut User, conn: &PgConnection) -> Result<usize> {
|
||||
let hashed_pass = hash(user.password.as_ref().unwrap().as_bytes(), DEFAULT_COST)?;
|
||||
user.password = Some(hashed_pass);
|
||||
postgres::create_user(user, conn)
|
||||
}
|
||||
|
||||
pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result<User> {
|
||||
let user = match postgres::find_user(id.clone(), conn) {
|
||||
Ok(user) => user,
|
||||
Err(err) => {
|
||||
match err.downcast_ref::<Error>() {
|
||||
Some(Error::NotFound) => {
|
||||
let new_user = User {
|
||||
id: id.clone(),
|
||||
username: None,
|
||||
password: None,
|
||||
activated: Some(false),
|
||||
};
|
||||
postgres::create_user(&new_user, conn)?;
|
||||
new_user
|
||||
}
|
||||
_ => return Err(err)
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(user)
|
||||
}
|
||||
Reference in new issue
Block a user