Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const createToken = ({ id, isAdmin }) =>
{ id, isAdmin },
process.env.JWT_SECRET,
{
expiresIn: 60,
expiresIn: 30 * 60,
},
(err, token) => {
if (!err) {
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/tag/tag.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { Tag } = require('../../models');
const { removeById } = require('../common');

exports.getAll = () => Tag.all();

exports.create = (name) => Tag.create({name});
exports.create = name => Tag.create({ name });

exports.remove = id => removeById(Tag, id);
4 changes: 2 additions & 2 deletions app/routes/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const Answer = require('../controllers/answer/answer.controller');

exports.create = async ctx => {
const { id } = ctx.params;
const { text, userId } = ctx.request.body;
const question = await Answer.create(text, id, userId);
const { text } = ctx.request.body;
const question = await Answer.create(text, id, ctx.state.user.id);
if (question) {
ctx.body = question;
} else {
Expand Down
12 changes: 12 additions & 0 deletions app/routes/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.checkAdminOrOwner = model => async (ctx, next) => {
if (ctx.state.user.isAdmin) {
next();
return;
}
const instance = await model.findById(ctx.params.id);
if (instance.userId !== ctx.state.user.id) {
ctx.status = 403;
return
}
next();
};
11 changes: 7 additions & 4 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const question = require('./question');
const answer = require('./answer');
const tag = require('./tag');

const { Question, Answer } = require('../models');
const { checkAdminOrOwner } = require('./helpers');

const router = new Router();

router.get('/', ctx => {
Expand All @@ -18,10 +21,10 @@ router.get('/users', user.getAll);
router.post('/questions', question.create);
router.get('/questions', question.getAll);
router.get('/questions/:id', question.getById);
router.patch('/questions/:id', question.update);
router.patch('/questions/:id', checkAdminOrOwner(Question), question.update);
router.post('/questions/:id/upvote', question.upvote);
router.post('/questions/:id/downvote', question.downvote);
router.delete('/questions/:id', question.remove);
router.delete('/questions/:id', checkAdminOrOwner(Question), question.remove);

router.post('/questions/:id/answers', answer.create);
router.get('/questions/:id/answers', answer.getAllByQuestionId);
Expand All @@ -30,8 +33,8 @@ router.post('/questions/:id/add-tag', question.addTag);
router.post('/questions/:id/remove-tag', question.removeTag);

router.get('/answers/:id', answer.getById);
router.patch('/answers/:id', answer.update);
router.delete('/answers/:id', answer.remove);
router.patch('/answers/:id', checkAdminOrOwner(Answer), answer.update);
router.delete('/answers/:id', checkAdminOrOwner(Answer), answer.remove);
router.post('/answers/:id/upvote', answer.upvote);
router.post('/answers/:id/doenvote', answer.downvote);

Expand Down
4 changes: 2 additions & 2 deletions app/routes/question.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Question = require('../controllers/question/question.controller');

exports.create = async ctx => {
const { title, description, userId } = ctx.request.body;
const question = await Question.create(title, description, userId);
const { title, description } = ctx.request.body;
const question = await Question.create(title, description, ctx.state.user.id);
if (question) {
ctx.body = question;
} else {
Expand Down
14 changes: 9 additions & 5 deletions app/routes/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ exports.getAll = async ctx => {
}

exports.create = async ctx => {
const { name } = ctx.request.body;
const tag = await Tag.create(name);
if (tag) {
ctx.body = tag;
if (ctx.state.user.isAdmin) {
const { name } = ctx.request.body;
const tag = await Tag.create(name);
if (tag) {
ctx.body = tag;
} else {
ctx.status = 400;
}
} else {
ctx.status = 400;
ctx.status = 403;
}
};
2 changes: 2 additions & 0 deletions app/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ exports.register = async ctx => {
const { login, password } = ctx.request.body;
const user = await User.create(login, password);
if (user) {
const token = await createToken(user);
ctx.body = {
token,
message: `User ${user.login} has been created.`,
};
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"dependencies": {
"bcrypt": "^1.0.3",
"dotenv": "^5.0.1",
"eslint-config-prettier": "^2.9.0",
"jsonwebtoken": "^8.2.1",
"koa": "^2.5.0",
"koa-bodyparser": "^4.2.0",
Expand All @@ -37,6 +36,7 @@
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.10.0"
}
}