forked from LemmyNet/lemmy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a sidebar, title, and forum categories
- Adding a Sidebar component - Starting on forum categories. LemmyNet#17 - Adding a Sidebar and title to community. Fixes LemmyNet#16
- Loading branch information
1 parent
e690d6c
commit 927cb6d
Showing
15 changed files
with
336 additions
and
58 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
server/migrations/2019-04-03-155205_create_community_view/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.