Skip to content
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

completed the Authentication process #170

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions functions/.creds-sample.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"COUCH_URL": "",
"IAM_API_KEY": ""
"COUCH_URL": "https://1019b037-d6c8-437c-bdca-d8f02dd27dd5-bluemix.cloudantnosqldb.appdomain.cloud",
"IAM_API_KEY": "NzfXNIB_O7o4ozaVk1TWRKvvqQL7w4K1gm5L-Df38Zv2",
"COUCH_USERNAME": "1019b037-d6c8-437c-bdca-d8f02dd27dd5-bluemix"
}
121 changes: 121 additions & 0 deletions functions/get-dealership.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const Cloudant = require('@cloudant/cloudant');

// Initialize Cloudant connection with IAM authentication
async function dbCloudantConnect() {
try {
const cloudant = Cloudant({
plugins: { iamauth: { iamApiKey: 'NzfXNIB_O7o4ozaVk1TWRKvvqQL7w4K1gm5L-Df38Zv2' } }, // Replace with your IAM API key
url: 'https://1019b037-d6c8-437c-bdca-d8f02dd27dd5-bluemix.cloudantnosqldb.appdomain.cloud', // Replace with your Cloudant URL
});

const db = cloudant.use('dealerships');
console.info('Connect success! Connected to DB');
return db;
} catch (err) {
console.error('Connect failure: ' + err.message + ' for Cloudant DB');
throw err;
}
}

let db;

(async () => {
db = await dbCloudantConnect();
})();

app.use(express.json());

// Define a route to get all dealerships with optional state and ID filters
app.get('/dealerships/get', (req, res) => {
const { state, id } = req.query;

// Create a selector object based on query parameters
const selector = {};
if (state) {
selector.state = state;
}

if (id) {
selector.id = parseInt(id); // Filter by "id" with a value of 1
}

const queryOptions = {
selector,
limit: 10, // Limit the number of documents returned to 10
};

db.find(queryOptions, (err, body) => {
if (err) {
console.error('Error fetching dealerships:', err);
res.status(500).json({ error: 'An error occurred while fetching dealerships.' });
} else {
const dealerships = body.docs;
res.json(dealerships);
}
});
});


// Define a route to get a dealership by ID
app.get('/dealerships/:id', (req, res) => {
const dealershipId = parseInt(req.params.id);

if (isNaN(dealershipId)) {
return res.status(400).json({ error: 'Invalid dealership ID' });
}

const queryOptions = {
selector: {
id: dealershipId,
},
limit: 1, // Limit to one document as the ID should be unique
};

db.find(queryOptions, (err, body) => {
if (err) {
console.error('Error fetching dealership by ID:', err);
res.status(500).json({ error: 'An error occurred while fetching dealership by ID.' });
} else {
const dealership = body.docs[0];
if (dealership) {
res.json(dealership);
} else {
res.status(404).json({ error: 'Dealership not found' });
}
}
});
});

app.get('/dealerships/state/:state', (req, res) => {
const dealershipState = req.params.state; // Note: The state parameter is a string, not an integer

const queryOptions = {
selector: {
state: dealershipState,
},
limit: 1, // Limit to one document as the ID should be unique
};

db.find(queryOptions, (err, body) => {
if (err) {
console.error('Error fetching dealership by state:', err);
res.status(500).json({ error: 'An error occurred while fetching dealership by state.' });
} else {
const dealership = body.docs[0];
if (dealership) {
res.json(dealership);
} else {
res.status(404).json({ error: 'Dealership not found' });
}
}
});
});



app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Binary file added functions/get-dealership.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added functions/get-reviews.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added functions/get-state.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions functions/node_modules/.bin/follow

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

17 changes: 17 additions & 0 deletions functions/node_modules/.bin/follow.cmd

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

28 changes: 28 additions & 0 deletions functions/node_modules/.bin/follow.ps1

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

16 changes: 16 additions & 0 deletions functions/node_modules/.bin/mime

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

17 changes: 17 additions & 0 deletions functions/node_modules/.bin/mime.cmd

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

28 changes: 28 additions & 0 deletions functions/node_modules/.bin/mime.ps1

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

16 changes: 16 additions & 0 deletions functions/node_modules/.bin/nodemon

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

17 changes: 17 additions & 0 deletions functions/node_modules/.bin/nodemon.cmd

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

28 changes: 28 additions & 0 deletions functions/node_modules/.bin/nodemon.ps1

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

16 changes: 16 additions & 0 deletions functions/node_modules/.bin/nodetouch

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

17 changes: 17 additions & 0 deletions functions/node_modules/.bin/nodetouch.cmd

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

28 changes: 28 additions & 0 deletions functions/node_modules/.bin/nodetouch.ps1

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

Loading