70 lines
2.8 KiB
JavaScript
70 lines
2.8 KiB
JavaScript
const app = require('express').Router();
|
|
const checkPermission = require("../middlewares/checkPermission");
|
|
const articleValidation = require("../validation/articleValidation");
|
|
const Article = require("../models/Article");
|
|
|
|
app.put("/", checkPermission("admin.article.create"), async (req, res) => {
|
|
// Validate request
|
|
const {error} = articleValidation.create.validate(req.body);
|
|
if (error) return res.status(400).json({message: error.details[0].message});
|
|
|
|
// Check if article already exists
|
|
const {count: articleCount} = await Article.findAndCountAll({where: req.body});
|
|
if (articleCount === 1) return res.status(400).json({message: "Article already exists"});
|
|
|
|
// Create article
|
|
res.status(200).json(await Article.create(req.body));
|
|
});
|
|
|
|
app.delete("/", checkPermission("admin.article.delete"), async (req, res) => {
|
|
// Validate request
|
|
const {error} = articleValidation.info.validate(req.body);
|
|
if (error) return res.status(400).json({message: error.details[0].message});
|
|
|
|
// Check if already exists
|
|
const {count: articleCount} = await Article.findAndCountAll({where: req.body});
|
|
if (articleCount === 0) return res.status(400).json({message: "Article does not exist"});
|
|
|
|
// Delete the article
|
|
await Article.destroy({where: req.body});
|
|
res.status(200).json({message: "Article deleted successfully"});
|
|
});
|
|
|
|
app.get("/:articleId", checkPermission("admin.article.info"), async (req, res) => {
|
|
// Validate request
|
|
if (!req.params.articleId) return res.status(400).json({message: "Missing article id"});
|
|
|
|
// Get the article information
|
|
const info = await Article.findByPk(req.params.articleId);
|
|
|
|
if (info) {
|
|
res.status(200).json(info);
|
|
} else res.status(404).json({message: "Article not found"});
|
|
});
|
|
|
|
app.get("/:moduleName/list", checkPermission("articles.list"), async (req, res) => {
|
|
// Validate request
|
|
if (!req.params.moduleName) return res.status(400).json({message: "Module name is required"});
|
|
|
|
// Get the article
|
|
const list = await Article.findAndCountAll({where: {moduleName: req.params.moduleName}});
|
|
|
|
if (list) {
|
|
res.status(200).json(list.rows);
|
|
} else res.status(404).json({message: "No articles found"});
|
|
});
|
|
|
|
app.get("/:moduleName/:articleId", checkPermission("admin.article.info"), async (req, res) => {
|
|
// Validate request
|
|
if (!req.params.moduleName) return res.status(400).json({message: "Missing module name"});
|
|
if (!req.params.articleId) return res.status(400).json({message: "Missing article id"});
|
|
|
|
// Get the article information
|
|
const info = await Article.findOne({where: {moduleName: req.params.moduleName, articleId: req.params.articleId}});
|
|
|
|
if (info) {
|
|
res.status(200).json(info);
|
|
} else res.status(404).json({message: "Article not found"});
|
|
});
|
|
|
|
module.exports = app; |