let express = require("express");
let router = express.Router();
let User = require("../models/user");
let Category = require("../models/category");
let Content = require("../models/content");
router.get("/",function(req,res){
res.render("admin/index",{userInfo:req.userInfo});
});
router.get("/user",function(req,res){
let page = Number(req.query.page||1);
// 默认每页显示8条数据
let limit = 8;
let skip = (page-1)*limit;
let total;
let counts;
User.count().then(function(count){
total = Math.ceil(count/limit);
page = Math.max(1,page);
page = Math.min(page,total);
counts = count;
});
User.find().limit(limit).skip(skip).then(function(users){
res.render("admin/userindex",{
userInfo:req.userInfo,
users:users,
page:page,
total:total,
counts:counts
})
});
});
router.get("/category",function(req,res){
Category.find().sort({_id:-1}).then(function(categories){
res.render("admin/category",{
userInfo:req.userInfo,
categories:categories
});
});
});
router.get("/category/add",function(req,res){
res.render("admin/addcategory",{userInfo:req.userInfo});
});
router.post("/category/add",function(req,res){
let name =req.body.name||"";
if(name==""){
res.render("admin/error",{userInfo:req.userInfo});
}
else{
Category.findOne({name:name},function(err,info){
if(err){
console.log(err);
}
if(info){
res.render("admin/error",{userInfo:req.userInfo});
return false;
}
let newcate = new Category({
name:name
});
newcate.save();
res.render("admin/success",{userInfo:req.userInfo});
});
}
});
router.get("/category/edit",function(req,res){
let cateid = req.query.id||"";
Category.find({id:cateid}).then(function(cateinfo){
res.render("admin/categoryedit",{
userInfo:req.userInfo ,
name:cateinfo.name
});
});
});
router.post("/category/edit",function(req,res){
let name =req.body.name||"";
let id = req.query.id||"";
if(name==""){
res.render("admin/error",{userInfo:req.userInfo});
return false;
}else{
Category.findOne({_id:id},function(err,info){
if(err){
console.log(err);
}
if(info){
console.log(info);
info.name = name;
info.save();
res.render("admin/success",{userInfo:req.userInfo});
}
});
}
});
router.get("/category/delete",function(req,res){
let id = req.query.id||"";
//console.log(id);
res.render("admin/confirm",{
userInfo:req.userInfo,
id:id
});
});
router.post("/category/delete",function(req,res){
let id = req.query.id||"";
//console.log(id);
Category.remove({_id:id}).then(function(){
res.render("admin/success",{
userInfo:req.userInfo
});
});
});
router.get("/content",function(req,res){ Content.find().populate(["category","user"]).sort({_id:-1}).then(function(contents){
res.render("admin/content",{
userInfo:req.userInfo,
contents:contents
});
});
});
router.get("/content/add",function(req,res){
let cate=null;
Category.find().then(function(categories){
cate = categories;
res.render("admin/addcontent",{
userInfo:req.userInfo,
categories :cate
});
});
});
router.post("/content/add",function(req,res){
let title = req.body.name||"";
let category = req.body.category||"";
let description = req.body.description||"";
let content = req.body.content||"";
if(title==""||category==""||description==""||content==""){
res.render("admin/addok",{
userInfo:req.userInfo,
message:"还有未填入的信息,请重新填入!"
});
return false;
}else {
let newcontent = new Content({
title:title,
category:category,
description:description,
composition:content,
addtime:new Date(),
num:0,
user :req.userInfo._id.toString()
});
newcontent.save();
res.render("admin/addok",{
userInfo:req.userInfo,
message : "更新博文成功"
});
}
});
router.get("/content/edit",function(req,res){
let cate=null;
let id = req.query.id||"";
Category.find().then(function(categories){
cate = categories;
});
Content.findOne({_id:id}).populate("category").then(function(info){
//console.log(info);
res.render("admin/editcontent",{
userInfo:req.userInfo,
info :info,
categories:cate
});
});
});
router.post("/content/edit",function(req,res){
let id = req.query.id||"";
let title = req.body.name||"";
let category = req.body.category||"";
let description = req.body.description||"";
let content = req.body.content||"";
if(title==""||category==""||description==""||content==""){
res.render("admin/addok",{
userInfo:req.userInfo,
message:"无效的修改,请重新修改!"
});
return false;
}else {
Content.update({_id:id},{
title : title,
category: category,
description : description,
content : content,
addtime:new Date(),
user :req.userInfo._id.toString()
}).then(function(){
res.render("admin/addok",{
userInfo:req.userInfo,
message : "修改成功!"
});
});
}
});
router.get("/content/delete",function(req,res){
let id = req.query.id||"";
res.render("admin/confirm2",{
userInfo:req.userInfo,
id:id
});
});
router.post("/content/delete",function(req,res){
let id = req.query.id||"";
//console.log(id);
Content.remove({_id:id}).then(function(){
res.render("admin/addok",{
userInfo:req.userInfo,
message : "删除成功!点击返回上一页!"
});
});
});
module.exports = router; |