Skip to content

Commit

Permalink
feat: category CURD
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxing committed Jun 5, 2018
1 parent b9e7945 commit 221a1e7
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 3 deletions.
24 changes: 24 additions & 0 deletions models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const CategorySchema = new Schema({
name: {
type: String,
required: true
},
title: {
type: String,
required: true
},
desc: {
type: String
},
meta: {
createAt: {
type: Date,
default: Date.now()
}
}
})

module.exports = mongoose.model('Category', CategorySchema)
4 changes: 4 additions & 0 deletions models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const PostSchema = new Schema({
type: String,
required: true
},
category: {
type: Schema.Types.ObjectId,
ref: 'Category'
},
pv: {
type: Number,
default: 0
Expand Down
3 changes: 2 additions & 1 deletion public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
.editor-header .right-box {
margin-left: 200px;
justify-content: flex-end;
padding: 0 20px
padding: 0 20px;
display: flex;
}

.margin-top {
Expand Down
35 changes: 35 additions & 0 deletions routes/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const CategoryModel = require('../models/category')

module.exports = {
async create (ctx, next) {
if (ctx.method === 'GET') {
await ctx.render('create_category', {
title: '新建分类'
})
return
}
await CategoryModel.create(ctx.request.body)
ctx.redirect('/category')
},
async list (ctx, next) {
const categories = await CategoryModel.find({})
await ctx.render('category', {
title: '新建分类',
categories
})
},
async edit (ctx, next) {
if (ctx.method === 'GET') {
const category = await CategoryModel.findById(ctx.params.id)
await ctx.render('create_category', {
title: '编辑分类',
category
})
}
},
async destroy (ctx, next) {
await CategoryModel.findByIdAndRemove(ctx.params.id)
ctx.flash = { success: '删除分类成功' }
ctx.redirect('/category')
}
}
5 changes: 4 additions & 1 deletion routes/home.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const PostModel = require('../models/post')
const CategoryModel = require('../models/category')

module.exports = {
async index (ctx, next) {
const posts = await PostModel.find({})
const categories = await CategoryModel.find({}).limit(5)
await ctx.render('index', {
title: 'JS之禅',
desc: '欢迎关注公众号 JavaScript之禅',
posts
posts,
categories
})
}
}
4 changes: 4 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = (app) => {
router.get('/posts/:id/delete', isLoginUser, require('./posts').destroy)
router.post('/comments/new', isLoginUser, require('./comments').create)
router.get('/comments/:id/delete', isLoginUser, require('./comments').destroy)
router.get('/category', require('./category').list)
router.get('/category/new', require('./category').create)
router.post('/category/new', require('./category').create)
router.get('/category/:id/delete', require('./category').destroy)

app
.use(router.routes())
Expand Down
5 changes: 4 additions & 1 deletion routes/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const PostModel = require('../models/post')
const CommentModel = require('../models/comment')
const CategoryModel = require('../models/category')

module.exports = {
async index (ctx, next) {
Expand All @@ -24,8 +25,10 @@ module.exports = {

async create (ctx, next) {
if (ctx.method === 'GET') {
const categories = await CategoryModel.find({})
await ctx.render('create', {
title: '新建文章'
title: '新建文章',
categories
})
return
}
Expand Down
31 changes: 31 additions & 0 deletions views/category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'views/base.html' %}
{% block body %}
{% include "views/components/header.html" %}
<div class="container margin-top">
<div class="columns">
<div class="column is-8 is-offset-2">
<a href="/category/new" class="button is-small is-primary">新建分类</a>
<table class="table margin-top is-bordered is-striped is-narrow is-hoverable is-fullwidth">
<thead>
<tr>
<th>name</th>
<th>分类名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{category.name}}</td>
<td>{{category.title}}</td>
<td>
<a href="/category/{{category._id}}/delete" class="button is-small is-danger">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions views/components/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<div class="navbar-item">
<a class="button is-small is-primary" href="/posts/new">写文章</a>
</div>
<div class="navbar-item">
<a class="button is-small is-primary" href="/category">分类</a>
</div>
<div class="navbar-item">
<a href="/user/{{ctx.session.user.name}}">{{ctx.session.user.name}}</a>
</div>
Expand Down
8 changes: 8 additions & 0 deletions views/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<header class="editor-header">
<input name="title" class="input is-shadowless is-radiusless" autofocus="autofocus" type="text" placeholder="输入文章标题...">
<div class="right-box">
<div class="select is-small">
<select name="category">
<option disabled="disabled">分类</option>
{% for category in categories %}
<option value={{category._id}}>{{category.title}}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="button is-small is-primary">发布</button>
</div>
</header>
Expand Down
31 changes: 31 additions & 0 deletions views/create_category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'views/base.html' %}
{% block body %}
{% include "views/components/header.html" %}
<div class="container margin-top">
<div class="columns">
<div class="column is-8 is-offset-2">
<form action="/category/new" method="POST">
<div class="field">
<label class="label">分类名</label>
<div class="control">
<input name="name" class="input" type="text" placeholder="frontend">
</div>
</div>
<div class="field">
<label class="label">分类标题</label>
<div class="control">
<input name="title" class="input" type="text" placeholder="前端">
</div>
</div>
<div class="field">
<label class="label">描述</label>
<div class="control">
<textarea name="desc" class="textarea" placeholder="Textarea"></textarea>
</div>
</div>
<button class="button is-primary">新建分类</button>
</form>
</div>
</div>
</div>
{% endblock %}

0 comments on commit 221a1e7

Please sign in to comment.