diff --git a/README.md b/README.md index 76d6d581..0f1f5d83 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# Welcome to the Integrating With HubSpot I: Foundations Practicum +# Welcome to the Integrating With HubSpot I: Foundations Practicum This repository is for the Integrating With HubSpot I: Foundations course. This practicum is one of two requirements for receiving your Integrating With HubSpot I: Foundations certification. You must also take the exam and receive a passing grade (at least 75%). To read the full directions, please go to the [practicum instructions](https://app.hubspot.com/academy/l/tracks/1092124/1093824/5493?language=en). -**Put your HubSpot developer test account custom objects URL link here:** https://app.hubspot.com/contacts/l/objects/${custom-obj-number}/views/all/list +**Put your HubSpot developer test account custom objects URL link here:** https://app-eu1.hubspot.com/contacts/146477196/objects/2-144466597/views/all/list ___ ## Tips: diff --git a/index.js b/index.js index f337a32d..0727753b 100644 --- a/index.js +++ b/index.js @@ -7,20 +7,59 @@ app.use(express.static(__dirname + '/public')); app.use(express.urlencoded({ extended: true })); app.use(express.json()); -// * Please DO NOT INCLUDE the private app access token in your repo. Don't do this practicum in your normal account. -const PRIVATE_APP_ACCESS = ''; +require('dotenv').config(); +const PRIVATE_APP_ACCESS = process.env.HUBSPOT_ACCESS_TOKEN; -// TODO: ROUTE 1 - Create a new app.get route for the homepage to call your custom object data. Pass this data along to the front-end and create a new pug template in the views folder. +app.get('/', async (req, res) => { + const url = 'https://api.hubapi.com/crm/v3/objects/2-144466597?properties=name,country,image_url,website,opening_date'; + const headers = { + Authorization: `Bearer ${PRIVATE_APP_ACCESS}`, + 'Content-Type': 'application/json' + } -// * Code for Route 1 goes here + try { + const resp = await axios.get(url, { headers }); + const parks = resp.data.results; + res.render('homepage', { title: 'My parks', parks }); + } catch (error) { + console.error('Error fetching parks:', error); + res.status(500).send('Error fetching parks data'); + } +}); -// TODO: ROUTE 2 - Create a new app.get route for the form to create or update new custom object data. Send this data along in the next route. +app.get('/update-cobj', (req, res) => { + res.render('updates', { title: 'Update Custom Object Form | Integrating With HubSpot I Practicum'}); +}); -// * Code for Route 2 goes here +app.post('/update-cobj', async (req, res) => { + const { name, country, image_url, website, opening_date } = req.body; + + const url = 'https://api.hubapi.com/crm/v3/objects/2-144466597'; + const headers = { + Authorization: `Bearer ${PRIVATE_APP_ACCESS}`, + 'Content-Type': 'application/json' + }; + + const data = { + properties: { + name, + country, + image_url, + website, + opening_date + } + }; + + try { + await axios.post(url, data, { headers }); + res.redirect('/'); + } catch (error) { + console.error('Error creating park:', error); + res.status(500).send('Error creating park'); + } +}); -// TODO: ROUTE 3 - Create a new app.post route for the custom objects form to create or update your custom object data. Once executed, redirect the user to the homepage. -// * Code for Route 3 goes here /** * * This is sample code to give you a reference for how you should structure your calls. diff --git a/package.json b/package.json index 62db37aa..260931cf 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "axios": "^1.3.5", + "dotenv": "^17.0.1", "express": "^4.18.2", "pug": "^3.0.2" } diff --git a/views/homepage.pug b/views/homepage.pug new file mode 100644 index 00000000..a5ef409b --- /dev/null +++ b/views/homepage.pug @@ -0,0 +1,45 @@ +doctype html +html + head + title= title + style. + table, th, td { + border: 1px solid black; + border-collapse: collapse; + padding: 8px; + } + img { + max-width: 100px; + } + body + h1= title + + a(href="/update-cobj") Add to this table + + if parks.length + table + thead + tr + th Name + th Country + th Website + th Image + th Opening Date + tbody + each park in parks + tr + td= park.properties.name + td= park.properties.country + td + if park.properties.website + a(href=park.properties.website target="_blank")= park.properties.website + else + | - + td + if park.properties.image_url + img(src=park.properties.image_url alt="park image") + else + | No image + td= park.properties.opening_date + else + p No parks to display. diff --git a/views/updates.pug b/views/updates.pug new file mode 100644 index 00000000..f9567103 --- /dev/null +++ b/views/updates.pug @@ -0,0 +1,27 @@ +doctype html +html + head + title= title + body + h1= title + + a(href="/") Return to the homepage + + form(action="/update-cobj" method="POST") + div + label(for="name") Name: + input(type="text" name="name" required) + div + label(for="country") Country: + input(type="text" name="country") + div + label(for="website") Website: + input(type="url" name="website") + div + label(for="image_url") Image URL: + input(type="url" name="image_url") + div + label(for="opening_date") Opening Date: + input(type="date" name="opening_date") + div + button(type="submit") Submit