mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-08 02:52:39 -05:00
Added initial counts api for comments
refs https://github.com/TryGhost/Team/issues/1664
This commit is contained in:
parent
484e5102e2
commit
9ee003f8b2
4 changed files with 35 additions and 0 deletions
|
@ -2,6 +2,7 @@ const Promise = require('bluebird');
|
|||
const tpl = require('@tryghost/tpl');
|
||||
const errors = require('@tryghost/errors');
|
||||
const models = require('../../models');
|
||||
const db = require('../../data/db');
|
||||
const ALLOWED_INCLUDES = ['post', 'member', 'likes', 'replies'];
|
||||
const UNSAFE_ATTRS = ['status'];
|
||||
|
||||
|
@ -156,6 +157,29 @@ module.exports = {
|
|||
}
|
||||
},
|
||||
|
||||
counts: {
|
||||
permissions: false,
|
||||
async query(frame) {
|
||||
const query = db.knex('comments')
|
||||
.select(db.knex.raw(`COUNT(*) AS count, post_id`))
|
||||
.groupBy('post_id');
|
||||
|
||||
if (Array.isArray(frame?.data?.ids)) {
|
||||
query.whereIn('post_id', frame.data.ids);
|
||||
}
|
||||
|
||||
const results = await query;
|
||||
|
||||
const counts = {};
|
||||
|
||||
for (const row of results) {
|
||||
counts[row.post_id] = row.count;
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
},
|
||||
|
||||
like: {
|
||||
statusCode: 204,
|
||||
options: [
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
module.exports = {
|
||||
counts(response, apiConfig, frame) {
|
||||
frame.response = response;
|
||||
}
|
||||
};
|
|
@ -13,6 +13,10 @@ module.exports = {
|
|||
return require('./default');
|
||||
},
|
||||
|
||||
get comments() {
|
||||
return require('./comments');
|
||||
},
|
||||
|
||||
get authentication() {
|
||||
return require('./authentication');
|
||||
},
|
||||
|
|
|
@ -13,6 +13,8 @@ module.exports = function apiRoutes() {
|
|||
// Global handling for member session, ensures a member is logged in to the frontend
|
||||
router.use(membersService.middleware.loadMemberSession);
|
||||
|
||||
router.post('/counts', http(api.commentsComments.counts));
|
||||
|
||||
router.get('/', http(api.commentsComments.browse));
|
||||
router.get('/:id', http(api.commentsComments.read));
|
||||
router.post('/', http(api.commentsComments.add));
|
||||
|
|
Loading…
Add table
Reference in a new issue