generated from Real-Dev-Squad/website-template
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Badge GET api #163
Merged
Merged
Badge GET api #163
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fad85c4
Badge GET api
whyDontI 33160c5
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
whyDontI 5c95ec8
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
whyDontI 5cdd165
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
whyDontI f1d16ad
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
whyDontI 86813f1
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
whyDontI 5145902
Write tests for badges GET route
whyDontI a66d0a6
API schema update
whyDontI File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,25 @@ | ||
const badgeQuery = require('../models/badges') | ||
|
||
/** | ||
* Get badges data | ||
* | ||
* @param req {Object} - Express request object | ||
* @param res {Object} - Express response object | ||
*/ | ||
|
||
const getBadges = async (req, res) => { | ||
try { | ||
const allBadges = await badgeQuery.fetchBadges(req.query) | ||
return res.json({ | ||
message: 'Badges returned successfully!', | ||
badges: allBadges | ||
}) | ||
} catch (error) { | ||
logger.error(`Error while fetching all badges: ${error}`) | ||
return res.boom.serverUnavailable('Something went wrong please contact admin') | ||
} | ||
} | ||
|
||
module.exports = { | ||
getBadges | ||
} |
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,32 @@ | ||
const firestore = require('../utils/firestore') | ||
const badgeModel = firestore.collection('badges') | ||
|
||
/** | ||
* Fetches the data about our badges | ||
* @param query { Object }: Filter for badges data | ||
* @return {Promise<badgeModel|Array>} | ||
*/ | ||
const fetchBadges = async ({ | ||
size = 100, | ||
page = 0 | ||
}) => { | ||
try { | ||
const snapshot = await badgeModel | ||
.limit(parseInt(size)) | ||
.offset((parseInt(size)) * (parseInt(page))) | ||
.get() | ||
|
||
const allBadges = [] | ||
snapshot.forEach((doc) => { | ||
allBadges.push(doc.data()) | ||
}) | ||
return allBadges | ||
} catch (err) { | ||
logger.error('Error retrieving badges', err) | ||
return err | ||
} | ||
} | ||
|
||
module.exports = { | ||
fetchBadges | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,37 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const badgeController = require('../controllers/badgeController.js') | ||
|
||
/** | ||
* @swagger | ||
* /badges: | ||
* get: | ||
* summary: Get all the badges in system. | ||
* | ||
* tags: | ||
* - Badges | ||
* responses: | ||
* 200: | ||
* description: Badge details | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* message: | ||
* type: string | ||
* example: Badges returned successfully! | ||
* badges: | ||
* type: array | ||
* items: | ||
* $ref: '#/components/schemas/badges' | ||
* 503: | ||
* description: serverUnavailable | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/errors/serverUnavailable' | ||
*/ | ||
router.get('/', badgeController.getBadges) | ||
|
||
module.exports = router |
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,26 @@ | ||
const chai = require('chai') | ||
const { expect } = chai | ||
const chaiHttp = require('chai-http') | ||
|
||
const app = require('../../server') | ||
|
||
chai.use(chaiHttp) | ||
|
||
describe('Badges', function () { | ||
describe('GET /badges', function () { | ||
it('Should get all the list of badges', function (done) { | ||
chai | ||
.request(app) | ||
.get('/badges') | ||
.end((err, res) => { | ||
if (err) { return done() } | ||
expect(res).to.have.status(200) | ||
expect(res.body).to.be.a('object') | ||
expect(res.body.message).to.equal('Badges returned successfully!') | ||
expect(res.body.badges).to.be.a('array') | ||
|
||
return done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are we adding the badges in the database to
GET
them? 🤔How will this test case pass on CI? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ankush is going to add badges manually in database until we get the authorization in place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the CI also uses firebase emulators that's why we install it on every commit.
I meant the CI doesn't use the production db, right?
Feel free to correct me 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, seems like CI uses emulator, check test.yml file in .git/workflow
CI fetching and adding data in prod DB won't be a good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I'm saying 😅
The prod DB will have the badges details
but the firebase emulator wont and hence the test wouldn't pass 😅
Let me know if I'm wrong 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's the convention, even if the GET Endpoint returns an empty array, the response code will still be 200, and the request is considered fulfilled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool