-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new GET endpoint for /v1/campaign/:id
This commit adds the new endpoint for retreiving a specific campaign by its ID. The endpoint responds with campaign data if found, or a 404 code if the campaign does not exist. Additional wiring between the api folder and api.js file were established.
- Loading branch information
1 parent
af11fad
commit 8634112
Showing
5 changed files
with
72 additions
and
0 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,36 @@ | ||
// below are where the tests for the campaign endpoints will go, I kept getting errors back and couldnt set this up correctly 😞, i got thrown off by the section about inserting test data into the database | ||
|
||
// const request = require('supertest') | ||
// const app = require('../../app') | ||
// const Campaign = require('../../db/models/campaign') | ||
|
||
// describe('v1Campaigns', () => { | ||
// beforeEach(async () => { | ||
// // Insert test data into the database | ||
// await Campaign.query().insert([ | ||
// { name: 'The Breathe Act', organization: 'M4BL', page_url: "breatheact.org", cause: "Civic Rights", type: "Grant" }, | ||
// // { name: 'Campaign 2', description: 'Description 2' }, | ||
// // { name: 'Campaign 3', description: 'Description 3' }, | ||
// ]) | ||
// }) | ||
|
||
// afterEach(async () => { | ||
// // Delete test data from the database | ||
// await Campaign.query().delete() | ||
// }) | ||
|
||
// describe('GET /v1/campaigns/:id', () => { | ||
// test('returns a campaign by ID', async () => { | ||
// const campaign = await Campaign.query().first() | ||
// const response = await request(app).get(`/v1/campaigns/${campaign.id}`) | ||
// expect(response.status).toBe(200) | ||
// expect(response.body).toEqual(campaign) | ||
// }) | ||
|
||
// test('returns a 404 error for non-existent campaign', async () => { | ||
// const response = await request(app).get('/v1/campaigns/999') | ||
// expect(response.status).toBe(404) | ||
// expect(response.text).toBe('Campaign not found') | ||
// }) | ||
// }) | ||
// }) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,21 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const Campaign = require('../../db/models/campaign') | ||
|
||
router.get('/:id', async (req, res) => { | ||
try { | ||
const campaign = await Campaign.query().findById(req.params.id) | ||
if (campaign) { | ||
res.send(campaign) | ||
} else { | ||
res.status(404).send('Campaign not found') | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
res.status(500).send('Internal Server Error') | ||
} | ||
}) | ||
|
||
// below is where you would find the V1 endpoints for the GET all campaigns, POST a new campaign, PUT an existing campaign, and DELETE (maye) a campaign | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const v1Campaigns = require('./api/v1Campaigns') | ||
// const v1Admin = require('./api/v1Admin') this had to be commented out in order for the error regarding missing modules to be fixed. | ||
|
||
router.use('/campaigns', v1Campaigns) | ||
// router.use('/admin', v1Admin) this had to be commented out in order for the error regarding missing modules to be fixed. | ||
|
||
module.exports = router |