Add post_read back in, do some cleanup

This commit is contained in:
Felix Ableitner 2020-08-12 13:47:32 +02:00
parent afdbed30e5
commit d95bdd61a4
12 changed files with 76 additions and 82 deletions

View file

@ -34,10 +34,6 @@ impl Crud<ActivityForm> for Activity {
activity.find(activity_id).first::<Self>(conn) activity.find(activity_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _activity_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
use crate::schema::activity::dsl::*; use crate::schema::activity::dsl::*;
insert_into(activity) insert_into(activity)

View file

@ -23,10 +23,6 @@ impl Crud<CategoryForm> for Category {
category.find(category_id).first::<Self>(conn) category.find(category_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _category_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
insert_into(category) insert_into(category)
.values(new_category) .values(new_category)

View file

@ -172,10 +172,6 @@ pub struct CommentLikeForm {
} }
impl Likeable<CommentLikeForm> for CommentLike { impl Likeable<CommentLikeForm> for CommentLike {
fn read(_conn: &PgConnection, _comment_id_from: i32) -> Result<Vec<Self>, Error> {
unimplemented!()
}
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> { fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::*; use crate::schema::comment_like::dsl::*;
insert_into(comment_like) insert_into(comment_like)

View file

@ -50,9 +50,12 @@ pub trait Crud<T> {
fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
where where
Self: Sized; Self: Sized,
{
unimplemented!()
}
} }
pub trait Followable<T> { pub trait Followable<T> {
@ -74,9 +77,6 @@ pub trait Joinable<T> {
} }
pub trait Likeable<T> { pub trait Likeable<T> {
fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
where
Self: Sized;
fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;

View file

@ -41,10 +41,6 @@ impl Crud<ModRemovePostForm> for ModRemovePost {
mod_remove_post.find(from_id).first::<Self>(conn) mod_remove_post.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*; use crate::schema::mod_remove_post::dsl::*;
insert_into(mod_remove_post) insert_into(mod_remove_post)
@ -84,10 +80,6 @@ impl Crud<ModLockPostForm> for ModLockPost {
mod_lock_post.find(from_id).first::<Self>(conn) mod_lock_post.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*; use crate::schema::mod_lock_post::dsl::*;
insert_into(mod_lock_post) insert_into(mod_lock_post)
@ -127,10 +119,6 @@ impl Crud<ModStickyPostForm> for ModStickyPost {
mod_sticky_post.find(from_id).first::<Self>(conn) mod_sticky_post.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
use crate::schema::mod_sticky_post::dsl::*; use crate::schema::mod_sticky_post::dsl::*;
insert_into(mod_sticky_post) insert_into(mod_sticky_post)
@ -172,10 +160,6 @@ impl Crud<ModRemoveCommentForm> for ModRemoveComment {
mod_remove_comment.find(from_id).first::<Self>(conn) mod_remove_comment.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*; use crate::schema::mod_remove_comment::dsl::*;
insert_into(mod_remove_comment) insert_into(mod_remove_comment)
@ -219,10 +203,6 @@ impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
mod_remove_community.find(from_id).first::<Self>(conn) mod_remove_community.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*; use crate::schema::mod_remove_community::dsl::*;
insert_into(mod_remove_community) insert_into(mod_remove_community)
@ -272,10 +252,6 @@ impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
mod_ban_from_community.find(from_id).first::<Self>(conn) mod_ban_from_community.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*; use crate::schema::mod_ban_from_community::dsl::*;
insert_into(mod_ban_from_community) insert_into(mod_ban_from_community)
@ -323,10 +299,6 @@ impl Crud<ModBanForm> for ModBan {
mod_ban.find(from_id).first::<Self>(conn) mod_ban.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*; use crate::schema::mod_ban::dsl::*;
insert_into(mod_ban).values(form).get_result::<Self>(conn) insert_into(mod_ban).values(form).get_result::<Self>(conn)
@ -366,10 +338,6 @@ impl Crud<ModAddCommunityForm> for ModAddCommunity {
mod_add_community.find(from_id).first::<Self>(conn) mod_add_community.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*; use crate::schema::mod_add_community::dsl::*;
insert_into(mod_add_community) insert_into(mod_add_community)
@ -409,10 +377,6 @@ impl Crud<ModAddForm> for ModAdd {
mod_add.find(from_id).first::<Self>(conn) mod_add.find(from_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _from_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*; use crate::schema::mod_add::dsl::*;
insert_into(mod_add).values(form).get_result::<Self>(conn) insert_into(mod_add).values(form).get_result::<Self>(conn)

View file

@ -28,9 +28,6 @@ impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
.find(password_reset_request_id) .find(password_reset_request_id)
.first::<Self>(conn) .first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _password_reset_request_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
insert_into(password_reset_request) insert_into(password_reset_request)
.values(form) .values(form)

View file

@ -1,10 +1,4 @@
use crate::{ use crate::{naive_now, schema::{post, post_like, post_read, post_saved}, Crud, Likeable, Saveable, Readable};
naive_now,
schema::{post, post_like, post_saved},
Crud,
Likeable,
Saveable,
};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::{ParseError, Url}; use url::{ParseError, Url};
@ -200,9 +194,6 @@ pub struct PostLikeForm {
} }
impl Likeable<PostLikeForm> for PostLike { impl Likeable<PostLikeForm> for PostLike {
fn read(_conn: &PgConnection, _post_id_from: i32) -> Result<Vec<Self>, Error> {
unimplemented!()
}
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> { fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use crate::schema::post_like::dsl::*; use crate::schema::post_like::dsl::*;
insert_into(post_like) insert_into(post_like)
@ -255,6 +246,46 @@ impl Saveable<PostSavedForm> for PostSaved {
} }
} }
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Post)]
#[table_name = "post_read"]
pub struct PostRead {
pub id: i32,
pub post_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "post_read"]
pub struct PostReadForm {
pub post_id: i32,
pub user_id: i32,
}
impl Readable<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use crate::schema::post_read::dsl::*;
insert_into(post_read)
.values(post_read_form)
.get_result::<Self>(conn)
}
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
use crate::schema::post_read::dsl::*;
diesel::delete(
post_read
.filter(post_id.eq(post_read_form.post_id))
.filter(user_id.eq(post_read_form.user_id)),
)
.execute(conn)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
@ -397,10 +428,26 @@ mod tests {
published: inserted_post_saved.published, published: inserted_post_saved.published,
}; };
// Post Read
let post_read_form = PostReadForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
};
let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
let expected_post_read = PostRead {
id: inserted_post_read.id,
post_id: inserted_post.id,
user_id: inserted_user.id,
published: inserted_post_read.published,
};
let read_post = Post::read(&conn, inserted_post.id).unwrap(); let read_post = Post::read(&conn, inserted_post.id).unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap(); let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
let like_removed = PostLike::remove(&conn, &post_like_form).unwrap(); let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap(); let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap(); let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap(); Community::delete(&conn, inserted_community.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap(); User_::delete(&conn, inserted_user.id).unwrap();
@ -410,8 +457,10 @@ mod tests {
assert_eq!(expected_post, updated_post); assert_eq!(expected_post, updated_post);
assert_eq!(expected_post_like, inserted_post_like); assert_eq!(expected_post_like, inserted_post_like);
assert_eq!(expected_post_saved, inserted_post_saved); assert_eq!(expected_post_saved, inserted_post_saved);
assert_eq!(expected_post_read, inserted_post_read);
assert_eq!(1, like_removed); assert_eq!(1, like_removed);
assert_eq!(1, saved_removed); assert_eq!(1, saved_removed);
assert_eq!(1, read_removed);
assert_eq!(1, num_deleted); assert_eq!(1, num_deleted);
} }
} }

View file

@ -37,10 +37,6 @@ impl Crud<PrivateMessageForm> for PrivateMessage {
private_message.find(private_message_id).first::<Self>(conn) private_message.find(private_message_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _private_message_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> { fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*; use crate::schema::private_message::dsl::*;
insert_into(private_message) insert_into(private_message)

View file

@ -359,6 +359,15 @@ table! {
} }
} }
table! {
post_read (id) {
id -> Int4,
post_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
table! { table! {
post_saved (id) { post_saved (id) {
id -> Int4, id -> Int4,
@ -504,6 +513,8 @@ joinable!(post -> community (community_id));
joinable!(post -> user_ (creator_id)); joinable!(post -> user_ (creator_id));
joinable!(post_like -> post (post_id)); joinable!(post_like -> post (post_id));
joinable!(post_like -> user_ (user_id)); joinable!(post_like -> user_ (user_id));
joinable!(post_read -> post (post_id));
joinable!(post_read -> user_ (user_id));
joinable!(post_saved -> post (post_id)); joinable!(post_saved -> post (post_id));
joinable!(post_saved -> user_ (user_id)); joinable!(post_saved -> user_ (user_id));
joinable!(site -> user_ (creator_id)); joinable!(site -> user_ (creator_id));
@ -536,6 +547,7 @@ allow_tables_to_appear_in_same_query!(
post, post,
post_aggregates_fast, post_aggregates_fast,
post_like, post_like,
post_read,
post_saved, post_saved,
private_message, private_message,
site, site,

View file

@ -39,10 +39,6 @@ impl Crud<SiteForm> for Site {
site.first::<Self>(conn) site.first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _site_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*; use crate::schema::site::dsl::*;
insert_into(site).values(new_site).get_result::<Self>(conn) insert_into(site).values(new_site).get_result::<Self>(conn)

View file

@ -28,10 +28,6 @@ impl Crud<UserMentionForm> for UserMention {
user_mention.find(user_mention_id).first::<Self>(conn) user_mention.find(user_mention_id).first::<Self>(conn)
} }
fn delete(_conn: &PgConnection, _user_mention_id: i32) -> Result<usize, Error> {
unimplemented!()
}
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> { fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*; use crate::schema::user_mention::dsl::*;
insert_into(user_mention) insert_into(user_mention)

View file

@ -159,10 +159,6 @@ impl<'a> UserQueryBuilder<'a> {
} }
impl UserView { impl UserView {
pub fn read(_conn: &PgConnection, _from_user_id: i32) -> Result<Self, Error> {
unimplemented!()
}
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> { pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use super::user_view::user_fast::dsl::*; use super::user_view::user_fast::dsl::*;
use diesel::sql_types::{Nullable, Text}; use diesel::sql_types::{Nullable, Text};