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 af11fad commit 8634112
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
36 changes: 36 additions & 0 deletions server/__tests__/integration/v1.campaigns.test.js
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')
// })
// })
// })
5 changes: 5 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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")


// Created a nested router
const apiRouter = express.Router()
Expand All @@ -36,4 +38,7 @@ apiRouter.use('/checkout', checkout)
//apiRouter.use('/twilio', twilio)
apiRouter.use('/event_logger', eventLogger)

// v1 routes
apiRouter.use('/v1', v1Router)

module.exports = apiRouter
1 change: 1 addition & 0 deletions server/routes/api/v1Admin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions server/routes/api/v1Campaigns.js
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
9 changes: 9 additions & 0 deletions server/routes/v1Router.js
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

0 comments on commit 8634112

Please sign in to comment.