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

Create index.http to test server for lesson #9

Open
wants to merge 2 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ You will learn how to develop RESTful APIs with Node.js, and Express.

Implement all steps needed to build a secure API.

## In Class

Replicate the product.js
This file will be named 'logos.js' with all of the CRUD features.
25 changes: 25 additions & 0 deletions index.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### Test the home page endpoint

GET http://localhost:3001

### Test a 404
GET http://localhost:3001/testing-404

### Test creating a product
POST http://localhost:3001/
// need to add in Content-Type to tell the body-parser that it's json.
Content-Type: application/json

{
"title": "Golf shirts"
}

### Test the PUT which should update a product

PUT http://localhost:3001/5f2b56f5a7cbec0cf269529f
// need to add in Content-Type to tell the body-parser that it's json for PUT as well.
Content-Type: application/json

{
"title": "Comfy shirt"
}
128 changes: 60 additions & 68 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions src/app-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ const bodyParser = require('body-parser');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const {startDatabase} = require('./database/mongo-common');
const { startDatabase } = require('./database/mongo-common');
// alternative:
// const mongo = require('./database/mongo-common');
// mongo.startDatabase

const {deleteProduct, updateProduct, createProduct, getProducts} = require('./database/products');
const { deleteProduct, updateProduct, createProduct, getProducts } = require('./database/products');

// Other entities: Logos, CustomizationOptions, Material, Patterns
// These would get their own files: Logos.js for example, like products in this repo

// Bonus items: , Customer info, etc

Expand Down
53 changes: 53 additions & 0 deletions src/database/logos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { getDatabase } = require('./mongo-common');
// https://docs.mongodb.com/manual/reference/method/ObjectId/
const { ObjectID } = require('mongodb');

// a "collection" in mongo is a lot like a list which is a lot like an Array
const collectionName = 'logos';

async function createLogo(logo) {
const database = await getDatabase();
// for `insertOne` info, see https://docs.mongodb.com/manual/reference/method/js-collection/
const { insertedId } = await database.collection(collectionName).insertOne(logo);
return insertedId;
}

async function getLogos() {
const database = await getDatabase();
// `find` https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
return await database.collection(collectionName).find({}).toArray();
}

async function deleteLogo(id) {
const database = await getDatabase();
// https://docs.mongodb.com/manual/reference/method/ObjectId/
// for `deleteOne` info see https://docs.mongodb.com/manual/reference/method/js-collection/
await database.collection(collectionName).deleteOne({
_id: new ObjectID(id),
});
}

async function updateLogo(id, logo) {
const database = await getDatabase();

// `delete` is new to you. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
delete logo._id;

// https://docs.mongodb.com/manual/reference/method/db.collection.update/
await database.collection(collectionName).update(
{ _id: new ObjectID(id), },
{
$set: {
...logo,
},
},
);
}

// export the functions that can be used by the main app code
module.exports = {
createLogo,
getLogos,
deleteLogo,
updateLogo,
};
Loading