categories backend
This commit is contained in:
parent
8651cca8f3
commit
20579e2c17
@ -18,7 +18,11 @@ const ArticleSchema = new Schema({
|
||||
votes: { type: Number, default: 0 }
|
||||
}],
|
||||
views: { type: Number, default: 0 },
|
||||
votes: { type: Number, default: 0 }
|
||||
votes: { type: Number, default: 0 },
|
||||
category: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Category'
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
8
src/models/category.js
Normal file
8
src/models/category.js
Normal file
@ -0,0 +1,8 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const { Schema } = mongoose;
|
||||
const CategorySchema = new Schema({
|
||||
name: { type: String, index: { unique: true } }
|
||||
});
|
||||
|
||||
export default mongoose.model('Category', CategorySchema);
|
107
src/server.js
107
src/server.js
@ -9,6 +9,7 @@ import passport from 'passport';
|
||||
import { Strategy } from 'passport-local';
|
||||
import sessionFileStore from 'session-file-store';
|
||||
import Article from './models/article.js';
|
||||
import Category from './models/category.js';
|
||||
import User from './models/user.js';
|
||||
|
||||
require('dotenv').config();
|
||||
@ -52,6 +53,29 @@ passport.use(new Strategy((username, password, done) => {
|
||||
});
|
||||
}));
|
||||
|
||||
const isAuthor = function(req, res, next) {
|
||||
if (req.user) {
|
||||
if (req.user.author) {
|
||||
next();
|
||||
} else {
|
||||
res.writeHead(401, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `You are not designated as an author.`
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
res.writeHead(401, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `You are not logged in`
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
express()
|
||||
.use(bodyParser.json())
|
||||
.use(bodyParser.urlencoded({ extended: true }))
|
||||
@ -178,27 +202,7 @@ express()
|
||||
})
|
||||
|
||||
.post('/cms/article',
|
||||
function(req, res, next) {
|
||||
if (req.user) {
|
||||
if (req.user.author) {
|
||||
next();
|
||||
} else {
|
||||
res.writeHead(401, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `You are not designated as an author.`
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
res.writeHead(401, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `You are not logged in`
|
||||
}));
|
||||
}
|
||||
},
|
||||
isAuthor,
|
||||
async function(req, res, next) {
|
||||
try {
|
||||
const { html, title, image } = req.body;
|
||||
@ -229,19 +233,62 @@ express()
|
||||
}
|
||||
)
|
||||
|
||||
.get('/a/all', async function (req, res, next) {
|
||||
let articles = await Article.find().sort({ created_at: 'desc' }).populate({
|
||||
path: 'author',
|
||||
select: 'realname'
|
||||
});
|
||||
articles.forEach(article => {
|
||||
article.slug = article.title.toLowerCase().replace(/\W+/g, '-');
|
||||
article.html = article.html.replace(/^\t{3}/gm, '');
|
||||
});
|
||||
.post('/cms/category',
|
||||
isAuthor,
|
||||
async function(req, res, next) {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
if (!name) {
|
||||
res.writeHead(422, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `You must supply a category name.`
|
||||
}));
|
||||
}
|
||||
const cat = await new Category({ name });
|
||||
await cat.save();
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
category: cat.name
|
||||
}));
|
||||
} catch (err) {
|
||||
res.writeHead(500, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `Failed to add category: ${err}`
|
||||
}));
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
.get('/a/:category', async function (req, res, next) {
|
||||
let { category } = req.params;
|
||||
let articles;
|
||||
if (category === 'all') {
|
||||
articles = await Article.find().sort({ created_at: 'desc' });
|
||||
} else {
|
||||
let cat = await Category.findOne({ name: category });
|
||||
if (!cat) {
|
||||
res.writeHead(404, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
message: `That category does not exist.`
|
||||
}));
|
||||
return;
|
||||
} else {
|
||||
articles = await Article.find({ category: cat.id }).sort({ created_at: 'desc' });
|
||||
}
|
||||
}
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify(articles));
|
||||
|
||||
})
|
||||
|
||||
.get('/me', function(req, res, next) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user