Skip to content

Commit

Permalink
CHE-120 refactored aggregate query, moved into queryHelper with forum…
Browse files Browse the repository at this point in the history
…Id as conditional
  • Loading branch information
Howard Sun committed Jun 18, 2024
1 parent a09baac commit 96f66f8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 51 deletions.
31 changes: 2 additions & 29 deletions server/controllers/forumController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Request, Response, NextFunction } from 'express';
import Forum from '../models/forumModel';
import Thread from '../models/threadModel';
import { aggregateSort } from './helpers/queryHelpers';
import mongoose from 'mongoose';
import { aggregateThreadsWithPostCount } from './helpers/queryHelpers';

// ENDPOINT POST api/forums
// PURPOSE Create a new forum
Expand Down Expand Up @@ -57,32 +55,7 @@ const getForumById = async (req: Request, res: Response, next: NextFunction) =>
return res.status(404).json({ message: 'Forum not found' });
}

const threadsAggregate = Thread.aggregate([
{
$match: {
forum: new mongoose.Types.ObjectId(forumId),
},
},
{
$lookup: {
from: 'posts',
localField: '_id',
foreignField: 'thread',
as: 'posts',
},
},
{
$addFields: {
postCount: { $size: '$posts' },
},
},
{
$project: {
posts: 0,
},
},
]);
const threads = await aggregateSort(threadsAggregate);
const threads = await aggregateThreadsWithPostCount(forumId);

res.status(200).json({ forum, threads });
} catch (error) {
Expand Down
39 changes: 39 additions & 0 deletions server/controllers/helpers/queryHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Thread from '../../models/threadModel';
import mongoose from 'mongoose';

type SortOrder = 1 | -1;

interface SortAndPopulateQuery<T> {
Expand Down Expand Up @@ -36,3 +39,39 @@ export const aggregateSort = <T>(
const sortObj: Record<string, SortOrder> = { [sortField]: sortOrder };
return pipeline.sort(sortObj).exec();
};

export const aggregateThreadsWithPostCount = (forumId?: string) => {
const baseStages = [
{
$lookup: {
from: 'posts',
localField: '_id',
foreignField: 'thread',
as: 'posts',
},
},
{
$addFields: {
postCount: { $size: '$posts' },
},
},
{
$project: {
posts: 0,
},
},
];

const threadsAggregate = forumId
? [
{
$match: {
forum: new mongoose.Types.ObjectId(forumId),
},
},
...baseStages,
]
: baseStages;

return aggregateSort(Thread.aggregate(threadsAggregate));
};
24 changes: 2 additions & 22 deletions server/controllers/threadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from 'express';
import Post from '../models/postModel';
import Thread from '../models/threadModel';
import { CustomRequest } from '../types/customRequest';
import { sortAndPopulate, aggregateSort } from './helpers/queryHelpers';
import { sortAndPopulate, aggregateThreadsWithPostCount } from './helpers/queryHelpers';

// ENDPOINT POST api/:forumId/threads
// PURPOSE Create a new thread
Expand Down Expand Up @@ -39,27 +39,7 @@ const createThread = async (req: CustomRequest, res: Response, next: NextFunctio
// ACCESS Private
const getAllThreads = async (req: CustomRequest, res: Response, next: NextFunction) => {
try {
const threadsAggregate = Thread.aggregate([
{
$lookup: {
from: 'posts',
localField: '_id',
foreignField: 'thread',
as: 'posts',
},
},
{
$addFields: {
postCount: { $size: '$posts' },
},
},
{
$project: {
posts: 0,
},
},
]);
const threads = await aggregateSort(threadsAggregate);
const threads = await aggregateThreadsWithPostCount();

res.status(200).json(threads);
} catch (error) {
Expand Down

0 comments on commit 96f66f8

Please sign in to comment.