-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adminController and adminHelper functions for listing projects
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters