Skip to content

Commit

Permalink
Add new GET endpoint for /v1/campaign/:id
Browse files Browse the repository at this point in the history
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
briansegura15 committed Nov 2, 2023
1 parent 8634112 commit f144155
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions server/__tests__/integration/v1.campaigns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
// // { 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')
// })
// })
// })
// })
3 changes: 1 addition & 2 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const lob = require('./routes/api/lob')
const checkout = require('./routes/api/checkout')
//const twilio = require('./routes/api/twilio')
const eventLogger = require('./routes/api/event_logger')
const v1Router = require("./routes/v1Router")

const v1Router = require('./routes/v1Router')

// Created a nested router
const apiRouter = express.Router()
Expand Down
2 changes: 1 addition & 1 deletion server/routes/api/v1Admin.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// this is where all the admin endpoints will exist, this had to be commented out in order for the error regarding missing modules to be fixed.
// this is where all the admin endpoints will exist, this had to be commented out in order for the error regarding missing modules to be fixed.
24 changes: 12 additions & 12 deletions server/routes/api/v1Campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ 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')
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
// 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
2 changes: 1 addition & 1 deletion server/routes/v1Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const v1Campaigns = require('./api/v1Campaigns')
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
module.exports = router

0 comments on commit f144155

Please sign in to comment.