Skip to content

Commit

Permalink
refactor: api categories
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 17, 2020
1 parent e78c498 commit 083c74e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion public/src/admin/manage/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ define('admin/manage/categories', [
};

Categories.create = function (payload) {
socket.emit('admin.categories.create', payload, function (err, data) {
api.post('/categories', payload, function (err, data) {
if (err) {
return app.alertError(err.message);
}
Expand Down
1 change: 0 additions & 1 deletion src/admin/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ async function buildNamespace(language, namespace) {
const cache = {};

async function getDictionary(language) {
console.log('get dictionary', language);
if (cache[language]) {
return cache[language];
}
Expand Down
28 changes: 28 additions & 0 deletions src/api/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const categories = require('../categories');
const events = require('../events');

const categoriesAPI = module.exports;

categoriesAPI.create = async function (caller, data) {
const response = await categories.create(data);
const categoryObjs = await categories.getCategories([response.cid], caller.uid);
return categoryObjs[0];
};

categoriesAPI.update = async function (caller, data) {
await categories.update(data);
};

categoriesAPI.delete = async function (caller, data) {
const name = await categories.getCategoryField(data.cid, 'name');
await categories.purge(data.cid, caller.uid);
await events.log({
type: 'category-purge',
uid: caller.uid,
ip: caller.ip,
cid: data.cid,
name: name,
});
};
1 change: 1 addition & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = {
users: require('./users'),
groups: require('./groups'),
topics: require('./topics'),
categories: require('./categories'),
};
21 changes: 5 additions & 16 deletions src/controllers/write/categories.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
'use strict';

const categories = require('../../categories');
const events = require('../../events');
const api = require('../../api');

const helpers = require('../helpers');

const Categories = module.exports;

Categories.create = async (req, res) => {
const response = await categories.create(req.body);
const categoryObjs = await categories.getCategories([response.cid]);
helpers.formatApiResponse(200, res, categoryObjs[0]);
const response = await api.categories.create(req, req.body);
helpers.formatApiResponse(200, res, response);
};

Categories.update = async (req, res) => {
const payload = {};
payload[req.params.cid] = req.body;

await categories.update(payload);
await api.categories.update(req, payload);
const categoryObjs = await categories.getCategories([req.params.cid]);
helpers.formatApiResponse(200, res, categoryObjs[0]);
};

Categories.delete = async (req, res) => {
const name = await categories.getCategoryField(req.params.cid, 'name');
await categories.purge(req.params.cid, req.user.uid);
await events.log({
type: 'category-purge',
uid: req.user.uid,
ip: req.ip,
cid: req.params.cid,
name: name,
});

await api.categories.delete(req, { cid: req.params.cid });
helpers.formatApiResponse(200, res);
};
17 changes: 4 additions & 13 deletions src/socket.io/admin/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const categories = require('../../categories');
const privileges = require('../../privileges');
const plugins = require('../../plugins');
const events = require('../../events');
const api = require('../../api');
const sockets = require('..');

const Categories = module.exports;
Expand All @@ -18,8 +19,7 @@ Categories.create = async function (socket, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}

return await categories.create(data);
return await api.categories.create(socket, data);
};

// DEPRECATED: @1.14.3, remove in version >=1.16
Expand All @@ -42,15 +42,7 @@ Categories.getNames = async function () {
Categories.purge = async function (socket, cid) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/categories/:cid');

const name = await categories.getCategoryField(cid, 'name');
await categories.purge(cid, socket.uid);
await events.log({
type: 'category-purge',
uid: socket.uid,
ip: socket.ip,
cid: cid,
name: name,
});
await api.categories.delete(socket, { cid: cid });
};

Categories.update = async function (socket, data) {
Expand All @@ -59,8 +51,7 @@ Categories.update = async function (socket, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}

return await categories.update(data);
return await api.categories.update(socket, data);
};

Categories.setPrivilege = async function (socket, data) {
Expand Down

0 comments on commit 083c74e

Please sign in to comment.