Skip to content

Commit

Permalink
Merge pull request #217 from learnsoftwaredevelopment/add-new-recent-…
Browse files Browse the repository at this point in the history
…software-api-endpoints

Added `api/software/added/recent` and `api/software/updates/recent` API endpoints
  • Loading branch information
JonathanLeeWH authored Aug 3, 2021
2 parents 1e47f48 + 8be5d1b commit e1aa8a0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
4 changes: 3 additions & 1 deletion controllers/api/searchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const searchSoftware = async (req, res) => {
const queryResponse = await Software.find(query)
.sort({ name: 'asc' })
.skip(page * perPage)
.limit(perPage);
.limit(perPage)
.populate('meta.addedByUser', { username: 1, name: 1 })
.populate('meta.updatedByUser', { username: 1, name: 1 });

const totalQueryResultCount = await Software.find(query).countDocuments();

Expand Down
36 changes: 30 additions & 6 deletions controllers/api/softwareController.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ const postSoftware = async (req, res) => {
user.contributions.softwaresAdded = user.contributions.softwaresAdded.concat(
savedSoftware._id,
);
user.contributions.softwaresContributed = user.contributions.softwaresContributed.concat(
savedSoftware._id,
);
user.contributions.softwaresContributed =
user.contributions.softwaresContributed.concat(savedSoftware._id);

await user.save();

Expand Down Expand Up @@ -90,9 +89,8 @@ const patchSoftwareById = async (req, res) => {
const user = await User.findById(userId);

if (!user.contributions.softwaresContributed.includes(id)) {
user.contributions.softwaresContributed = user.contributions.softwaresContributed.concat(
updatedSoftware._id,
);
user.contributions.softwaresContributed =
user.contributions.softwaresContributed.concat(updatedSoftware._id);
await user.save();
}

Expand Down Expand Up @@ -136,10 +134,36 @@ const deleteSoftwareById = async (req, res) => {
return res.status(204).end();
};

const getRecentAddedSoftware = async (req, res) => {
const count = parseInt(req.query.count, 10) || 5;

const response = await Software.find({})
.sort({ createdAt: 'desc' })
.limit(count)
.populate('meta.addedByUser', { username: 1, name: 1 })
.populate('meta.updatedByUser', { username: 1, name: 1 });

return res.status(200).json(response);
};

const getRecentUpdatedSoftware = async (req, res) => {
const count = parseInt(req.query.count, 10) || 5;

const response = await Software.find({})
.sort({ updatedAt: 'desc' })
.limit(count)
.populate('meta.addedByUser', { username: 1, name: 1 })
.populate('meta.updatedByUser', { username: 1, name: 1 });

return res.status(200).json(response);
};

module.exports = {
getSoftware,
postSoftware,
patchSoftwareById,
getSoftwareById,
deleteSoftwareById,
getRecentAddedSoftware,
getRecentUpdatedSoftware,
};
File renamed without changes.
1 change: 1 addition & 0 deletions requests/api/softwares/get_recentAddedSoftware.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3001/api/software/added/recent?count=10
1 change: 1 addition & 0 deletions requests/api/softwares/get_recentUpdatedSoftware.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3001/api/software/updates/recent?count=10
7 changes: 7 additions & 0 deletions routes/api/software.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const softwareController = require('../../controllers/api/softwareController');

softwareRouter.get('/', softwareController.getSoftware);

softwareRouter.get('/added/recent', softwareController.getRecentAddedSoftware);

softwareRouter.get(
'/updates/recent',
softwareController.getRecentUpdatedSoftware,
);

softwareRouter.post(
'/',
middleware.tokenValidation,
Expand Down

0 comments on commit e1aa8a0

Please sign in to comment.