Skip to content

Commit

Permalink
Adding a sidebar, title, and forum categories
Browse files Browse the repository at this point in the history
- Adding a Sidebar component
- Starting on forum categories. LemmyNet#17
- Adding a Sidebar and title to community. Fixes LemmyNet#16
  • Loading branch information
dessalines committed Apr 3, 2019
1 parent e690d6c commit 927cb6d
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
create view community_view as
select *,
(select name from user_ u where c.creator_id = u.id) as creator_name,
(select name from category ct where c.category_id = ct.id) as category_name,
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
(select count(*) from post p where p.community_id = c.id) as number_of_posts
from community c;
Expand Down
73 changes: 73 additions & 0 deletions server/src/actions/category.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
extern crate diesel;
use schema::{category};
use diesel::*;
use diesel::result::Error;
use serde::{Deserialize, Serialize};
use {Crud};

#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name="category"]
pub struct Category {
pub id: i32,
pub name: String
}

#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
#[table_name="category"]
pub struct CategoryForm {
pub name: String,
}

impl Crud<CategoryForm> for Category {
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
use schema::category::dsl::*;
category.find(category_id)
.first::<Self>(conn)
}

fn delete(conn: &PgConnection, category_id: i32) -> Result<usize, Error> {
use schema::category::dsl::*;
diesel::delete(category.find(category_id))
.execute(conn)
}

fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
use schema::category::dsl::*;
insert_into(category)
.values(new_category)
.get_result::<Self>(conn)
}

fn update(conn: &PgConnection, category_id: i32, new_category: &CategoryForm) -> Result<Self, Error> {
use schema::category::dsl::*;
diesel::update(category.find(category_id))
.set(new_category)
.get_result::<Self>(conn)
}
}

impl Category {
pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use schema::category::dsl::*;
category.load::<Self>(conn)
}
}

#[cfg(test)]
mod tests {
use establish_connection;
use super::*;
// use Crud;
#[test]
fn test_crud() {
let conn = establish_connection();

let categories = Category::list_all(&conn).unwrap();
let expected_first_category = Category {
id: 1,
name: "Discussion".into()
};

assert_eq!(expected_first_category, categories[0]);
}
}
7 changes: 0 additions & 7 deletions server/src/actions/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
}
}

impl Community {
pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use schema::community::dsl::*;
community.load::<Self>(conn)
}
}

#[cfg(test)]
mod tests {
use establish_connection;
Expand Down
51 changes: 51 additions & 0 deletions server/src/actions/community_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
extern crate diesel;
use diesel::*;
use diesel::result::Error;
use serde::{Deserialize, Serialize};

table! {
community_view (id) {
id -> Int4,
name -> Varchar,
title -> Varchar,
description -> Nullable<Text>,
category_id -> Int4,
creator_id -> Int4,
published -> Timestamp,
updated -> Nullable<Timestamp>,
creator_name -> Varchar,
category_name -> Varchar,
number_of_subscribers -> BigInt,
number_of_posts -> BigInt,
}
}

#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
#[table_name="community_view"]
pub struct CommunityView {
pub id: i32,
pub name: String,
pub title: String,
pub description: Option<String>,
pub category_id: i32,
pub creator_id: i32,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub creator_name: String,
pub category_name: String,
pub number_of_subscribers: i64,
pub number_of_posts: i64
}

impl CommunityView {
pub fn read(conn: &PgConnection, from_community_id: i32) -> Result<Self, Error> {
use actions::community_view::community_view::dsl::*;
community_view.find(from_community_id).first::<Self>(conn)
}

pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
use actions::community_view::community_view::dsl::*;
community_view.load::<Self>(conn)
}
}

2 changes: 2 additions & 0 deletions server/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pub mod post;
pub mod comment;
pub mod post_view;
pub mod comment_view;
pub mod category;
pub mod community_view;
6 changes: 3 additions & 3 deletions server/src/actions/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl PostView {
}


pub fn get(conn: &PgConnection, from_post_id: i32, from_user_id: Option<i32>) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, from_post_id: i32, from_user_id: Option<i32>) -> Result<Self, Error> {

use actions::post_view::post_view::dsl::*;
use diesel::prelude::*;
Expand Down Expand Up @@ -235,8 +235,8 @@ mod tests {

let read_post_listings_with_user = PostView::list(&conn, ListingType::Community, ListingSortType::New, Some(inserted_community.id), Some(inserted_user.id), 10).unwrap();
let read_post_listings_no_user = PostView::list(&conn, ListingType::Community, ListingSortType::New, Some(inserted_community.id), None, 10).unwrap();
let read_post_listing_no_user = PostView::get(&conn, inserted_post.id, None).unwrap();
let read_post_listing_with_user = PostView::get(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();

let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions server/src/actions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
let conn = establish_connection();

let new_user = UserForm {
name: "thom".into(),
name: "thommy".into(),
fedi_name: "rrf".into(),
preferred_username: None,
password_encrypted: "nope".into(),
Expand All @@ -129,7 +129,7 @@ mod tests {

let expected_user = User_ {
id: inserted_user.id,
name: "thom".into(),
name: "thommy".into(),
fedi_name: "rrf".into(),
preferred_username: None,
password_encrypted: "$2y$12$YXpNpYsdfjmed.QlYLvw4OfTCgyKUnKHc/V8Dgcf9YcVKHPaYXYYy".into(),
Expand Down
2 changes: 1 addition & 1 deletion server/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate server;
use std::time::{Instant, Duration};
use server::actix::*;
use server::actix_web::server::HttpServer;
use server::actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse};
use server::actix_web::{ws, App, Error, HttpRequest, HttpResponse};

use server::websocket_server::server::*;

Expand Down
Loading

0 comments on commit 927cb6d

Please sign in to comment.