mirror of https://github.com/LemmyNet/lemmy
Adding a sidebar, title, and forum categories
- Adding a Sidebar component - Starting on forum categories. #17 - Adding a Sidebar and title to community. Fixes #16pull/64/head
parent
81da0853aa
commit
7b1fb030b3
@ -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]);
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
import { Component, linkEvent } from 'inferno';
|
||||
import { Community } from '../interfaces';
|
||||
import { mdToHtml } from '../utils';
|
||||
|
||||
interface SidebarProps {
|
||||
community: Community;
|
||||
}
|
||||
|
||||
interface SidebarState {
|
||||
}
|
||||
|
||||
export class Sidebar extends Component<SidebarProps, SidebarState> {
|
||||
|
||||
constructor(props, context) {
|
||||
super(props, context);
|
||||
}
|
||||
|
||||
|
||||
render() {
|
||||
let community = this.props.community;
|
||||
return (
|
||||
<div>
|
||||
<h4>{community.title}</h4>
|
||||
<div><button type="button" class="btn btn-secondary mb-2">Subscribe</button></div>
|
||||
<div className="badge badge-light">{community.category_name}</div>
|
||||
<div>{community.number_of_subscribers} Subscribers</div>
|
||||
<div>{community.number_of_posts} Posts</div>
|
||||
<hr />
|
||||
{community.description && <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue