start working on posts
This commit is contained in:
parent
314c1bec7d
commit
bfb63afa7e
|
@ -597,6 +597,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"base64",
|
||||
"image",
|
||||
"markdown",
|
||||
"rand",
|
||||
"rand_hc",
|
||||
"regex",
|
||||
|
@ -1095,6 +1096,15 @@ dependencies = [
|
|||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "markdown"
|
||||
version = "1.0.0-alpha.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0f0025e8c0d89b84d6dc63e859475e40e8e82ab1a08be0a93ad5731513a508"
|
||||
dependencies = [
|
||||
"unicode-id",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.1.0"
|
||||
|
@ -2589,6 +2599,12 @@ version = "0.3.14"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-id"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1b6def86329695390197b82c1e244a54a131ceb66c996f2088a3876e2ae083f"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
base64 = "0.21.7"
|
||||
image = "0.24.8"
|
||||
markdown = "1.0.0-alpha.16"
|
||||
rand = "0.8.5"
|
||||
rand_hc = "0.3.2"
|
||||
regex = "1.10.3"
|
||||
|
|
|
@ -626,6 +626,13 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
|
|||
))
|
||||
.await;
|
||||
|
||||
let _ = conn
|
||||
.execute(sqlx::query(
|
||||
"ALTER TABLE posts
|
||||
ADD COLUMN IF NOT EXISTS uuid TEXT NOT NULL UNIQUE,
|
||||
ADD COLUMN IF NOT EXISTS text_id TEXT NOT NULL UNIQUE",
|
||||
))
|
||||
.await;
|
||||
rocket
|
||||
}
|
||||
|
||||
|
|
275
src/tables.rs
275
src/tables.rs
|
@ -17,23 +17,34 @@ pub struct Db(PgPool);
|
|||
#[derive(FromRow)]
|
||||
pub struct Post {
|
||||
pub id: i32,
|
||||
pub uuid: String,
|
||||
pub text_id: String,
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub published: bool,
|
||||
}
|
||||
impl Post {
|
||||
pub async fn create(mut db: Connection<Db>, title: String, body: String, published: bool) {
|
||||
pub async fn create(
|
||||
mut db: Connection<Db>,
|
||||
title: String, /*ex: Why Trans People Deserve All Your Money*/
|
||||
body: String, /*ex: # Because we're cooler than you */
|
||||
published: bool,
|
||||
uuid: String,
|
||||
text_id: String, /*ex: why-trans-people-deserve-all-your-money */
|
||||
) {
|
||||
match db
|
||||
.fetch_all(
|
||||
sqlx::query(
|
||||
r#"
|
||||
INSERT INTO posts (title, body, published)
|
||||
VALUES ($1, $2, $3);
|
||||
INSERT INTO posts (title, body, published, uuid, text_id)
|
||||
VALUES ($1, $2, $3, $4, $5);
|
||||
"#,
|
||||
)
|
||||
.bind(title)
|
||||
.bind(body)
|
||||
.bind(published),
|
||||
.bind(published)
|
||||
.bind(uuid)
|
||||
.bind(text_id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
@ -50,12 +61,204 @@ impl Post {
|
|||
.unwrap();
|
||||
Post {
|
||||
id: res.get::<i32, _>("id"),
|
||||
uuid: res.get::<String, _>("uuid"),
|
||||
text_id: res.get::<String, _>("text_id"),
|
||||
title: res.get::<String, _>("title"),
|
||||
body: res.get::<String, _>("body"),
|
||||
published: res.get::<bool, _>("published"),
|
||||
}
|
||||
}
|
||||
pub async fn get_by_uuid(mut db: Connection<Db>, uuid: String) -> Post {
|
||||
let res = db
|
||||
.fetch_one(sqlx::query("SELECT * FROM posts WHERE uuid = $1;").bind(uuid))
|
||||
.await
|
||||
.unwrap();
|
||||
Post {
|
||||
id: res.get::<i32, _>("id"),
|
||||
uuid: res.get::<String, _>("uuid"),
|
||||
text_id: res.get::<String, _>("text_id"),
|
||||
title: res.get::<String, _>("title"),
|
||||
body: res.get::<String, _>("body"),
|
||||
published: res.get::<bool, _>("published"),
|
||||
}
|
||||
}
|
||||
pub async fn get_by_text_id(mut db: Connection<Db>, text_id: String) -> Post {
|
||||
let res = db
|
||||
.fetch_one(sqlx::query("SELECT * FROM posts WHERE text_id = $1;").bind(text_id))
|
||||
.await
|
||||
.unwrap();
|
||||
Post {
|
||||
id: res.get::<i32, _>("id"),
|
||||
uuid: res.get::<String, _>("uuid"),
|
||||
text_id: res.get::<String, _>("text_id"),
|
||||
title: res.get::<String, _>("title"),
|
||||
body: res.get::<String, _>("body"),
|
||||
published: res.get::<bool, _>("published"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn edit_body(
|
||||
mut db: Connection<Db>,
|
||||
id: i32,
|
||||
new_body: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET body = $1 WHERE id = $2;")
|
||||
.bind(new_body)
|
||||
.bind(id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_body_by_uuid(
|
||||
mut db: Connection<Db>,
|
||||
uuid: String,
|
||||
new_body: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET body = $1 WHERE uuid = $2;")
|
||||
.bind(new_body)
|
||||
.bind(uuid),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_body_by_text_id(
|
||||
mut db: Connection<Db>,
|
||||
text_id: String,
|
||||
new_body: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET body = $1 WHERE text_id = $2;")
|
||||
.bind(new_body)
|
||||
.bind(text_id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_title(
|
||||
mut db: Connection<Db>,
|
||||
id: i32,
|
||||
new_title: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET title = $1 WHERE id = $2;")
|
||||
.bind(new_title)
|
||||
.bind(id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_title_by_uuid(
|
||||
mut db: Connection<Db>,
|
||||
uuid: String,
|
||||
new_title: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET title = $1 WHERE uuid = $2;")
|
||||
.bind(new_title)
|
||||
.bind(uuid),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_title_by_text_id(
|
||||
mut db: Connection<Db>,
|
||||
text_id: String,
|
||||
new_title: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET body = $1 WHERE text_id = $2;")
|
||||
.bind(new_title)
|
||||
.bind(text_id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_text_id(
|
||||
mut db: Connection<Db>,
|
||||
id: i32,
|
||||
new_text_id: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET text_id = $1 WHERE id = $2;")
|
||||
.bind(new_text_id)
|
||||
.bind(id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn edit_text_id_by_uuid(
|
||||
mut db: Connection<Db>,
|
||||
uuid: String,
|
||||
new_text_id: String,
|
||||
) -> Result<(), String> {
|
||||
match db
|
||||
.execute(
|
||||
sqlx::query("UPDATE posts SET text_id = $1 WHERE uuid = $2;")
|
||||
.bind(new_text_id)
|
||||
.bind(uuid),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("Couldn't update post".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromRow, Debug)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
|
@ -70,11 +273,15 @@ pub struct User {
|
|||
pub enum LoginStatus {
|
||||
InvalidToken,
|
||||
NotLoggedIn,
|
||||
LoggedIn(User)
|
||||
LoggedIn(User),
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub async fn create(db: &mut Connection<Db>, username: &String, password: &String) -> Result<String, String> {
|
||||
pub async fn create(
|
||||
db: &mut Connection<Db>,
|
||||
username: &String,
|
||||
password: &String,
|
||||
) -> Result<String, String> {
|
||||
match Regex::new(r"[^A-Za-z0-9_]") {
|
||||
Ok(r) => {
|
||||
match r.captures(username) {
|
||||
|
@ -112,7 +319,7 @@ impl User {
|
|||
}
|
||||
Err(why) => {
|
||||
eprintln!("Couldn't compile name regex: {why:?}");
|
||||
Err("Couldn't compile name regex.".to_string())
|
||||
Err("Couldn't compile name regex.".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -226,17 +433,11 @@ impl User {
|
|||
|
||||
pub async fn login_status(db: &mut Connection<Db>, cookies: &CookieJar<'_>) -> LoginStatus {
|
||||
match cookies.get_private("token") {
|
||||
Some(t) => {
|
||||
match User::get_by_token(db, t).await {
|
||||
Some(user) => {
|
||||
LoginStatus::LoggedIn(user)
|
||||
},
|
||||
None => {
|
||||
LoginStatus::InvalidToken
|
||||
}
|
||||
}
|
||||
Some(t) => match User::get_by_token(db, t).await {
|
||||
Some(user) => LoginStatus::LoggedIn(user),
|
||||
None => LoginStatus::InvalidToken,
|
||||
},
|
||||
None => LoginStatus::NotLoggedIn
|
||||
None => LoginStatus::NotLoggedIn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -262,12 +463,10 @@ impl Image {
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) =>
|
||||
Ok(())
|
||||
,
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => {
|
||||
eprintln!("Couldn't create database entry: {why:?}");
|
||||
Err("Couldn't create image.".to_string())
|
||||
Err("Couldn't create image.".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -277,25 +476,30 @@ impl Image {
|
|||
.await
|
||||
{
|
||||
Ok(img) => Ok(Image {
|
||||
id: img.get::<i32, _>("id"),
|
||||
uuid: img.get::<String, _>("uuid"),
|
||||
owner_name: img.get::<String, _>("owner_name"),
|
||||
}),
|
||||
id: img.get::<i32, _>("id"),
|
||||
uuid: img.get::<String, _>("uuid"),
|
||||
owner_name: img.get::<String, _>("owner_name"),
|
||||
}),
|
||||
Err(_) => Err("Couldn't get image.".to_string()),
|
||||
}
|
||||
}
|
||||
pub async fn is_owned_by(db: &mut Connection<Db>, uuid: &String, username: &String) -> Result<bool, String> {
|
||||
pub async fn is_owned_by(
|
||||
db: &mut Connection<Db>,
|
||||
uuid: &String,
|
||||
username: &String,
|
||||
) -> Result<bool, String> {
|
||||
match Image::get_by_uuid(db, uuid).await {
|
||||
Ok(img) => {
|
||||
Ok(&img.owner_name == username)
|
||||
},
|
||||
Ok(img) => Ok(&img.owner_name == username),
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
Err("couldn't get image".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn get_by_username(db: &mut Connection<Db>, owner_name: &String) -> Result<Vec<Image>, String> {
|
||||
pub async fn get_by_username(
|
||||
db: &mut Connection<Db>,
|
||||
owner_name: &String,
|
||||
) -> Result<Vec<Image>, String> {
|
||||
match db
|
||||
.fetch_all(sqlx::query("SELECT * FROM images WHERE owner_name = $1;").bind(owner_name))
|
||||
.await
|
||||
|
@ -310,16 +514,19 @@ impl Image {
|
|||
})
|
||||
}
|
||||
Ok(res)
|
||||
},
|
||||
}
|
||||
Err(_) => Err("Couldn't get image.".to_string()),
|
||||
}
|
||||
}
|
||||
pub async fn delete(db: &mut Connection<Db>, uuid: &String) -> Result<(), ()> {
|
||||
match db.execute(sqlx::query("DELETE FROM images WHERE uuid = $1").bind(uuid)).await {
|
||||
match db
|
||||
.execute(sqlx::query("DELETE FROM images WHERE uuid = $1").bind(uuid))
|
||||
.await
|
||||
{
|
||||
Ok(rows_gone) => {
|
||||
eprintln!("Deleted {rows_gone:?} rows.");
|
||||
Ok(())
|
||||
},
|
||||
Ok(())
|
||||
}
|
||||
Err(why) => {
|
||||
eprintln!("Couldn't remove database entry: {why:?}");
|
||||
Err(())
|
||||
|
|
Loading…
Reference in New Issue