Skip to content

Commit

Permalink
Add adminController and adminHelper functions for listing projects
Browse files Browse the repository at this point in the history
  • Loading branch information
guillecro committed Jan 23, 2024
1 parent 683d193 commit f65a3af
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
16 changes: 16 additions & 0 deletions controllers/adminController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Project = require('../models/project');
const AdminHelper = require('../helpers/adminHelper');

exports.listProjects = async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
// get the projects
const resData = await AdminHelper.listProjects(page, limit);
// return the projects
return res.status(200).json(resData);
} catch (error){
console.error(error)
return res.status(500).json({message: error.message})
}
}
1 change: 0 additions & 1 deletion controllers/projectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const Like = require('../models/like');
const ProjectHelper = require('../helpers/projectsHelper');
const ArticleHelper = require('../helpers/articleHelper');

// DONE
exports.listProjects = async (req, res) => {
try {
const currentUserId = req.user ? req.user._id : null;
Expand Down
71 changes: 71 additions & 0 deletions helpers/adminHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const Project = require('../models/project');


exports.listProjects = async (page = 1, limit = 10) => {
try {
const projects = []
// get the projects by page
const query = {}
const projectList = await Project.find(query).populate({
path: 'author',
select: '_id name country role',
populate: {
path: 'country',
select: '_id name code emoji unicode image'
}
}).sort({createdAt: -1}).skip((page - 1) * limit).limit(limit);

for(let i = 0; i < projectList.length; i++) {
const project = projectList[i];
const projectOutput = {}
projectOutput._id = project._id;
projectOutput.slug = project.slug;
projectOutput.author = project.author;
projectOutput.title_es = project.title_es;
projectOutput.title_pt = project.title_pt;
projectOutput.coverUrl = project.coverUrl;
projectOutput.youtubeUrl = project.youtubeUrl;
projectOutput.shortAbout_es = project.shortAbout_es;
projectOutput.shortAbout_pt = project.shortAbout_pt;
projectOutput.about_es = project.about_es;
projectOutput.about_pt = project.about_pt;
projectOutput.version = project.version;
projectOutput.eventCount = project.eventsCount;
projectOutput.articleCount = project.articlesCount;
projectOutput.versionsCount = project.versionsCount;
projectOutput.commentsCount = await project.getCommentsCount();
projectOutput.articleCommentsCount = await project.getArticleCommentsCount();
projectOutput.likes = await project.getLikesCount();
projectOutput.dislikes = await project.getDislikesCount();
projectOutput.stage = project.stage;
projectOutput.closed = project.closed;
projectOutput.closedAt = project.closedAt;
projectOutput.publishedAt = project.publishedAt;
projectOutput.hidden = project.hidden;
projectOutput.published = project.published;
projectOutput.createdAt = project.createdAt;
projectOutput.updatedAt = project.updatedAt;
projects.push(projectOutput);
}

// get pagination metadata
const total = await Project.countDocuments(query); // get total of projects
const pages = Math.ceil(total / limit); // round up to the next integer
const nextPage = page < pages ? page + 1 : null; // if there is no next page, return null
const prevPage = page > 1 ? page - 1 : null; // if there is no previous page, return null

// return the projects with pagination metadata
return {
projects,
page,
pages,
total,
limit,
nextPage,
prevPage
}
} catch (error) {
console.error(error);
throw error;
}
}
1 change: 0 additions & 1 deletion helpers/projectsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ exports.canModerate = async (user, project) => {
}



exports.listProjects = async (page = 1, limit = 10, currentUserId = null) => {
try {
const projects = []
Expand Down
13 changes: 13 additions & 0 deletions routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const exists = require('../../middlewares/exists');
const validate = require('../../middlewares/validate');

const UserController = require('../../controllers/userController');
const AdminController = require('../../controllers/adminController');

// initialize router
const router = express.Router({mergeParams: true});
Expand All @@ -14,7 +15,9 @@ const router = express.Router({mergeParams: true});
// BASE /admin
// -----------------------------------------------
// GET /admin/users
// GET /admin/users/authors
// GET /admin/users/:userId
// GET /admin/projects
// -----------------------------------------------

// GET /admin/users
Expand Down Expand Up @@ -51,4 +54,14 @@ router.get('/users/:userId',
UserController.get
)

router.get('/projects',
[
query('page').optional().isInt({ min: 1 }).withMessage('Page must be an integer greater than 0'),
query('limit').optional().isInt({ min: 1 }).withMessage('Limit must be an integer greater than 1'),
],
validate,
authenticate(constants.ROLES.ADMINISTRATOR),
AdminController.listProjects
)

module.exports = router;

0 comments on commit f65a3af

Please sign in to comment.