forked from liuxing/node-blog
-
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.
- Loading branch information
Showing
11 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
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,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) |
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
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') | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
} |
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
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 %} |
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
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 %} |