categories backend
This commit is contained in:
parent
8651cca8f3
commit
20579e2c17
@ -18,7 +18,11 @@ const ArticleSchema = new Schema({
|
|||||||
votes: { type: Number, default: 0 }
|
votes: { type: Number, default: 0 }
|
||||||
}],
|
}],
|
||||||
views: { 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);
|
103
src/server.js
103
src/server.js
@ -9,6 +9,7 @@ import passport from 'passport';
|
|||||||
import { Strategy } from 'passport-local';
|
import { Strategy } from 'passport-local';
|
||||||
import sessionFileStore from 'session-file-store';
|
import sessionFileStore from 'session-file-store';
|
||||||
import Article from './models/article.js';
|
import Article from './models/article.js';
|
||||||
|
import Category from './models/category.js';
|
||||||
import User from './models/user.js';
|
import User from './models/user.js';
|
||||||
|
|
||||||
require('dotenv').config();
|
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()
|
express()
|
||||||
.use(bodyParser.json())
|
.use(bodyParser.json())
|
||||||
.use(bodyParser.urlencoded({ extended: true }))
|
.use(bodyParser.urlencoded({ extended: true }))
|
||||||
@ -178,27 +202,7 @@ express()
|
|||||||
})
|
})
|
||||||
|
|
||||||
.post('/cms/article',
|
.post('/cms/article',
|
||||||
function(req, res, next) {
|
isAuthor,
|
||||||
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`
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async function(req, res, next) {
|
async function(req, res, next) {
|
||||||
try {
|
try {
|
||||||
const { html, title, image } = req.body;
|
const { html, title, image } = req.body;
|
||||||
@ -229,19 +233,62 @@ express()
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
.get('/a/all', async function (req, res, next) {
|
.post('/cms/category',
|
||||||
let articles = await Article.find().sort({ created_at: 'desc' }).populate({
|
isAuthor,
|
||||||
path: 'author',
|
async function(req, res, next) {
|
||||||
select: 'realname'
|
try {
|
||||||
|
const { name } = req.body;
|
||||||
|
if (!name) {
|
||||||
|
res.writeHead(422, {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
articles.forEach(article => {
|
res.end(JSON.stringify({
|
||||||
article.slug = article.title.toLowerCase().replace(/\W+/g, '-');
|
message: `You must supply a category name.`
|
||||||
article.html = article.html.replace(/^\t{3}/gm, '');
|
}));
|
||||||
|
}
|
||||||
|
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, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
res.end(JSON.stringify(articles));
|
res.end(JSON.stringify(articles));
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.get('/me', function(req, res, next) {
|
.get('/me', function(req, res, next) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user