2019-03-04 16:39:07 +00:00
|
|
|
extern crate diesel;
|
2019-04-03 06:49:32 +00:00
|
|
|
use schema::{community, community_moderator, community_follower};
|
2019-03-04 16:39:07 +00:00
|
|
|
use diesel::*;
|
|
|
|
use diesel::result::Error;
|
2019-03-23 01:42:57 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-03-04 16:39:07 +00:00
|
|
|
use {Crud, Followable, Joinable};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
|
2019-03-04 16:39:07 +00:00
|
|
|
#[table_name="community"]
|
|
|
|
pub struct Community {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
pub title: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub category_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub creator_id: i32,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
|
2019-03-04 16:39:07 +00:00
|
|
|
#[table_name="community"]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct CommunityForm {
|
|
|
|
pub name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
pub title: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub category_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub creator_id: i32,
|
2019-03-23 01:42:57 +00:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
|
|
|
#[belongs_to(Community)]
|
2019-04-03 06:49:32 +00:00
|
|
|
#[table_name = "community_moderator"]
|
|
|
|
pub struct CommunityModerator {
|
2019-03-04 16:39:07 +00:00
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-04-03 06:49:32 +00:00
|
|
|
#[table_name="community_moderator"]
|
|
|
|
pub struct CommunityModeratorForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
|
|
|
|
#[belongs_to(Community)]
|
|
|
|
#[table_name = "community_follower"]
|
|
|
|
pub struct CommunityFollower {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-03-04 16:39:07 +00:00
|
|
|
#[table_name="community_follower"]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct CommunityFollowerForm {
|
|
|
|
pub community_id: i32,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub user_id: i32,
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
impl Crud<CommunityForm> for Community {
|
|
|
|
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community::dsl::*;
|
|
|
|
community.find(community_id)
|
2019-03-23 01:42:57 +00:00
|
|
|
.first::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community::dsl::*;
|
|
|
|
diesel::delete(community.find(community_id))
|
|
|
|
.execute(conn)
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community::dsl::*;
|
|
|
|
insert_into(community)
|
|
|
|
.values(new_community)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community::dsl::*;
|
|
|
|
diesel::update(community.find(community_id))
|
|
|
|
.set(new_community)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
impl Followable<CommunityFollowerForm> for CommunityFollower {
|
|
|
|
fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community_follower::dsl::*;
|
|
|
|
insert_into(community_follower)
|
|
|
|
.values(community_follower_form)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
use schema::community_follower::dsl::*;
|
|
|
|
diesel::delete(community_follower
|
2019-03-23 01:42:57 +00:00
|
|
|
.filter(community_id.eq(&community_follower_form.community_id))
|
2019-04-03 06:49:32 +00:00
|
|
|
.filter(user_id.eq(&community_follower_form.user_id)))
|
2019-03-04 16:39:07 +00:00
|
|
|
.execute(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
impl Joinable<CommunityModeratorForm> for CommunityModerator {
|
|
|
|
fn join(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<Self, Error> {
|
|
|
|
use schema::community_moderator::dsl::*;
|
|
|
|
insert_into(community_moderator)
|
2019-03-04 16:39:07 +00:00
|
|
|
.values(community_user_form)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
fn leave(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<usize, Error> {
|
|
|
|
use schema::community_moderator::dsl::*;
|
|
|
|
diesel::delete(community_moderator
|
2019-03-04 16:39:07 +00:00
|
|
|
.filter(community_id.eq(community_user_form.community_id))
|
2019-04-03 06:49:32 +00:00
|
|
|
.filter(user_id.eq(community_user_form.user_id)))
|
2019-03-04 16:39:07 +00:00
|
|
|
.execute(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use establish_connection;
|
|
|
|
use super::*;
|
|
|
|
use actions::user::*;
|
|
|
|
use Crud;
|
|
|
|
#[test]
|
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_connection();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let new_user = UserForm {
|
|
|
|
name: "bob".into(),
|
|
|
|
fedi_name: "rrf".into(),
|
|
|
|
preferred_username: None,
|
|
|
|
password_encrypted: "nope".into(),
|
|
|
|
email: None,
|
|
|
|
updated: None
|
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let new_community = CommunityForm {
|
|
|
|
name: "TIL".into(),
|
2019-04-03 20:59:37 +00:00
|
|
|
creator_id: inserted_user.id,
|
|
|
|
title: "nada".to_owned(),
|
|
|
|
description: None,
|
|
|
|
category_id: 1,
|
2019-04-03 06:49:32 +00:00
|
|
|
updated: None,
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
let expected_community = Community {
|
|
|
|
id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
creator_id: inserted_user.id,
|
2019-03-04 16:39:07 +00:00
|
|
|
name: "TIL".into(),
|
2019-04-03 20:59:37 +00:00
|
|
|
title: "nada".to_owned(),
|
|
|
|
description: None,
|
|
|
|
category_id: 1,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_community.published,
|
|
|
|
updated: None
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
let expected_community_follower = CommunityFollower {
|
|
|
|
id: inserted_community_follower.id,
|
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_community_follower.published
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let community_user_form = CommunityModeratorForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let expected_community_user = CommunityModerator {
|
2019-03-04 16:39:07 +00:00
|
|
|
id: inserted_community_user.id,
|
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_community_user.published
|
2019-03-04 16:39:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let read_community = Community::read(&conn, inserted_community.id).unwrap();
|
|
|
|
let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
|
|
|
|
let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
|
2019-04-03 06:49:32 +00:00
|
|
|
let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
|
2019-03-23 01:42:57 +00:00
|
|
|
let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
|
|
|
|
User_::delete(&conn, inserted_user.id).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
|
|
|
assert_eq!(expected_community, read_community);
|
|
|
|
assert_eq!(expected_community, inserted_community);
|
|
|
|
assert_eq!(expected_community, updated_community);
|
|
|
|
assert_eq!(expected_community_follower, inserted_community_follower);
|
|
|
|
assert_eq!(expected_community_user, inserted_community_user);
|
|
|
|
assert_eq!(1, ignored_community);
|
|
|
|
assert_eq!(1, left_community);
|
2019-04-03 06:49:32 +00:00
|
|
|
// assert_eq!(2, loaded_count);
|
2019-03-04 16:39:07 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|