From d830fd9cca9b9c97e82841be7227eedc310faafe Mon Sep 17 00:00:00 2001 From: Damian Montero Date: Sat, 6 Aug 2022 09:04:35 -0400 Subject: [PATCH 1/2] Adding documentation and changing it to hide the credentials.js file --- .gitignore | 3 ++- README.md | 9 +++++++++ credentials-example.js | 1 + mongoConnect.js | 4 +++- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 README.md create mode 100644 credentials-example.js diff --git a/.gitignore b/.gitignore index 25c8fdb..745a569 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -package-lock.json \ No newline at end of file +package-lock.json +credentials.js \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b31bc3a --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Our MongoDB Api + +This project showcases what we've learned to make an api that will access a "movie" collection in our mongoDB database + +## To get started + +1) Clone it locally and run ```npm i``` (in the terminal) to install all dependencies +2) rename the ```credentials-example.js``` to ```credentials.js``` and edit the ```uri``` string to either be the one you get from your own cloud.mongodb.com cluster (under the "connect" button) OR you can us ```mongodb://localhost:27017``` if you want to connect to a local instance. +3) you can add new movies by sending a valid JSON body to```http://localhost:4001/add-movie``` and get movies by going to ```http://localhost:4001/get-movies``` diff --git a/credentials-example.js b/credentials-example.js new file mode 100644 index 0000000..36f3134 --- /dev/null +++ b/credentials-example.js @@ -0,0 +1 @@ +export const uri = ' replace this string (from quote to quote) with the full string from cloud.mongodb.com or mongodb://localhost:27017 like we had before ' diff --git a/mongoConnect.js b/mongoConnect.js index 733b693..20956de 100644 --- a/mongoConnect.js +++ b/mongoConnect.js @@ -1,6 +1,8 @@ import { MongoClient } from 'mongodb' + +import { uri } from "./credentials.js" -const uri = 'mongodb://localhost:27017' +// const uri = 'mongodb://localhost:27017' const client = new MongoClient(uri) // creating client mongo instance const db = client.db('bocacode-test') // connecting to db = bocacod-test From 15ff5618f95dd47b533d268a84d45fcab8619e53 Mon Sep 17 00:00:00 2001 From: Damian Montero Date: Tue, 9 Aug 2022 09:29:14 -0400 Subject: [PATCH 2/2] Adding the credentials.js file back for documentation purposes --- .gitignore | 3 ++- employees.js | 17 +++++++++++++++++ index.js | 41 ++++++++++++++--------------------------- mongoConnect.js | 14 ++++++++++---- movies.js | 36 ++++++++++++++++-------------------- package.json | 7 ++++++- readme.md | 11 +++++++++++ 7 files changed, 76 insertions(+), 53 deletions(-) create mode 100644 employees.js create mode 100644 readme.md diff --git a/.gitignore b/.gitignore index 745a569..3d98f48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules package-lock.json -credentials.js \ No newline at end of file +credentials.js +.env* diff --git a/employees.js b/employees.js new file mode 100644 index 0000000..e739ffe --- /dev/null +++ b/employees.js @@ -0,0 +1,17 @@ +import db from './mongoConnect.js' + +const employees = db.collection('employees') + +export const addEmployee = async (req, res) => { + if (req.body.firstName && req.body.lastName && req.body.position) { + try { + const newEmployee = await employees.insertOne(req.body) + res.send(newEmployee) + } catch (err) { + res.send('error adding employee to database') + throw err + } + } else { + res.send('REQUEST body needs to have a firstName, lastName and position field!') + } +} diff --git a/index.js b/index.js index 08178c1..66698b6 100644 --- a/index.js +++ b/index.js @@ -1,39 +1,26 @@ import express from 'express' import cors from 'cors' +import dotenv from 'dotenv' +dotenv.config() + import { addMovie, queryMovies, deleteMovie, updateMovie } from './movies.js' +import { addEmployee } from './employees.js' const app = express() app.use(cors()) app.use(express.json()) // for us to receive json in body for POST -const PORT = 4001 - -// CRUD -app.post('/add-movie', async (req, res) => { - const result = await addMovie(req.body) - res.send(result) -}) - -app.put('/update-movie', async (req, res) => { - const result = await updateMovie(req.query, req.body) - res.send(result) -}) - -app.delete('/delete-movie', async (req, res) => { - const result = await deleteMovie(req.query) - res.send(result) -}) +const PORT = process.env.PORT || 4045 -app.get('/get-movies', async (req, res) => { - const data = await queryMovies() - res.send(data) -}) +// movie routes +app.post('/add-movie', addMovie) +app.put('/update-movie', updateMovie) +app.delete('/delete-movie', deleteMovie) +app.get('/get-movies', queryMovies) -app.get('/', (req, res) => { - res.send('Hey here is my API!!') -}) +// employee routes +app.post('/add-employee', addEmployee) -app.listen(PORT, () => { - console.log('API running on ', PORT) -}) +app.get('/', (req, res) => res.send('Hey here is my API!!')) +app.listen(PORT, () => console.log('API running on ', PORT)) diff --git a/mongoConnect.js b/mongoConnect.js index 20956de..0901ccf 100644 --- a/mongoConnect.js +++ b/mongoConnect.js @@ -1,11 +1,17 @@ import { MongoClient } from 'mongodb' - -import { uri } from "./credentials.js" +import dotenv from 'dotenv' +dotenv.config() +// If you'll be using the credentials then rename the credentials-example.js to credendials.js and add your info +//import { uri } from "./credentials.js" + +// If you're connecgting to your local server on your laptop // const uri = 'mongodb://localhost:27017' +// If you're using the .env file with a MONGO_URI entry +const uri = process.env.MONGO_URI + const client = new MongoClient(uri) // creating client mongo instance const db = client.db('bocacode-test') // connecting to db = bocacod-test -const movies = db.collection('movie') // getting the movie collection -export default movies \ No newline at end of file +export default db \ No newline at end of file diff --git a/movies.js b/movies.js index 0e9f4f1..93ac5c3 100644 --- a/movies.js +++ b/movies.js @@ -1,30 +1,26 @@ -import movies from './mongoConnect.js' +import db from './mongoConnect.js' -export const queryMovies = async () => { - // movies.find().toArray((err, result) => { - // console.log(result) - // }) +const movies = db.collection('movie') // getting the movie collection +export const queryMovies = async (req, res) => { const allMovies = await movies.find().toArray() - - return allMovies + res.send(allMovies) } -export const addMovie = async (body) => { - const movieAdded = await movies.insertOne(body) - return movieAdded +export const addMovie = async (req, res) => { + await movies.insertOne(req.body) + const allMovies = await movies.find().toArray() + res.send(allMovies) } -export async function updateMovie(query, body) { - const movieUpdated = await movies.findOneAndUpdate(query, { $set: body }) - return movieUpdated +export async function updateMovie(req, res) { + await movies.findOneAndUpdate(req.query, { $set: req.body }) + const movieUpdated = await movies.find(req.query).toArray() + res.send(movieUpdated) } -export async function deleteMovie(movieToDelete) { - // movies.findOneAndDelete({ name: 'Frozen' }, (err, result) => { - // console.log(result) - // }) - - const movieDeleted = await movies.findOneAndDelete(movieToDelete) - return movieDeleted +export async function deleteMovie(req, res) { + await movies.findOneAndDelete(req.query) + const allMovies = await movies.find().toArray() + res.send(allMovies) } diff --git a/package.json b/package.json index dbaf4da..a8b8932 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,19 @@ "description": "", "type": "module", "main": "index.js", + "engines": { + "node": "16.x" + }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node index.js" }, "keywords": [], "author": "Jonathan Sanchez", "license": "ISC", "dependencies": { "cors": "^2.8.5", + "dotenv": "^16.0.1", "express": "^4.18.1", "mongodb": "^4.8.1" } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..9bdd398 --- /dev/null +++ b/readme.md @@ -0,0 +1,11 @@ +# Node API with MongoDb +This project showcases what we've learned to make an api that will access a "movie" collection in our mongoDB database + +## To get started + +1. After cloning project install dependencies with `npm i` +2. Connect to MongoDB (if local start with `brew services start mongo-community) +3. Run API with nodemon + +## How to use it: +you can add new movies by sending a valid JSON body to```http://localhost:4001/add-movie``` and get movies by going to ```http://localhost:4001/get-movies``` \ No newline at end of file