routes: pastes: Finish impl of paste creation

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
sphericalkat committed 2020-06-26 00:19:20 +05:30
1 parent 0e5abac774
commit 8e44b02235
15 files changed
+141 -22

No files matched your search

+3 -3
View File
@@ -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 -2
View File
@@ -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)
}
View File
Whitespace-only changes.
+9
View File
@@ -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)
}
View File
Whitespace-only changes.
+10
View File
@@ -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 -2
View File
@@ -1,4 +1,3 @@
pub mod diesel;
pub mod postgres;
pub mod entity;
pub mod repository;
pub mod service;
+19
View File
@@ -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)
}
View File
Whitespace-only changes.
+36
View File
@@ -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)
}